X-Git-Url: https://git.jsancho.org/?p=gacela.git;a=blobdiff_plain;f=src%2Fsystem.scm;h=50e9534b451f34c961150745e30bf49f433133ac;hp=d3566652bd0e8e995fe0fe684d4bd6dfdfc3aee1;hb=f3d35ed115ff03f513c93a05325885e44da10891;hpb=1b809e1a58ec2a94903b4b44101d706fd0f27c81 diff --git a/src/system.scm b/src/system.scm index d356665..50e9534 100644 --- a/src/system.scm +++ b/src/system.scm @@ -16,9 +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) + #: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 @@ -28,19 +53,44 @@ (string-concatenate (map (lambda (a) (if (symbol? a) (symbol->string a) a)) args)))) -(define-macro (define-component name . args) - `(begin - (use-modules (srfi srfi-9) (srfi srfi-9 gnu)) - (define-record-type ,name - (,(symbol-concatenate "make-" name) ,@args) - ,(symbol-concatenate name "?") - ,@(map (lambda (a) (list a (symbol-concatenate name "-" a) (symbol-concatenate "set-" name "-" a "!"))) args)) - (set-record-type-printer! ,name - (lambda (record port) - (format port "#<[~a]" ',name) - ,@(map (lambda (a) `(format port " ~a: ~a" ',a (,(symbol-concatenate name "-" a) record))) args) - (format port ">"))) - ',name)) +(define-syntax define-component + (lambda (x) + (define (concat . args) + (datum->syntax x + (apply symbol-concatenate + (map (lambda (a) + (if (string? a) + a + (syntax->datum a))) + args)))) + (define (filtered-args args) + (let ((datum (map (lambda (a) (syntax->datum a)) args))) + (map (lambda (a) (datum->syntax x a)) + (map (lambda (a) (if (list? a) (car a) a)) + (filter (lambda (a) (not (keyword? a))) datum))))) + (syntax-case x () + ((_ name field ...) + (with-syntax ((make-name (concat "make-" #'name)) + (make-name-record (concat "make-" #'name "-record")) + (name? (concat #'name "?")) + ((field-name ...) (filtered-args #'(field ...))) + ((field-getter ...) (map (lambda (f) (concat #'name "-" f)) (filtered-args #'(field ...)))) + ((field-setter ...) (map (lambda (f) (concat "set-" #'name "-" f "!")) (filtered-args #'(field ...))))) + #'(begin + (define* (make-name field ...) + (make-name-record field-name ...)) + (define-record-type name + (make-name-record field-name ...) + name? + (field-name field-getter field-setter) + ...) + (set-record-type-printer! name + (lambda (record port) + (format port "#<[~a]" 'name) + (format port " ~a: ~a" 'field-name (field-getter record)) + ... + (format port ">"))) + 'name)))))) (define (export-component component) (let ((name (record-type-name component)) @@ -59,116 +109,172 @@ (define (get-component-type component) (record-type-name (record-type-descriptor component))) -(export define-component - export-component - get-component-type) - ;;; Entities and components -(define (register-components entity components clist) - (cond ((null? components) clist) - (else - (let* ((type (car components)) - (elist (assoc-ref clist type))) - (register-components entity (cdr components) - (assoc-set! clist type - (cond (elist - (lset-adjoin eq? elist entity)) - (else - (list entity))))))))) - -(define (unregister-components entity components clist) - (cond ((null? components) clist) - (else - (let* ((type (car components)) - (elist (lset-difference eq? (assoc-ref clist type) (list entity)))) - (unregister-components entity (cdr components) - (cond ((null? elist) - (assoc-remove! clist type)) - (else - (assoc-set! clist type elist)))))))) - -(define (new-entity new-components entities components) - (let ((key (gensym))) - (values - (acons key new-components entities) - (register-components key - (map (lambda (c) (car c)) new-components) - components) - key))) - -(define (remove-entity key entities components) - (let ((clist (map (lambda (c) (car c)) (assoc-ref entities key)))) - (values - (assoc-remove! entities 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))) - (values - (assoc-set! entities key new-components) - (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)) new-components) entities components)) - -(define (set-entities new-entities entities components) - (cond ((null? new-entities) - (values entities components)) +(define (make-entity-set . changes) + (modify-entities (bongodb:make-collection) changes)) + +(define (entity-list entity-set) + (bongodb:find entity-set)) + +(define (entity-count entity-set) + (bongodb:count entity-set)) + +(define (normalize-components components) + (map + (lambda (c) + (if (record? c) + `(,(get-component-type c) . ,c) + c)) + components)) + +(define (entity-ref key entity-set) + (hash-get-handle (car entity-set) key)) + +(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) + (let ((clist (entity-component-names key entity-set)) + (entity (entity-ref key entity-set))) + (hash-remove! (car entity-set) key) + (unregister-components key clist (cdr entity-set)) + (values + entity-set + entity)))) + +(define (set-entity key . new-components) + (lambda (entity-set) + (let* ((nc (normalize-components new-components)) + (clist (entity-component-names key entity-set)) + (nclist (component-names nc))) + (hash-set! (car entity-set) key nc) + (register-components key + (lset-difference eq? nclist clist) + (unregister-components key (lset-difference eq? clist nclist) (cdr entity-set))) + (values + entity-set + (cons key nc))))) + +(define (set-entity-components key . new-components) + (lambda (entity-set) + (let ((nc (normalize-components new-components)) + (clist (alist-copy (hash-ref (car entity-set) key)))) + (for-each + (lambda (c) + (set! clist (assoc-set! clist (car c) (cdr c)))) + nc) + (hash-set! (car entity-set) key clist) + (register-components key (component-names nc) (cdr entity-set)) + (values + entity-set + (cons key clist))))) + +(define (remove-entity-components key . old-components) + (lambda (entity-set) + (let ((clist (alist-copy (hash-ref (car entity-set) key)))) + (for-each + (lambda (c) + (set! clist (assoc-remove! clist c))) + old-components) + (hash-set! (car entity-set) key clist) + (unregister-components key old-components (cdr entity-set)) + (values + entity-set + (cons key clist))))) + +(define (modify-entities entity-set changes) + (cond ((null? changes) + entity-set) (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 - set-entity - set-entity-components - set-entities) + (modify-entities ((car changes) entity-set) (cdr changes))))) ;;; Making systems -(define* (find-entities-by-components c t) - (cond ((null? t) '()) +(define-record-type entities-changes-type + (entities-changes changes) + entities-changes? + (changes get-entities-changes)) + +(define (append-changes changes) + (entities-changes + (apply append + (map get-entities-changes changes)))) + +(define (find-entities-by-components entity-set clist) + (cond ((null? clist) '()) (else - (let* ((e (assoc-ref c (car t))) - (e* (if e e '()))) - (cond ((null? (cdr t)) e*) - (else - (lset-intersection eq? e* (find-entities-by-components c (cdr t))))))))) - - -(define (make-system component-types system-fun) - (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 (car x) component-types)) (cdr x)))) e*)) - (res (system-fun e**))) - (lambda* (#:optional (entities2 #f) (components2 #f)) - (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) + (let ((e (hash-ref (cdr entity-set) (car clist) '())) + (e* (find-entities-by-components entity-set (cdr clist)))) + (if (null? e*) + e + (lset-intersection eq? e e*)))))) + +(define-syntax make-system + (syntax-rules () + ((_ ((name (component-type ...)) ...) form ...) + (lambda (entity-set) + (let ((name (map (lambda (x) + (cons (car x) + (filter (lambda (x) + (memq (car x) '(component-type ...))) + (cdr x)))) + (map (lambda (x) + (entity-ref x entity-set)) + (find-entities-by-components entity-set '(component-type ...))))) + ...) + form + ...))))) + +(define-syntax define-system + (syntax-rules () + ((_ system-name ((name (component-type ...)) ...) form ...) + (define system-name + (make-system ((name (component-type ...)) ...) + form + ...))))) + +(define (composed-systems-result results) + (let ((changes (filter (lambda (r) (entities-changes? r)) results))) + (cond ((null? changes) + (car results)) + (else + (append-changes changes))))) + +(define (join-systems . systems) + (lambda (entity-set) + (let run ((s systems) (res '())) + (cond ((null? s) + (composed-systems-result res)) + (else + (run (cdr s) (cons ((car s) entity-set) res))))))) + +(define (thread-systems . systems) + (lambda (entity-set) + (let run-wait ((thd + (map (lambda (s) + (call-with-new-thread + (lambda () (s entity-set)))) + systems)) + (res '())) + (cond ((null? thd) + (composed-systems-result res)) + (else + (run-wait (cdr thd) (cons (join-thread (car thd)) res))))))) + + +;;; Entities and components access inside systems + +(define (get-key entity) + (car entity)) + +(define (get-component component-name entity) + (assoc-ref (cdr entity) component-name))