1 ;;; Gacela, a GNU Common Lisp extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
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)))
23 ;;; Behaviours of objects
25 (defmacro make-behaviour (name attr &rest code)
26 `(defun ,(get-behaviour-fun-name name) (object-attr)
27 (let ,(mapcar #'attribute-definition attr)
29 ,(cons 'progn (mapcar #'attribute-save (reverse attr)))
32 (defun get-behaviour-fun-name (name)
33 (intern (concatenate 'string "BEHAVIOUR-" (string name)) 'gacela))
35 (defun attribute-name (attribute)
36 (intern (string attribute) 'keyword))
38 (defun attribute-definition (attribute)
39 (let* ((name (cond ((listp attribute) (car attribute))
41 (pname (attribute-name name))
42 (value (cond ((listp attribute) (cadr attribute)))))
43 `(,name (getf object-attr ,pname ,value))))
45 (defun attribute-save (attribute)
46 (let* ((name (cond ((listp attribute) (car attribute))
48 (pname (attribute-name name)))
49 `(setf (getf object-attr ,pname) ,name)))
55 (let (active-objects objects-to-add objects-to-kill)
56 (defun add-object (object)
57 (push object objects-to-add))
59 (defun kill-object (object)
60 (push object objects-to-kill))
62 (defun kill-all-objects ()
63 (setq active-objects nil objects-to-add nil objects-to-kill nil))
65 (defun refresh-active-objects ()
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))))
73 (defun render-objects ()
77 (defun make-object-old (name &key attr bhv look)
79 `(:name ,name :attr ,(make-object-attributes attr) :bhv ,(make-object-behaviour bhv) :look ,look)))
83 (defmacro make-object (name attr bhv &body look)
84 `(let ((attr ,(make-object-attributes attr) bhv ,(make-object-behaviour bhv)))
85 (defun ,name (option) ,@look)
88 (defun make-object-attributes (attr)
89 (cond ((or (null attr) (atom attr)) nil)
90 (t (let ((rest (make-object-attributes (cdr attr)))
91 (this (object-attribute-definition (car attr))))
92 (setf (getf rest (car this)) (cadr this))
95 (defun object-attribute-definition (attribute)
96 (let* ((name (cond ((listp attribute) (car attribute))
98 (pname (attribute-name name))
99 (value (cond ((listp attribute) (cadr attribute)))))
102 (defun make-object-behaviour (bhv)
103 (cond ((null bhv) nil)
104 ((atom bhv) (list bhv))