diff options
Diffstat (limited to 'entity.scm')
| -rw-r--r-- | entity.scm | 23 |
1 files changed, 8 insertions, 15 deletions
@@ -2,34 +2,27 @@ * (import scheme (chicken base) - (chicken keyword) - (only srfi-1 fold)) + (only srfi-1 fold alist-delete)) - ;; Entities = plists with shared keys (#:type, #:x, #:y, #:width, #:height, ...). + ;; Entities = alists with shared keys (#:type, #:x, #:y, #:width, #:height, ...). (define (make-entity x y w h) - (list #:type 'none #:x x #:y y #:width w #:height h)) + `((#:type . none) (#:x . ,x) (#:y . ,y) (#:width . ,w) (#:height . ,h))) (define (entity-ref entity key #!optional default) - (get-keyword key entity (if (procedure? default) default (lambda () default)))) + (cond ((assq key entity) => cdr) + ((procedure? default) (default)) + (else default))) (define (entity-type entity) (entity-ref entity #:type #f)) (define (entity-set entity key val) - (let ((cleaned (let loop ((lst entity) (acc '())) - (if (null? lst) - (reverse acc) - (let ((k (car lst)) - (v (cadr lst))) - (if (eq? k key) - (loop (cddr lst) acc) - (loop (cddr lst) (cons v (cons k acc))))))))) - (cons key (cons val cleaned)))) + (cons (cons key val) (alist-delete key entity eq?))) (define (entity-set-many entity pairs) (fold (lambda (pair working-ent) - (entity-set working-ent (car pair) (if (list? (cdr pair)) (cadr pair) (cdr pair)))) + (entity-set working-ent (car pair) (cdr pair))) entity pairs)) |
