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 (srfi srfi-1)
20 #:use-module (srfi srfi-9))
23 ;;; Component definitions
25 (define (symbol-concatenate . args)
28 (map (lambda (a) (if (symbol? a) (symbol->string a) a)) args))))
30 (define-macro (define-component name . args)
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
39 (format port "#<[~a]" ',name)
40 ,@(map (lambda (a) `(format port " ~a: ~a" ',a (,(symbol-concatenate name "-" a) record))) args)
44 (define (export-component component)
45 (let ((name (record-type-name component))
47 (module-export! m (list
48 (symbol-concatenate "make-" name)
49 (symbol-concatenate name "?")))
52 (module-export! (current-module)
54 (symbol-concatenate name "-" a)
55 (symbol-concatenate "set-" name "-" a "!"))))
56 (record-type-fields component))))
58 (define (get-component-type component)
59 (record-type-name (record-type-descriptor component)))
61 (export define-component
66 ;;; Entities and components
68 (define (new-entity new-components entities components)
71 (acons key (map (lambda (c) `(,(get-component-type c) . ,c)) new-components) entities)
72 (register-components key new-components components)
75 (define* (register-components entity components clist)
76 (cond ((null? components) clist)
78 (let* ((type (get-component-type (car components)))
79 (elist (assoc-ref clist type)))
80 (register-components entity (cdr components)
81 (assoc-set! clist type
83 (lset-adjoin eq? elist entity))
92 (define* (find-entities-by-components c t)
95 (let* ((e (assoc-ref c (car t)))
97 (cond ((null? (cdr t)) e*)
99 (lset-intersection eq? e* (find-entities-by-components c (cdr t)))))))))
102 (define (make-system component-types system-fun)
103 (lambda (entities components)
104 (let* ((e (find-entities-by-components components component-types))
105 (e* (map (lambda (x) (assoc x entities)) e))
106 (e** (map (lambda (x) (cons (car x) (filter (lambda (x) (memq (get-component-type x) component-types)) (cdr x)))) e*))
107 (res (system-fun e**)))
108 (lambda* (#:optional (entities2 #f) (components2 #f))
109 (let* ((e2 (if (and entities2 components2)
110 (find-entities-by-components components2 component-types)
112 (e2* (if (and entities2 components2)
113 (map (lambda (x) (assoc x entities2)) e2)
115 (e2** (if (and entities2 components2)
116 (map (lambda (x) (cons (car x) (filter (lambda (x) (memq (get-component-type x) component-types)) (cdr x)))) e2*)
120 ; ((1 a b) (2 a b c) (3 c))
123 ; ((1 a) (3 c) (4 a b))
125 (export find-entities-by-components