]> git.jsancho.org Git - gacela.git/blob - src/gacela.scm
First reactive example
[gacela.git] / src / gacela.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 gacela)
19   #:use-module (gacela system)
20   #:use-module (ice-9 threads)
21   #:use-module (srfi srfi-1)
22   #:export (make-world))
23
24
25 ;;; Entities and components
26
27 (define (make-world . entities)
28   (apply make-entity-set entities))
29
30 (define entities-mutex (make-mutex))
31 (define game-entities '())
32 (define game-components '())
33
34
35 (define (entity . components)
36   (with-mutex entities-mutex
37    (let ((key (gensym)))
38      (set! game-entities
39            (acons key
40                   (map (lambda (c) (list (get-component-type c) c)) components)
41                   game-entities))
42      (set! game-components (register-components key components))
43      key)))
44
45
46 (define* (register-components entity components #:optional (clist game-components))
47   (cond ((null? components) clist)
48         (else
49          (let* ((type (get-component-type (car components)))
50                 (elist (assoc-ref clist type)))
51            (register-components entity (cdr components)
52              (assoc-set! clist type
53                (cond (elist
54                       (lset-adjoin eq? elist entity))
55                      (else
56                       (list entity)))))))))
57
58
59 (define (get-entity key)
60   (with-mutex entities-mutex
61    (assoc key game-entities)))
62
63
64 (export entity
65         get-entity)