]> 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-attr)
27      (let ,(mapcar #'attribute-definition attr)
28        ,@code
29        ,(cons 'progn (mapcar #'attribute-save (reverse attr)))
30        object-attr)))
31
32 (defun get-behaviour-fun-name (name)
33   (intern (concatenate 'string "BEHAVIOUR-" (string-upcase (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-attr ,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-attr ,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     (pushnew object objects-to-add))
58
59   (defun kill-object (object)
60     (pushnew 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 bhv-objects ()
74     (dolist (o active-objects) (funcall o :action)))
75
76   (defun render-objects ()
77     (dolist (o active-objects) (funcall o :render))))
78
79
80 (defmacro make-object (name attr bhv &body look)
81   `(progn
82      (let ((attr ,(cond (attr (cons 'list (make-object-attributes attr)))))
83            (bhv ,(cond (bhv (cons 'list (make-object-behaviour bhv))))))
84        (defun ,name (option &rest param)
85          (case option
86                (:action (dolist (b bhv t) (setq attr (funcall (get-behaviour-fun-name b) attr))))
87                (:get-attr attr)
88                (:get-bhv bhv)
89                (:set-bhv (setq bhv (car param)))
90                (:render (glPushMatrix)
91                         ,@(mapcar (lambda (x) (if (stringp x) `(draw-image ,x) x)) look)
92                         (glPopMatrix)))))
93      (add-object ',name)
94      ',name))
95
96 (defun make-object-attributes (attr)
97   (cond ((or (null attr) (atom attr)) nil)
98         (t (let ((rest (make-object-attributes (cdr attr)))
99                  (this (object-attribute-definition (car attr))))
100              (setf (getf rest (car this)) (cadr this))
101              rest))))
102
103 (defun object-attribute-definition (attribute)
104   (let* ((name (cond ((listp attribute) (car attribute))
105                      (t attribute)))
106          (pname (attribute-name name))
107          (value (cond ((listp attribute) (cadr attribute)))))
108     `(,pname ,value)))
109
110 (defun make-object-behaviour (bhv)
111   (cond ((null bhv) nil)
112         ((atom bhv) (list bhv))
113         (t bhv)))