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