]> git.jsancho.org Git - gacela.git/blobdiff - src/system.scm
It's not obligatory to write type components at declarations; if
[gacela.git] / src / system.scm
index 915e51e88ee71f27288e4181f011133e6131b4b7..9df7cdcc5a701d84783f04e7157497936888513f 100644 (file)
@@ -16,6 +16,7 @@
 
 
 (define-module (gacela system)
+  #:use-module (ice-9 receive)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-9))
 
 
 ;;; 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 (map (lambda (c) `(,(get-component-type c) . ,c)) new-components) entities)
-     (register-components
-      key
-      (map (lambda (c) (get-component-type c)) new-components)
-      components)
+     (acons key nc entities)
+     (register-components key
+                         (map (lambda (c) (car c)) nc)
+                         components)
      key)))
 
 (define (remove-entity key entities components)
     (values
      (assoc-remove! entities key)
      (unregister-components key clist components))))
+
+(define (set-entity key new-components entities 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 nc)
+     (register-components key (lset-difference eq? nclist clist)
+                         (unregister-components key (lset-difference eq? clist nclist) components)))))
+
+(define (set-entity-components key new-components entities components)
+  (define (set-components clist new-components)
+    (cond ((null? new-components)
+          clist)
+         (else
+          (set-components
+           (if (cdar new-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)) (normalize-components new-components)) entities components))
+
+(define (set-entities new-entities entities components)
+  (cond ((null? new-entities)
+        (values entities components))
+       (else
+        (cond ((not (caar new-entities))
+               (receive (e c k) (new-entity (cdar new-entities) entities components)
+                        (set-entities (cdr new-entities) e c)))
+              ((not (cdar new-entities))
+               (receive (e c) (remove-entity (caar new-entities) entities components)
+                        (set-entities (cdr new-entities) e c)))
+              (else
+               (receive (e c) (set-entity-components (caar new-entities) (cdar new-entities) entities components)
+                        (set-entities (cdr new-entities) e c)))))))
+
    
 (export new-entity
-       remove-entity)
+       remove-entity
+       set-entity
+       set-entity-components
+       set-entities)
 
 
 ;;; Making systems
   (lambda (entities components)
     (let* ((e (find-entities-by-components components component-types))
           (e* (map (lambda (x) (assoc x entities)) e))
-          (e** (map (lambda (x) (cons (car x) (filter (lambda (x) (memq (get-component-type x) component-types)) (cdr x)))) e*))
+          (e** (map (lambda (x) (cons (car x) (filter (lambda (x) (memq (car x) component-types)) (cdr x)))) e*))
           (res (system-fun e**)))
       (lambda* (#:optional (entities2 #f) (components2 #f))
-        (let* ((e2 (if (and entities2 components2)
-                      (find-entities-by-components components2 component-types)
-                      e))
-              (e2* (if (and entities2 components2)
-                       (map (lambda (x) (assoc x entities2)) e2)
-                       e*))
-              (e2** (if (and entities2 components2)
-                        (map (lambda (x) (cons (car x) (filter (lambda (x) (memq (get-component-type x) component-types)) (cdr x)))) e2*)
-                        e**)))
-         e2**)))))
-
-; ((1 a b) (2 a b c) (3 c))
-; ((1 a b) (2 a b))
-; ((1 a) (a b))
-; ((1 a) (3 c) (4 a b))
+       (let ((e (if (and entities2 components2) entities2 entities))
+             (c (if (and entities2 components2) components2 components)))
+         (set-entities res e c))))))
+
 
 (export find-entities-by-components
        make-system)