]> 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 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     (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-objects ()
74     active-objects))
75
76
77 (defun make-object-old (name &key attr bhv look)
78   (let ((object
79          `(:name ,name :attr ,(make-object-attributes attr) :bhv ,(make-object-behaviour bhv) :look ,look)))
80     (add-object object)
81     object))
82
83 (defmacro make-object (name attr bhv &body look)
84   `(let ((object
85          '(:name ,name :attr ,(make-object-attributes attr) :bhv ,(make-object-behaviour bhv) :look (lambda () ,@look))))
86      (add-object object)
87      object))
88
89 (defun make-object-attributes (attr)
90   (cond ((or (null attr) (atom attr)) nil)
91         (t (let ((rest (make-object-attributes (cdr attr)))
92                  (this (object-attribute-definition (car attr))))
93              (setf (getf rest (car this)) (cadr this))
94              rest))))
95
96 (defun object-attribute-definition (attribute)
97   (let* ((name (cond ((listp attribute) (car attribute))
98                      (t attribute)))
99          (pname (attribute-name name))
100          (value (cond ((listp attribute) (cadr attribute)))))
101     `(,pname ,value)))
102
103 (defun make-object-behaviour (bhv)
104   (cond ((null bhv) nil)
105         ((atom bhv) (list bhv))
106         (t bhv)))