]> git.jsancho.org Git - gacela.git/blob - src/system.scm
d3566652bd0e8e995fe0fe684d4bd6dfdfc3aee1
[gacela.git] / src / system.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2013 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
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.
8 ;;;
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.
13 ;;;
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/>.
16
17
18 (define-module (gacela system)
19   #:use-module (ice-9 receive)
20   #:use-module (srfi srfi-1)
21   #:use-module (srfi srfi-9))
22
23
24 ;;; Component definitions
25
26 (define (symbol-concatenate . args)
27   (string->symbol
28    (string-concatenate
29     (map (lambda (a) (if (symbol? a) (symbol->string a) a)) args))))
30
31 (define-macro (define-component name . args)
32   `(begin
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
39        (lambda (record port)
40          (format port "#<[~a]" ',name)
41          ,@(map (lambda (a) `(format port " ~a: ~a" ',a (,(symbol-concatenate name "-" a) record))) args)
42          (format port ">")))
43      ',name))
44
45 (define (export-component component)
46   (let ((name (record-type-name component))
47         (m (current-module)))
48     (module-export! m (list
49                        (symbol-concatenate "make-" name)
50                        (symbol-concatenate name "?")))
51     (for-each
52      (lambda (a)
53        (module-export! (current-module)
54                        (list
55                         (symbol-concatenate name "-" a)
56                         (symbol-concatenate "set-" name "-" a "!"))))
57      (record-type-fields component))))
58
59 (define (get-component-type component)
60   (record-type-name (record-type-descriptor component)))
61
62 (export define-component
63         export-component
64         get-component-type)
65
66
67 ;;; Entities and components
68
69 (define (register-components entity components clist)
70   (cond ((null? components) clist)
71         (else
72          (let* ((type (car components))
73                 (elist (assoc-ref clist type)))
74            (register-components entity (cdr components)
75              (assoc-set! clist type
76                (cond (elist
77                       (lset-adjoin eq? elist entity))
78                      (else
79                       (list entity)))))))))
80
81 (define (unregister-components entity components clist)
82   (cond ((null? components) clist)
83         (else
84          (let* ((type (car components))
85                 (elist (lset-difference eq? (assoc-ref clist type) (list entity))))
86            (unregister-components entity (cdr components)
87              (cond ((null? elist)
88                     (assoc-remove! clist type))
89                    (else
90                     (assoc-set! clist type elist))))))))
91
92 (define (new-entity new-components entities components)
93   (let ((key (gensym)))
94     (values
95      (acons key new-components entities)
96      (register-components key
97                           (map (lambda (c) (car c)) new-components)
98                           components)
99      key)))
100
101 (define (remove-entity key entities components)
102   (let ((clist (map (lambda (c) (car c)) (assoc-ref entities key))))
103     (values
104      (assoc-remove! entities key)
105      (unregister-components key clist components))))
106
107 (define (set-entity key new-components entities components)
108   (let ((clist (map (lambda (c) (car c)) (assoc-ref entities key)))
109         (nclist (map (lambda (c) (car c)) new-components)))
110     (values
111      (assoc-set! entities key new-components)
112      (register-components key (lset-difference eq? nclist clist)
113                           (unregister-components key (lset-difference eq? clist nclist) components)))))
114
115 (define (set-entity-components key new-components entities components)
116   (define (set-components clist new-components)
117     (cond ((null? new-components)
118            clist)
119           (else
120            (set-components
121             (if (cdar new-components)
122                 (assoc-set! clist (caar new-components) (cdar new-components))
123                 (assoc-remove! clist (caar new-components)))
124             (cdr new-components)))))
125   (set-entity key (set-components (alist-copy (assoc-ref entities key)) new-components) entities components))
126
127 (define (set-entities new-entities entities components)
128   (cond ((null? new-entities)
129          (values entities components))
130         (else
131          (cond ((not (caar new-entities))
132                 (receive (e c k) (new-entity (cdar new-entities) entities components)
133                          (set-entities (cdr new-entities) e c)))
134                ((not (cdar new-entities))
135                 (receive (e c) (remove-entity (caar new-entities) entities components)
136                          (set-entities (cdr new-entities) e c)))
137                (else
138                 (receive (e c) (set-entity-components (caar new-entities) (cdar new-entities) entities components)
139                          (set-entities (cdr new-entities) e c)))))))
140
141    
142 (export new-entity
143         remove-entity
144         set-entity
145         set-entity-components
146         set-entities)
147
148
149 ;;; Making systems
150
151 (define* (find-entities-by-components c t)
152   (cond ((null? t) '())
153         (else
154          (let* ((e (assoc-ref c (car t)))
155                 (e* (if e e '())))
156            (cond ((null? (cdr t)) e*)
157                  (else
158                   (lset-intersection eq? e* (find-entities-by-components c (cdr t)))))))))
159                   
160
161 (define (make-system component-types system-fun)
162   (lambda (entities components)
163     (let* ((e (find-entities-by-components components component-types))
164            (e* (map (lambda (x) (assoc x entities)) e))
165            (e** (map (lambda (x) (cons (car x) (filter (lambda (x) (memq (car x) component-types)) (cdr x)))) e*))
166            (res (system-fun e**)))
167       (lambda* (#:optional (entities2 #f) (components2 #f))
168         (let ((e (if (and entities2 components2) entities2 entities))
169               (c (if (and entities2 components2) components2 components)))
170           (set-entities res e c))))))
171
172
173 (export find-entities-by-components
174         make-system)