]> git.jsancho.org Git - gacela.git/blob - gacela_entities.lisp
(no commit message)
[gacela.git] / gacela_entities.lisp
1 ;;; Gacela, a GNU Common Lisp extension for fast games development
2 ;;; Copyright (C) 2009 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 (eval-when (compile load eval)
19            (when (not (find-package 'gacela)) (make-package 'gacela :nicknames '(gg) :use '(lisp)))
20            (in-package 'gacela :nicknames '(gg) :use '(lisp)))
21
22
23 ;;; Behaviours of entities
24
25 (defmacro make-behaviour (name properties &rest code)
26   `(defun ,name (entity)
27      (let ,(mapcar #'property-definition properties)
28        ,@code
29        ,(cons 'progn (mapcar #'property-save properties))
30        entity)))
31
32 (defun property-name (property)
33   (intern (string property) 'keyword))
34
35 (defun property-definition (property)
36   (let* ((name (cond ((listp property) (car property))
37                      (t property)))
38          (pname (property-name name))
39          (value (cond ((listp property) (cadr property)))))
40     `(,name (getf entity ,pname ,value))))
41
42 (defun property-save (property)
43   (let* ((name (cond ((listp property) (car property))
44                      (t property)))
45          (pname (property-name name)))
46     `(setf (getf entity ,pname) ,name)))
47
48
49
50 ;;; Constructor
51
52 ;;; Boxes Factory
53
54 (let (visible-boxes boxes-to-add boxes-to-quit)
55   (defun add-box (box)
56     (push box boxes-to-add))
57
58   (defun quit-box (box)
59     (push box boxes-to-quit))
60
61   (defun quit-all-boxes ()
62     (setq visible-boxes nil boxes-to-add nil boxes-to-quit nil))
63
64   (defun refresh-visible-boxes ()
65     (cond (boxes-to-add
66            (setq visible-boxes (union visible-boxes boxes-to-add))
67            (setq boxes-to-add nil)))
68     (cond (boxes-to-quit
69            (setq visible-boxes (reverse (set-difference visible-boxes boxes-to-quit)))
70            (setq boxes-to-quit nil))))
71
72   (defun render-boxes ()
73     (labels ((render (l)
74                      (cond (l (funcall (render-fun-name (car l)))
75                               (render (cdr l))))))
76             (render visible-boxes))))
77
78
79 (defun render-fun-name (name)
80   (intern (concatenate 'string "RENDER-BOX-" (string name)) 'gacela))
81
82 (defun get-props-fun-name (name)
83   (intern (concatenate 'string "GET-PROPERTIES-BOX-" (string name)) 'gacela))
84
85 (defmacro make-box (name properties &rest code)
86   `(progn
87      (let ,(union '((rx 0) (ry 0) (rz 0)) properties)
88        (defun ,(render-fun-name name) () ,@code)
89        (defun ,(get-props-fun-name name) () (list :rx rx :ry ry :rz rz)))
90      (add-box ',name)))