]> git.jsancho.org Git - gacela.git/blob - gacela_mobs.lisp
8f3d903120c6af184f7c5847c3006c112b60982e
[gacela.git] / gacela_mobs.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 ;;; Mob Factory
24
25 (defmacro makemob (name &rest methods)
26   `(defun ,name (&rest args &aux (option (car args)))
27      ,(union
28        `(case option
29               (:on (mob-on ',name))
30               (:off (mob-off ',name)))
31        (labels ((options (m &aux (option (car m)) (body (cadr m)))
32                          (cond ((null m) nil)
33                                (t (cons (list option `(apply ,body (cdr args))) (options (cddr m)))))))
34                (options methods)))))
35
36
37 (let (running-mobs mobs-to-add mobs-to-quit)
38   (defun mob-on (mob)
39     (push mob mobs-to-add))
40
41   (defun run-mobs (option &key args function)
42     (dolist (mob running-mobs)
43       (cond (function (funcall function)))
44       (secure-block nil (apply (symbol-function mob) (cons option args)))))
45
46   (defun mob-off (mob)
47     (push mob mobs-to-quit))
48
49   (defun refresh-running-mobs ()
50     (do ((mob (pop mobs-to-add) (pop mobs-to-add))) ((null mob))
51         (push mob running-mobs)
52         (secure-block nil (funcall (symbol-function mob) :init)))
53     (setq running-mobs (reverse (set-difference running-mobs mobs-to-quit)))
54     (setq mobs-to-quit nil))
55
56   (defun quit-all-mobs ()
57     (setq running-mobs nil mobs-to-add nil mobs-to-quit nil)))
58
59
60 (defun logic-mobs ()
61   (run-mobs :logic))
62
63 (defun render-mobs ()
64   (run-mobs :render :function (lambda () (glLoadIdentity))))