]> git.jsancho.org Git - gacela.git/blobdiff - src/system.scm
Functions to merge systems, using a linear way or using threads
[gacela.git] / src / system.scm
index d3566652bd0e8e995fe0fe684d4bd6dfdfc3aee1..3c8454821b733e432dcef33fb9efb8d900bd4d10 100644 (file)
 
 ;;; 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)
          (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)