X-Git-Url: https://git.jsancho.org/?p=gacela.git;a=blobdiff_plain;f=src%2Fsystem.scm;h=26e31c6f52dc66b419341a75b62d226c9c33426c;hp=3c8454821b733e432dcef33fb9efb8d900bd4d10;hb=9ab0fd2802207cff18dd474ec5504f580b5d9856;hpb=0f49b8fac5821694a2db6f88b95423e5ad8aa719 diff --git a/src/system.scm b/src/system.scm index 3c84548..26e31c6 100644 --- a/src/system.scm +++ b/src/system.scm @@ -18,7 +18,8 @@ (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 @@ -28,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)) @@ -97,63 +114,70 @@ (else (assoc-set! clist type elist)))))))) -(define (new-entity new-components 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) - 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* ((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) +(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) + key)))) + +(define (remove-entity key) + (lambda (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) + (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)))))) + +(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))))) + +(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))))) + +(define (modify-entities changes entities components) + (cond ((null? changes) (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))))))) - - + (receive (e c) ((car changes) entities components) + (modify-entities (cdr changes) e c))))) + (export new-entity remove-entity set-entity set-entity-components - set-entities) + remove-entity-components + modify-entities) ;;; Making systems @@ -166,19 +190,24 @@ (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)))))) +(define-syntax make-system + (syntax-rules () + ((_ component-types system-func) + (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-func e**))) + (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 () + ((_ (name . component-types) system-func) + (define name (make-system component-types system-func))))) (define (join-systems . systems) (lambda (entities components) @@ -189,7 +218,6 @@ (receive (e2 c2) (((car s) e c)) (run (cdr s) e2 c2))))))) - (define (threaded-systems . systems) (lambda (entities components) (let run-wait ((thd @@ -205,8 +233,20 @@ (receive (e2 c2) ((join-thread (car thd)) e c) (run-wait (cdr thd) e2 c2))))))) - (export find-entities-by-components + define-system make-system join-systems threaded-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)