From: Javier Sancho Date: Thu, 8 Aug 2013 19:39:47 +0000 (+0200) Subject: It's not obligatory to write type components at declarations; if X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=c4ba277c7f9d23516926693b48e704ff3ea5e8f9;p=gacela.git It's not obligatory to write type components at declarations; if component is a Gacela component (a special record), type is detected automatically. * src/system.scm: normalize-components - returns a list with pairs (type . component) --- diff --git a/src/system.scm b/src/system.scm index d356665..9df7cdc 100644 --- a/src/system.scm +++ b/src/system.scm @@ -66,6 +66,14 @@ ;;; Entities and components +(define (normalize-components components) + (map + (lambda (c) + (if (record? c) + `(,(get-component-type c) . ,c) + c)) + components)) + (define (register-components entity components clist) (cond ((null? components) clist) (else @@ -90,11 +98,12 @@ (assoc-set! clist type elist)))))))) (define (new-entity new-components entities components) - (let ((key (gensym))) + (let ((key (gensym)) + (nc (normalize-components new-components))) (values - (acons key new-components entities) + (acons key nc entities) (register-components key - (map (lambda (c) (car c)) new-components) + (map (lambda (c) (car c)) nc) components) key))) @@ -105,10 +114,11 @@ (unregister-components key clist components)))) (define (set-entity key new-components entities components) - (let ((clist (map (lambda (c) (car c)) (assoc-ref entities key))) - (nclist (map (lambda (c) (car c)) new-components))) + (let* ((nc (normalize-components new-components)) + (clist (map (lambda (c) (car c)) (assoc-ref entities key))) + (nclist (map (lambda (c) (car c)) nc))) (values - (assoc-set! entities key new-components) + (assoc-set! entities key nc) (register-components key (lset-difference eq? nclist clist) (unregister-components key (lset-difference eq? clist nclist) components))))) @@ -122,7 +132,7 @@ (assoc-set! clist (caar new-components) (cdar new-components)) (assoc-remove! clist (caar new-components))) (cdr new-components))))) - (set-entity key (set-components (alist-copy (assoc-ref entities key)) new-components) entities components)) + (set-entity key (set-components (alist-copy (assoc-ref entities key)) (normalize-components new-components)) entities components)) (define (set-entities new-entities entities components) (cond ((null? new-entities) diff --git a/src/test.scm b/src/test.scm index 8b7723f..021afef 100644 --- a/src/test.scm +++ b/src/test.scm @@ -30,45 +30,45 @@ (let ((entities '()) (components '()) (key #f)) - (receive (e c k) (new-entity `((a . ,(make-a 1 2)) (b . ,(make-b))) entities components) + (receive (e c k) (new-entity (list (make-a 1 2) (make-b)) entities components) (set! entities e) (set! components c) (set! key k) (display k) (newline)) - (format #t "~a~%~a~%~%" entities components) + (format #t "New entity with a and b:~%~a~%~a~%~%" entities components) - (receive (e c k) (new-entity `((a . ,(make-a 10 20))) entities components) + (receive (e c k) (new-entity (list (make-a 10 20)) entities components) (set! entities e) (set! components c) (display k) (newline)) - (format #t "~a~%~a~%~%" entities components) + (format #t "New entity with a:~%~a~%~a~%~%" entities components) - (receive (e c) (set-entity-components key `((b . #f) (a . ,(make-a 50 50))) entities components) + (receive (e c) (set-entity-components key `((b . #f) ,(make-a 50 50)) entities components) (set! entities e) (set! components c)) - (format #t "~a~%~a~%~%" entities components) + (format #t "First entity removes b and changes a:~%~a~%~a~%~%" entities components) (receive (e c) (remove-entity key entities components) (set! entities e) (set! components c)) - (format #t "~a~%~a~%~%" entities components) + (format #t "Removes first entity:~%~a~%~a~%~%" entities components) - (receive (e c k) (new-entity `((a . ,(make-a 1 2)) (b . ,(make-b))) entities components) + (receive (e c k) (new-entity (list (make-a 1 2) (make-b)) entities components) (set! entities e) (set! components c) (set! key k) (display k) (newline)) - (format #t "~a~%~a~%~%" entities components) + (format #t "New entity with a and b:~%~a~%~a~%~%" entities components) - (receive (e c) (set-entities `((,key . ((b . #f) (a . ,(make-a 50 50)))) (#f . ((a . ,(make-a 1000 1000))))) entities components) + (receive (e c) (set-entities `((,key . ((b . #f) ,(make-a 50 50))) (#f . (,(make-a 1000 1000)))) entities components) (set! entities e) (set! components c)) - (format #t "~a~%~a~%~%" entities components) + (format #t "Last entity removes b and changes a, and new entity with a:~%~a~%~a~%~%" entities components) (receive (e c) (set-entities `((,key . #f)) entities components) (set! entities e) (set! components c)) - (format #t "~a~%~a~%~%" entities components) + (format #t "Remove last entity:~%~a~%~a~%~%" entities components) )) (export test1) @@ -77,10 +77,10 @@ (define (test2) (let ((entities '()) (components '())) - (receive (e c) (((make-system '() (lambda (e) `((#f . ((a . ,(make-a 1 2)))) (#f . ((a . ,(make-a 10 20))))))) entities components)) + (receive (e c) (((make-system '() (lambda (e) `((#f . (,(make-a 1 2))) (#f . (,(make-a 10 20)))))) entities components)) (set! entities e) (set! components c)) - (format #t "~a~%~a~%~%" entities components) + (format #t "Two new entities with a:~%~a~%~a~%~%" entities components) (((make-system '(a) (lambda (e) (display e) (newline) '())) entities components)) ))