]> git.jsancho.org Git - gacela.git/blob - gacela_objects.lisp
(no commit message)
[gacela.git] / gacela_objects.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 objects
24
25 (defmacro make-behaviour (name attr &rest code)
26   `(defun ,(get-behaviour-fun-name name) (object)
27      (let ,(mapcar #'attribute-definition attr)
28        ,@code
29        ,(cons 'progn (mapcar #'attribute-save (reverse attr)))
30        object)))
31
32 (defun get-behaviour-fun-name (name)
33   (intern (concatenate 'string "BEHAVIOUR-" (string name)) 'gacela))
34
35 (defun attribute-name (attribute)
36   (intern (string attribute) 'keyword))
37
38 (defun attribute-definition (attribute)
39   (let* ((name (cond ((listp attribute) (car attribute))
40                      (t attribute)))
41          (pname (attribute-name name))
42          (value (cond ((listp attribute) (cadr attribute)))))
43     `(,name (getf object ,pname ,value))))
44
45 (defun attribute-save (attribute)
46   (let* ((name (cond ((listp attribute) (car attribute))
47                      (t attribute)))
48          (pname (attribute-name name)))
49     `(setf (getf object ,pname) ,name)))
50
51
52
53 ;;; Objects Factory
54
55 (let (active-objects objects-to-add objects-to-kill)
56   (defun add-object (object)
57     (push object objects-to-add))
58
59   (defun kill-object (object)
60     (push object objects-to-kill))
61
62   (defun kill-all-objects ()
63     (setq active-objects nil objects-to-add nil objects-to-kill nil))
64
65   (defun refresh-active-objects ()
66     (cond (objects-to-add
67            (setq active-objects (union active-objects objects-to-add))
68            (setq objects-to-add nil)))
69     (cond (objects-to-kill
70            (setq active-objects (reverse (set-difference active-objects objects-to-kill)))
71            (setq objects-to-kill nil))))
72
73   (defun render-boxes ()
74     (labels ((render (l)
75                      (cond (l (funcall (render-fun-name (car l)))
76                               (render (cdr l))))))
77             (render visible-boxes))))
78
79
80 (defmacro make-box (name attr &rest code)
81   `(progn
82      (let ,(union '((rx 0) (ry 0) (rz 0)) attr)
83        (defun ,(render-fun-name name) () ,@code)
84        (defun ,(get-props-fun-name name) () (list :rx rx :ry ry :rz rz)))
85      (add-box ',name)))
86
87 (defmacro make-object (name &key attr bhv look)
88   `(let ((object '(:name ,name)))
89      (union object
90             (union ,(make-object-attributes attr)
91                    (union ,(make-object-behaviour bhv)
92                           ,(make-object-look look))))))
93
94 (defun make-object-attributes (attr))
95
96 (defun make-object-behaviour (bhv))
97
98 (defun make-object-look (look))