X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=src%2Fsystem.scm;h=e6feb4eb0e18d8a0700c386772eecee5c31c826d;hb=b1ade28aa0eab723292491d20d5841e4cb8da37c;hp=915e51e88ee71f27288e4181f011133e6131b4b7;hpb=01f67014fa380838c1db204b29ec83451dc8a03c;p=gacela.git diff --git a/src/system.scm b/src/system.scm index 915e51e..e6feb4e 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,83 +83,225 @@ ;;; Entities and components +(define (make-entity-set . changes) + (modify-entities + (cons (make-hash-table) (make-hash-table)) + changes)) + +(define (entity-list entity-set) + (hash-map->list (lambda (k v) (cons k v)) (car entity-set))) + +(define (entity-count entity-set) + (hash-count (const #t) (car entity-set))) + +(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 (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))))))))) + (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? (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 (map (lambda (c) `(,(get-component-type c) . ,c)) new-components) entities) - (register-components - key - (map (lambda (c) (get-component-type 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)))) - -(export new-entity - remove-entity) + (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 (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 + (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 -(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 (get-component-type x) component-types)) (cdr x)))) e*)) - (res (system-fun e**))) - (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)) - -(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))))))) + +(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-key entity) + (car entity)) + +(define (get-component component-name entity) + (assoc-ref (cdr entity) component-name)) + +(export get-key + get-component)