;;; 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
(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)))
(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)))))
(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)
(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)
(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))
))