X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=src%2Fsystem.scm;h=9654cffbafdb4a059aedfc3f3ef33fc58a10b1b2;hb=a9563e6e38b3a5e8e6ca92607570a92d63237793;hp=ea9476ab51bbd226286865323b095f7d4e688163;hpb=f2cf55b8c51e819bded19f2693aa74ae89abb12b;p=gacela.git diff --git a/src/system.scm b/src/system.scm index ea9476a..9654cff 100644 --- a/src/system.scm +++ b/src/system.scm @@ -16,8 +16,10 @@ (define-module (gacela system) + #: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)) ;;; Component definitions @@ -27,19 +29,35 @@ (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)))) + (syntax-case x () + ((_ name field ...) + (with-syntax ((make-name (concat "make-" #'name)) + (name? (concat #'name "?")) + ((field-getter ...) (map (lambda (f) (concat #'name "-" f)) #'(field ...))) + ((field-setter ...) (map (lambda (f) (concat "set-" #'name "-" f "!")) #'(field ...)))) + #'(begin + (define-record-type name + (make-name field ...) + name? + (field field-getter field-setter) + ...) + (set-record-type-printer! name + (lambda (record port) + (format port "#<[~a]" 'name) + (format port " ~a: ~a" 'field (field-getter record)) + ... + (format port ">"))) + 'name)))))) (define (export-component component) (let ((name (record-type-name component)) @@ -65,6 +83,14 @@ ;;; 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 @@ -88,46 +114,75 @@ (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 (new-entity . new-components) + (lambda (entities components) + (let ((key (gensym)) + (nc (normalize-components new-components))) + (values + (acons key nc entities) + (register-components key + (map (lambda (c) (car c)) nc) + components) + (cons key nc))))) + +(define (remove-entity key) + (lambda (entities components) + (let ((clist (map (lambda (c) (car c)) (assoc-ref entities key))) + (entity (assoc key entities))) + (values + (assoc-remove! entities key) + (unregister-components key clist components) + entity)))) + +(define (set-entity key . new-components) + (lambda (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)) + (cons key nc))))) + +(define (set-entity-components key . new-components) + (lambda (entities components) + (let ((nc (normalize-components new-components)) + (clist (alist-copy (assoc-ref entities key)))) + (for-each + (lambda (c) + (assoc-set! clist (car c) (cdr c))) + nc) + (values + (assoc-set! entities key clist) + (register-components key (map (lambda (c) (car c)) nc) components) + (cons key clist))))) + +(define (remove-entity-components key . old-components) + (lambda (entities components) + (let ((clist (alist-copy (assoc-ref entities key)))) + (for-each + (lambda (c) + (assoc-remove! clist c)) + old-components) + (values + (assoc-set! entities key clist) + (unregister-components key old-components components) + (cons key clist))))) + +(define (modify-entities changes entities components) + (cond ((null? changes) + (values entities components)) + (else + (receive (e c) ((car changes) entities components) + (modify-entities (cdr changes) e c))))) + (export new-entity remove-entity set-entity - set-entity-components) + set-entity-components + remove-entity-components + modify-entities) ;;; Making systems @@ -140,30 +195,100 @@ (cond ((null? (cdr t)) e*) (else (lset-intersection eq? e* (find-entities-by-components c (cdr t))))))))) - -(define (make-system component-types system-fun) +(define-syntax make-system + (syntax-rules () + ((_ ((name (component-type ...)) ...) form ...) + (lambda (entities components) + (let ((name (map (lambda (x) + (cons (car x) + (filter (lambda (x) + (memq (car x) '(component-type ...))) + (cdr x)))) + (map (lambda (x) + (assoc x entities)) + (find-entities-by-components components '(component-type ...))))) + ...) + (let ((res (begin form ...))) + (lambda* (#:optional (entities2 #f) (components2 #f)) + (let ((e (if (and entities2 components2) entities2 entities)) + (c (if (and entities2 components2) components2 components))) + (modify-entities res e c))))))))) + +(define-syntax define-system + (syntax-rules () + ((_ system-name ((name (component-type ...)) ...) form ...) + (define system-name + (make-system ((name (component-type ...)) ...) + form + ...))))) + +(define (join-systems . 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*)) - (res (system-fun e**))) + (let ((changes + (let run ((s systems) (e (alist-copy entities)) (c (alist-copy components)) (res '())) + (cond ((null? s) + res) + (else + (let ((r ((car s) e c))) + (receive (e2 c2) (r) + (run (cdr s) e2 c2 (cons r res))))))))) (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 modify ((e (if (and entities2 components2) entities2 entities)) + (c (if (and entities2 components2) components2 components)) + (ch (reverse changes))) + (cond ((null? ch) + (values e c)) + (else + (receive (e2 c2) ((car ch) e c) + (modify e2 c2 (cdr ch)))))))))) + +(define (threaded-systems . systems) + (lambda (entities components) + (let ((changes + (let run-wait ((thd + (map (lambda (s) + (call-with-new-thread + (lambda () (s entities components)))) + systems)) + (res '())) + (cond ((null? thd) + res) + (else + (run-wait (cdr thd) (cons (join-thread (car thd)) res))))))) + (lambda* (#:optional (entities2 #f) (components2 #f)) + (let modify ((e (if (and entities2 components2) entities2 entities)) + (c (if (and entities2 components2) components2 components)) + (ch changes)) + (cond ((null? ch) + (values e c)) + (else + (receive (e2 c2) ((car ch) e c) + (modify e2 c2 (cdr ch)))))))))) + +(define (group-systems . systems) + (cond ((null? systems) + (make-system ())) + ((= (length systems) 1) + (car systems)) + (else + (apply join-systems systems)))) (export find-entities-by-components - make-system) + define-system + make-system + join-systems + threaded-systems + group-systems) + + +;;; Entities and components access inside systems + +(define (get-key entity) + (car entity)) + +(define (get-component component-name entity) + (assoc-ref (cdr entity) component-name)) + +(export get-key + get-component)