From: Javier Sancho Date: Tue, 14 Jan 2014 17:58:39 +0000 (+0100) Subject: New way for returning results from systems X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=405b60cfd27f00e8dda02e19abe09bf7990bfbc2;p=gacela.git New way for returning results from systems * src/system: New type entities-changes that encapsulates entities modifications. A system that only does I/O operations doesn't must to return an entities-changes object. * src/examples/composing-systems.scm: Updated example * src/examples/making-systems.scm: Updated example --- diff --git a/src/examples/composing-systems.scm b/src/examples/composing-systems.scm index 7742b81..ef6e1e0 100644 --- a/src/examples/composing-systems.scm +++ b/src/examples/composing-systems.scm @@ -22,17 +22,17 @@ (define-system s1 ((with-l (l))) (sleep 3) - (map - (lambda (e) - (set-entity-components (get-key e) `(l . ,(cons 1 (get-component 'l e))))) - with-l)) + (entities-changes + (map (lambda (e) + (set-entity-components (get-key e) `(l . ,(cons 1 (get-component 'l e))))) + with-l))) (define-system s2 ((with-l (l))) (sleep 4) - (map - (lambda (e) - (set-entity-components (get-key e) `(l . ,(cons 2 (get-component 'l e))))) - with-l)) + (entities-changes + (map (lambda (e) + (set-entity-components (get-key e) `(l . ,(cons 2 (get-component 'l e))))) + with-l))) (define (composing-with-join) (let ((entities '()) diff --git a/src/examples/making-systems.scm b/src/examples/making-systems.scm index c049a90..545b7e7 100644 --- a/src/examples/making-systems.scm +++ b/src/examples/making-systems.scm @@ -23,15 +23,15 @@ (define-component a x y) (define-system s1 () - (list (new-entity (make-a 1 2)) - (new-entity (make-a 10 20)))) + (entities-changes + (list (new-entity (make-a 1 2)) + (new-entity (make-a 10 20))))) (define-system s2 ((with-a (a))) (for-each (lambda (e) (format #t "Key: ~a Component: ~a~%" (get-key e) (get-component 'a e))) - with-a) - '()) + with-a)) (define (making-systems) (let ((entities '()) diff --git a/src/system.scm b/src/system.scm index 9654cff..5a70cb5 100644 --- a/src/system.scm +++ b/src/system.scm @@ -187,6 +187,11 @@ ;;; Making systems +(define-record-type entities-changes-type + (entities-changes changes) + entities-changes? + (changes get-entities-changes)) + (define* (find-entities-by-components c t) (cond ((null? t) '()) (else @@ -213,7 +218,7 @@ (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))))))))) + (modify-entities (if (entities-changes? res) (get-entities-changes res) '()) e c))))))))) (define-syntax define-system (syntax-rules () @@ -274,7 +279,10 @@ (else (apply join-systems systems)))) -(export find-entities-by-components +(export entities-changes + entities-changes? + get-entities-changes + find-entities-by-components define-system make-system join-systems