X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=src%2Fsystem.scm;h=3c8454821b733e432dcef33fb9efb8d900bd4d10;hb=0f49b8fac5821694a2db6f88b95423e5ad8aa719;hp=d3566652bd0e8e995fe0fe684d4bd6dfdfc3aee1;hpb=1b809e1a58ec2a94903b4b44101d706fd0f27c81;p=gacela.git diff --git a/src/system.scm b/src/system.scm index d356665..3c84548 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) @@ -170,5 +180,33 @@ (set-entities res e c)))))) +(define (join-systems . systems) + (lambda (entities components) + (let run ((s systems) (e entities) (c components)) + (cond ((null? s) + (values e c)) + (else + (receive (e2 c2) (((car s) e c)) + (run (cdr s) e2 c2))))))) + + +(define (threaded-systems . systems) + (lambda (entities components) + (let run-wait ((thd + (map + (lambda (s) + (call-with-new-thread + (lambda () (s entities components)))) + systems)) + (e entities) (c components)) + (cond ((null? thd) + (values e c)) + (else + (receive (e2 c2) ((join-thread (car thd)) e c) + (run-wait (cdr thd) e2 c2))))))) + + (export find-entities-by-components - make-system) + make-system + join-systems + threaded-systems)