X-Git-Url: https://git.jsancho.org/?p=gacela.git;a=blobdiff_plain;f=src%2Fsystem.scm;h=50e9534b451f34c961150745e30bf49f433133ac;hp=1df24f9ba03d711b4ba6f7097e83aa273aded760;hb=f3d35ed115ff03f513c93a05325885e44da10891;hpb=253656b7188f5d1f3684a640530f248d29995eef diff --git a/src/system.scm b/src/system.scm index 1df24f9..50e9534 100644 --- a/src/system.scm +++ b/src/system.scm @@ -16,10 +16,34 @@ (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 @@ -85,23 +109,17 @@ (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 @@ -111,48 +129,16 @@ 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) @@ -210,16 +196,6 @@ (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 @@ -294,15 +270,6 @@ (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 @@ -311,6 +278,3 @@ (define (get-component component-name entity) (assoc-ref (cdr entity) component-name)) - -(export get-key - get-component)