From 8251c85a4a588504d38a2fad05e4b0fe1cdccb9d Mon Sep 17 00:00:00 2001 From: Gene Pasquet Date: Fri, 17 Apr 2026 16:30:34 +0100 Subject: Convert entities to alists --- entity.scm | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) (limited to 'entity.scm') diff --git a/entity.scm b/entity.scm index 7c29bf7..540a2c9 100644 --- a/entity.scm +++ b/entity.scm @@ -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)) -- cgit v1.2.3