]> git.jsancho.org Git - gacela.git/blob - gacela_mobs.lisp
(no commit message)
[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 (defmacro mob-structure (variables init logic render)
34   `(list
35     :init (lambda () ,init)
36     :logic (lambda () ,logic)
37     :render (lambda () ,render)
38     :context (lambda ()
39                ,(if variables
40                     `(mapcar #'list
41                              ',(mapcar #'car+ variables)
42                              (multiple-value-list
43                               (values-list ,(cons 'list (mapcar #'car+ variables)))))
44                   nil))))
45
46 (let (running-mobs mobs-to-add mobs-to-quit)
47   (defun mob-on (mob)
48     (push mob mobs-to-add))
49
50   (defun logic-mobs ()
51     (dolist (mob running-mobs) (funcall mob :logic)))
52
53   (defun render-mobs ()
54     (dolist (mob running-mobs) (funcall mob :render)))
55
56   (defun mob-off (mob)
57     (push mob mobs-to-quit))
58
59   (defun refresh-running-mobs ()
60     (do ((mob (pop mobs-to-add) (pop mobs-to-add))) ((null mob))
61         (push mob running-mobs)
62         (funcall mob :init))
63     (setq running-mobs (reverse (set-difference running-mobs mobs-to-quit :test #'equal))))
64
65   (defun quit-all-mobs ()
66     (setq running-mobs nil mobs-to-add nil mobs-to-quit nil)))