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