]> git.jsancho.org Git - gacela.git/blobdiff - src/system.scm
Trash
[gacela.git] / src / system.scm
index 1df24f9ba03d711b4ba6f7097e83aa273aded760..50e9534b451f34c961150745e30bf49f433133ac 100644 (file)
 
 
 (define-module (gacela system)
+  #:use-module ((bongodb) #:renamer (symbol-prefix-proc 'bongodb:))
   #:use-module (ice-9 receive)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-9)
-  #:use-module (srfi srfi-9 gnu))
+  #:use-module (srfi srfi-9 gnu)
+  #:export (define-component
+           export-component
+           get-component-type
+           make-entity-set
+           entity-list
+           entity-count
+           new-entity
+           get-entity
+           remove-entity
+           set-entity
+           set-entity-components
+           remove-entity-components
+           modify-entities
+           entities-changes
+           entities-changes?
+           get-entities-changes
+           find-entities-by-components
+           define-system
+           make-system
+           join-systems
+           thread-systems
+           get-key
+           get-component))
 
 
 ;;; Component definitions
 (define (get-component-type component)
   (record-type-name (record-type-descriptor component)))
 
-(export define-component
-       export-component
-       get-component-type)
-
 
 ;;; Entities and components
 
 (define (make-entity-set . changes)
-  (modify-entities
-   (cons (make-hash-table) (make-hash-table))
-   changes))
+  (modify-entities (bongodb:make-collection) changes))
 
 (define (entity-list entity-set)
-  (hash-map->list (lambda (k v) (cons k v)) (car entity-set)))
+  (bongodb:find entity-set))
 
 (define (entity-count entity-set)
-  (hash-count (const #t) (car entity-set)))
+  (bongodb:count entity-set))
 
 (define (normalize-components components)
   (map
         c))
    components))
 
-(define (register-components entity components clist)
-  (cond ((null? components) clist)
-       (else
-        (let* ((type (car components))
-               (elist (hash-ref clist type)))
-          (hash-set! clist type
-            (cond (elist
-                   (lset-adjoin eq? elist entity))
-                  (else
-                   (list entity))))
-          (register-components entity (cdr components) clist)))))
-
-(define (unregister-components entity components clist)
-  (cond ((null? components) clist)
-       (else
-        (let* ((type (car components))
-               (elist (lset-difference eq? (hash-ref clist type) (list entity))))
-          (cond ((null? elist)
-                 (hash-remove! clist type))
-                (else
-                 (hash-set! clist type elist)))
-          (unregister-components entity (cdr components) clist)))))
-
-(define (component-names components)
-  (map (lambda (c) (car c)) components))
-
-(define (entity-component-names key entity-set)
-  (component-names
-   (hash-ref (car entity-set) key)))
-
 (define (entity-ref key entity-set)
   (hash-get-handle (car entity-set) key))
 
-(define (new-entity . new-components)
-  (lambda (entity-set)
-    (let ((key (gensym))
-         (nc (normalize-components new-components)))
-      (hash-set! (car entity-set) key nc)
-      (register-components key (component-names nc) (cdr entity-set))
-      (values
-       entity-set
-       (cons key nc)))))
+(define (new-entity entity-set . new-components)
+  (receive (new-set new-keys) (bongodb:insert entity-set (normalize-components new-components))
+    (values new-set (car new-keys))))
+
+(define (get-entity entity-set key)
+  (let ((entity (bongodb:find entity-set (bongodb:$eq '_id key))))
+    (and entity (car entity))))
 
 (define (remove-entity key)
   (lambda (entity-set)
        (else
         (modify-entities ((car changes) entity-set) (cdr changes)))))
 
-(export make-entity-set
-       entity-list
-       entity-count
-       new-entity
-       remove-entity
-       set-entity
-       set-entity-components
-       remove-entity-components
-       modify-entities)
-
 
 ;;; Making systems
 
            (else
             (run-wait (cdr thd) (cons (join-thread (car thd)) res)))))))
 
-(export entities-changes
-       entities-changes?
-       get-entities-changes
-       find-entities-by-components
-       define-system
-       make-system
-       join-systems
-       thread-systems)
-
 
 ;;; Entities and components access inside systems
 
 
 (define (get-component component-name entity)
   (assoc-ref (cdr entity) component-name))
-
-(export get-key
-       get-component)