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 (make-entity-set . changes)
88 (cons (make-hash-table) (make-hash-table))
91 (define (entity-list entity-set)
92 (hash-map->list (lambda (k v) (cons k v)) (car entity-set)))
94 (define (entity-count entity-set)
95 (hash-count (const #t) (car entity-set)))
97 (define (normalize-components components)
101 `(,(get-component-type c) . ,c)
105 (define (register-components entity components clist)
106 (cond ((null? components) clist)
108 (let* ((type (car components))
109 (elist (hash-ref clist type)))
110 (hash-set! clist type
112 (lset-adjoin eq? elist entity))
115 (register-components entity (cdr components) clist)))))
117 (define (unregister-components entity components clist)
118 (cond ((null? components) clist)
120 (let* ((type (car components))
121 (elist (lset-difference eq? (hash-ref clist type) (list entity))))
123 (hash-remove! clist type))
125 (hash-set! clist type elist)))
126 (unregister-components entity (cdr components) clist)))))
128 (define (component-names components)
129 (map (lambda (c) (car c)) components))
131 (define (entity-component-names key entity-set)
133 (hash-ref (car entity-set) key)))
135 (define (entity-ref key entity-set)
136 (hash-get-handle (car entity-set) key))
138 (define (new-entity . new-components)
141 (nc (normalize-components new-components)))
142 (hash-set! (car entity-set) key nc)
143 (register-components key (component-names nc) (cdr entity-set))
148 (define (remove-entity key)
150 (let ((clist (entity-component-names key entity-set))
151 (entity (entity-ref key entity-set)))
152 (hash-remove! (car entity-set) key)
153 (unregister-components key clist (cdr entity-set))
158 (define (set-entity key . new-components)
160 (let* ((nc (normalize-components new-components))
161 (clist (entity-component-names key entity-set))
162 (nclist (component-names nc)))
163 (hash-set! (car entity-set) key nc)
164 (register-components key
165 (lset-difference eq? nclist clist)
166 (unregister-components key (lset-difference eq? clist nclist) (cdr entity-set)))
171 (define (set-entity-components key . new-components)
173 (let ((nc (normalize-components new-components))
174 (clist (alist-copy (hash-ref (car entity-set) key))))
177 (set! clist (assoc-set! clist (car c) (cdr c))))
179 (hash-set! (car entity-set) key clist)
180 (register-components key (component-names nc) (cdr entity-set))
185 (define (remove-entity-components key . old-components)
187 (let ((clist (alist-copy (hash-ref (car entity-set) key))))
190 (set! clist (assoc-remove! clist c)))
192 (hash-set! (car entity-set) key clist)
193 (unregister-components key old-components (cdr entity-set))
198 (define (modify-entities entity-set changes)
199 (cond ((null? changes)
202 (modify-entities ((car changes) entity-set) (cdr changes)))))
204 (export make-entity-set
210 set-entity-components
211 remove-entity-components
217 (define-record-type entities-changes-type
218 (entities-changes changes)
220 (changes get-entities-changes))
222 (define (append-changes changes)
225 (map get-entities-changes changes))))
227 (define (find-entities-by-components entity-set clist)
228 (cond ((null? clist) '())
230 (let ((e (hash-ref (cdr entity-set) (car clist) '()))
231 (e* (find-entities-by-components entity-set (cdr clist))))
234 (lset-intersection eq? e e*))))))
236 (define-syntax make-system
238 ((_ ((name (component-type ...)) ...) form ...)
240 (let ((name (map (lambda (x)
243 (memq (car x) '(component-type ...)))
246 (entity-ref x entity-set))
247 (find-entities-by-components entity-set '(component-type ...)))))
252 (define-syntax define-system
254 ((_ system-name ((name (component-type ...)) ...) form ...)
256 (make-system ((name (component-type ...)) ...)
260 (define (composed-systems-result results)
261 (let ((changes (filter (lambda (r) (entities-changes? r)) results)))
262 (cond ((null? changes)
265 (append-changes changes)))))
267 (define (join-systems . systems)
269 (let run ((s systems) (res '()))
271 (composed-systems-result res))
273 (run (cdr s) (cons ((car s) entity-set) res)))))))
275 (define (thread-systems . systems)
279 (call-with-new-thread
280 (lambda () (s entity-set))))
284 (composed-systems-result res))
286 (run-wait (cdr thd) (cons (join-thread (car thd)) res)))))))
288 (export entities-changes
291 find-entities-by-components
298 ;;; Entities and components access inside systems
300 (define (get-key entity)
303 (define (get-component component-name entity)
304 (assoc-ref (cdr entity) component-name))