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))
24 ;;; Component definitions
26 (define (symbol-concatenate . args)
29 (map (lambda (a) (if (symbol? a) (symbol->string a) a)) args))))
31 (define-macro (define-component name . args)
33 (use-modules (srfi srfi-9) (srfi srfi-9 gnu))
34 (define-record-type ,name
35 (,(symbol-concatenate "make-" name) ,@args)
36 ,(symbol-concatenate name "?")
37 ,@(map (lambda (a) (list a (symbol-concatenate name "-" a) (symbol-concatenate "set-" name "-" a "!"))) args))
38 (set-record-type-printer! ,name
40 (format port "#<[~a]" ',name)
41 ,@(map (lambda (a) `(format port " ~a: ~a" ',a (,(symbol-concatenate name "-" a) record))) args)
45 (define (export-component component)
46 (let ((name (record-type-name component))
48 (module-export! m (list
49 (symbol-concatenate "make-" name)
50 (symbol-concatenate name "?")))
53 (module-export! (current-module)
55 (symbol-concatenate name "-" a)
56 (symbol-concatenate "set-" name "-" a "!"))))
57 (record-type-fields component))))
59 (define (get-component-type component)
60 (record-type-name (record-type-descriptor component)))
62 (export define-component
67 ;;; Entities and components
69 (define (normalize-components components)
73 `(,(get-component-type c) . ,c)
77 (define (register-components entity components clist)
78 (cond ((null? components) clist)
80 (let* ((type (car components))
81 (elist (assoc-ref clist type)))
82 (register-components entity (cdr components)
83 (assoc-set! clist type
85 (lset-adjoin eq? elist entity))
89 (define (unregister-components entity components clist)
90 (cond ((null? components) clist)
92 (let* ((type (car components))
93 (elist (lset-difference eq? (assoc-ref clist type) (list entity))))
94 (unregister-components entity (cdr components)
96 (assoc-remove! clist type))
98 (assoc-set! clist type elist))))))))
100 (define (new-entity new-components entities components)
102 (nc (normalize-components new-components)))
104 (acons key nc entities)
105 (register-components key
106 (map (lambda (c) (car c)) nc)
110 (define (remove-entity key entities components)
111 (let ((clist (map (lambda (c) (car c)) (assoc-ref entities key))))
113 (assoc-remove! entities key)
114 (unregister-components key clist components))))
116 (define (set-entity key new-components entities components)
117 (let* ((nc (normalize-components new-components))
118 (clist (map (lambda (c) (car c)) (assoc-ref entities key)))
119 (nclist (map (lambda (c) (car c)) nc)))
121 (assoc-set! entities key nc)
122 (register-components key (lset-difference eq? nclist clist)
123 (unregister-components key (lset-difference eq? clist nclist) components)))))
125 (define (set-entity-components key new-components entities components)
126 (define (set-components clist new-components)
127 (cond ((null? new-components)
131 (if (cdar new-components)
132 (assoc-set! clist (caar new-components) (cdar new-components))
133 (assoc-remove! clist (caar new-components)))
134 (cdr new-components)))))
135 (set-entity key (set-components (alist-copy (assoc-ref entities key)) (normalize-components new-components)) entities components))
137 (define (set-entities new-entities entities components)
138 (cond ((null? new-entities)
139 (values entities components))
141 (cond ((not (caar new-entities))
142 (receive (e c k) (new-entity (cdar new-entities) entities components)
143 (set-entities (cdr new-entities) e c)))
144 ((not (cdar new-entities))
145 (receive (e c) (remove-entity (caar new-entities) entities components)
146 (set-entities (cdr new-entities) e c)))
148 (receive (e c) (set-entity-components (caar new-entities) (cdar new-entities) entities components)
149 (set-entities (cdr new-entities) e c)))))))
155 set-entity-components
161 (define* (find-entities-by-components c t)
162 (cond ((null? t) '())
164 (let* ((e (assoc-ref c (car t)))
166 (cond ((null? (cdr t)) e*)
168 (lset-intersection eq? e* (find-entities-by-components c (cdr t)))))))))
171 (define (make-system component-types system-fun)
172 (lambda (entities components)
173 (let* ((e (find-entities-by-components components component-types))
174 (e* (map (lambda (x) (assoc x entities)) e))
175 (e** (map (lambda (x) (cons (car x) (filter (lambda (x) (memq (car x) component-types)) (cdr x)))) e*))
176 (res (system-fun e**)))
177 (lambda* (#:optional (entities2 #f) (components2 #f))
178 (let ((e (if (and entities2 components2) entities2 entities))
179 (c (if (and entities2 components2) components2 components)))
180 (set-entities res e c))))))
183 (define (join-systems . systems)
184 (lambda (entities components)
185 (let run ((s systems) (e entities) (c components))
189 (receive (e2 c2) (((car s) e c))
190 (run (cdr s) e2 c2)))))))
193 (define (threaded-systems . systems)
194 (lambda (entities components)
198 (call-with-new-thread
199 (lambda () (s entities components))))
201 (e entities) (c components))
205 (receive (e2 c2) ((join-thread (car thd)) e c)
206 (run-wait (cdr thd) e2 c2)))))))
209 (export find-entities-by-components