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))))
132 (assoc-remove! entities key)
133 (unregister-components key clist components)))))
135 (define (set-entity key . new-components)
136 (lambda (entities components)
137 (let* ((nc (normalize-components new-components))
138 (clist (map (lambda (c) (car c)) (assoc-ref entities key)))
139 (nclist (map (lambda (c) (car c)) nc)))
141 (assoc-set! entities key nc)
142 (register-components key (lset-difference eq? nclist clist)
143 (unregister-components key (lset-difference eq? clist nclist) components))))))
145 (define (set-entity-components key . new-components)
146 (lambda (entities components)
147 (let ((nc (normalize-components new-components))
148 (clist (alist-copy (assoc-ref entities key))))
151 (assoc-set! clist (car c) (cdr c)))
154 (assoc-set! entities key clist)
155 (register-components key (map (lambda (c) (car c)) nc) components)))))
157 (define (remove-entity-components key . old-components)
158 (lambda (entities components)
159 (let ((clist (alist-copy (assoc-ref entities key))))
162 (assoc-remove! clist c))
165 (assoc-set! entities key clist)
166 (unregister-components key old-components components)))))
168 (define (modify-entities changes entities components)
169 (cond ((null? changes)
170 (values entities components))
172 (receive (e c) ((car changes) entities components)
173 (modify-entities (cdr changes) e c)))))
178 set-entity-components
179 remove-entity-components
185 (define* (find-entities-by-components c t)
186 (cond ((null? t) '())
188 (let* ((e (assoc-ref c (car t)))
190 (cond ((null? (cdr t)) e*)
192 (lset-intersection eq? e* (find-entities-by-components c (cdr t)))))))))
194 (define-syntax make-system
196 ((_ ((name (component-type ...)) ...) form ...)
197 (lambda (entities components)
198 (let ((name (map (lambda (x)
201 (memq (car x) '(component-type ...)))
205 (find-entities-by-components components '(component-type ...)))))
207 (let ((res (begin form ...)))
208 (lambda* (#:optional (entities2 #f) (components2 #f))
209 (let ((e (if (and entities2 components2) entities2 entities))
210 (c (if (and entities2 components2) components2 components)))
211 (modify-entities res e c)))))))))
213 (define-syntax define-system
215 ((_ system-name ((name (component-type ...)) ...) form ...)
217 (make-system ((name (component-type ...)) ...)
221 (define (join-systems . systems)
222 (lambda (entities components)
223 (let run ((s systems) (e entities) (c components))
227 (receive (e2 c2) (((car s) e c))
228 (run (cdr s) e2 c2)))))))
230 (define (threaded-systems . systems)
231 (lambda (entities components)
235 (call-with-new-thread
236 (lambda () (s entities components))))
238 (e entities) (c components))
242 (receive (e2 c2) ((join-thread (car thd)) e c)
243 (run-wait (cdr thd) e2 c2)))))))
245 (export find-entities-by-components
252 ;;; Entities and components access inside systems
254 (define (get-key entity)
257 (define (get-component component-name entity)
258 (assoc-ref (cdr entity) component-name))