1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2013 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 (define-module (gacela system)
19 #:use-module (ice-9 receive)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-9 gnu))
25 ;;; Component definitions
27 (define (symbol-concatenate . args)
30 (map (lambda (a) (if (symbol? a) (symbol->string a) a)) args))))
32 (define-syntax define-component
34 (define (concat . args)
36 (apply symbol-concatenate
44 (with-syntax ((make-name (concat "make-" #'name))
45 (name? (concat #'name "?"))
46 ((field-getter ...) (map (lambda (f) (concat #'name "-" f)) #'(field ...)))
47 ((field-setter ...) (map (lambda (f) (concat "set-" #'name "-" f "!")) #'(field ...))))
49 (define-record-type name
52 (field field-getter field-setter)
54 (set-record-type-printer! name
56 (format port "#<[~a]" 'name)
57 (format port " ~a: ~a" 'field (field-getter record))
62 (define (export-component component)
63 (let ((name (record-type-name component))
65 (module-export! m (list
66 (symbol-concatenate "make-" name)
67 (symbol-concatenate name "?")))
70 (module-export! (current-module)
72 (symbol-concatenate name "-" a)
73 (symbol-concatenate "set-" name "-" a "!"))))
74 (record-type-fields component))))
76 (define (get-component-type component)
77 (record-type-name (record-type-descriptor component)))
79 (export define-component
84 ;;; Entities and components
86 (define (normalize-components components)
90 `(,(get-component-type c) . ,c)
94 (define (register-components entity components clist)
95 (cond ((null? components) clist)
97 (let* ((type (car components))
98 (elist (assoc-ref clist type)))
99 (register-components entity (cdr components)
100 (assoc-set! clist type
102 (lset-adjoin eq? elist entity))
104 (list entity)))))))))
106 (define (unregister-components entity components clist)
107 (cond ((null? components) clist)
109 (let* ((type (car components))
110 (elist (lset-difference eq? (assoc-ref clist type) (list entity))))
111 (unregister-components entity (cdr components)
113 (assoc-remove! clist type))
115 (assoc-set! clist type elist))))))))
117 (define (new-entity . new-components)
118 (lambda (entities components)
120 (nc (normalize-components new-components)))
122 (acons key nc entities)
123 (register-components key
124 (map (lambda (c) (car c)) nc)
128 (define (remove-entity key)
129 (lambda (entities components)
130 (let ((clist (map (lambda (c) (car c)) (assoc-ref entities key)))
131 (entity (assoc key entities)))
133 (assoc-remove! entities key)
134 (unregister-components key clist components)
137 (define (set-entity key . new-components)
138 (lambda (entities components)
139 (let* ((nc (normalize-components new-components))
140 (clist (map (lambda (c) (car c)) (assoc-ref entities key)))
141 (nclist (map (lambda (c) (car c)) nc)))
143 (assoc-set! entities key nc)
144 (register-components key (lset-difference eq? nclist clist)
145 (unregister-components key (lset-difference eq? clist nclist) components))
148 (define (set-entity-components key . new-components)
149 (lambda (entities components)
150 (let ((nc (normalize-components new-components))
151 (clist (alist-copy (assoc-ref entities key))))
154 (assoc-set! clist (car c) (cdr c)))
157 (assoc-set! entities key clist)
158 (register-components key (map (lambda (c) (car c)) nc) components)
161 (define (remove-entity-components key . old-components)
162 (lambda (entities components)
163 (let ((clist (alist-copy (assoc-ref entities key))))
166 (assoc-remove! clist c))
169 (assoc-set! entities key clist)
170 (unregister-components key old-components components)
173 (define (modify-entities changes entities components)
174 (cond ((null? changes)
175 (values entities components))
177 (receive (e c) ((car changes) entities components)
178 (modify-entities (cdr changes) e c)))))
183 set-entity-components
184 remove-entity-components
190 (define-record-type entities-changes-type
191 (entities-changes changes)
193 (changes get-entities-changes))
195 (define* (find-entities-by-components c t)
196 (cond ((null? t) '())
198 (let* ((e (assoc-ref c (car t)))
200 (cond ((null? (cdr t)) e*)
202 (lset-intersection eq? e* (find-entities-by-components c (cdr t)))))))))
204 (define-syntax make-system
206 ((_ ((name (component-type ...)) ...) form ...)
207 (lambda (entities components)
208 (let ((name (map (lambda (x)
211 (memq (car x) '(component-type ...)))
215 (find-entities-by-components components '(component-type ...)))))
217 (let ((res (begin form ...)))
218 (lambda* (#:optional (entities2 #f) (components2 #f))
219 (let ((e (if (and entities2 components2) entities2 entities))
220 (c (if (and entities2 components2) components2 components)))
221 (modify-entities (if (entities-changes? res) (get-entities-changes res) '()) e c)))))))))
223 (define-syntax define-system
225 ((_ system-name ((name (component-type ...)) ...) form ...)
227 (make-system ((name (component-type ...)) ...)
231 (define (join-systems . systems)
232 (lambda (entities components)
234 (let run ((s systems) (e (alist-copy entities)) (c (alist-copy components)) (res '()))
238 (let ((r ((car s) e c)))
240 (run (cdr s) e2 c2 (cons r res)))))))))
241 (lambda* (#:optional (entities2 #f) (components2 #f))
242 (let modify ((e (if (and entities2 components2) entities2 entities))
243 (c (if (and entities2 components2) components2 components))
244 (ch (reverse changes)))
248 (receive (e2 c2) ((car ch) e c)
249 (modify e2 c2 (cdr ch))))))))))
251 (define (threaded-systems . systems)
252 (lambda (entities components)
256 (call-with-new-thread
257 (lambda () (s entities components))))
263 (run-wait (cdr thd) (cons (join-thread (car thd)) res)))))))
264 (lambda* (#:optional (entities2 #f) (components2 #f))
265 (let modify ((e (if (and entities2 components2) entities2 entities))
266 (c (if (and entities2 components2) components2 components))
271 (receive (e2 c2) ((car ch) e c)
272 (modify e2 c2 (cdr ch))))))))))
274 (define (group-systems . systems)
275 (cond ((null? systems)
277 ((= (length systems) 1)
280 (apply join-systems systems))))
282 (export entities-changes
285 find-entities-by-components
293 ;;; Entities and components access inside systems
295 (define (get-key entity)
298 (define (get-component component-name entity)
299 (assoc-ref (cdr entity) component-name))