]> git.jsancho.org Git - gacela.git/blob - src/gacela_mobs.scm
9e971b919a4df631e306b142ead5deb1de4d1629
[gacela.git] / src / gacela_mobs.scm
1 ;;; Gacela, a GNU Guile 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 (use-modules (srfi srfi-1))
19
20 ;;; Actions for mobs
21
22 (define-macro (define-action action-def . code)
23   (let ((name (car action-def)) (attr (cdr action-def)))
24     `(define (,name mob-attr)
25        (let ,(map attribute-definition attr)
26          ,@code
27          ,(cons 'list (map attribute-result attr))))))
28
29 (define (attribute-definition attribute)
30   (let ((name (if (list? attribute) (car attribute) attribute))
31         (value (if (list? attribute) (cadr attribute) #f)))
32     `(,name (let ((v (assoc-ref mob-attr ',name))) (if v (cdr v) ,value)))))
33
34 (define (attribute-result attribute)
35   (let ((name (if (list? attribute) (car attribute) attribute)))
36     `(list ',name ,name)))
37
38
39 ;;; Mob Factory
40
41 (define add-mob #f)
42 (define kill-mob #f)
43 (define kill-all-mobs #f)
44 (define refresh-active-mobs #f)
45 (define action-mobs #f)
46 (define render-mobs #f)
47
48 (let ((active-mobs '()) (mobs-to-add '()) (mobs-to-kill '())
49   (set! add-mob
50         (lambda (mob)
51           (pushnew mob mobs-to-add)))
52
53   (set! kill-mob
54         (lambda (mob)
55           (pushnew mob mobs-to-kill))
56
57   (set! kill-all-mobs
58         (lambda ()
59           (set! active-mobs '())
60           (set! mobs-to-add '())
61           (set! mobs-to-kill '())))
62
63   (set! refresh-active-mobs
64         (lambda ()
65           (define (add active to-add)
66             (cond ((null? to-add) active)
67                   (else
68                    (pushnew (car to-add) active)
69                    (add active (cdr to-add)))))
70
71           (add active-mobs mobs-to-add)
72           (set! mobs-to-add '())
73           (set! active-mobs (reverse (lset-difference eq? active-mobs mobs-to-kill)))
74           (set! mobs-to-kill '())))
75
76   (set! action-mobs
77         (lambda ()
78           (map (lambda (m) (m #:action)) active-mobs)))
79
80   (set! render-mobs
81         (lambda ()
82           (map (lambda (m) (m #:render)) active-mobs))))
83
84
85 (define-macro (makemob name . methods)
86   `(define* (,name . args)
87      (let ((option (car args)))
88        ,(lset-union eq?
89          `(case option
90             (:on (mob-on ',name))
91             (:off (mob-off ',name)))
92          (define (options m)
93            (let ((option (car m)) (body (cadr m)))
94              (cond ((null? m) '())
95                    (else (cons (list option `(apply ,body (cdr args))) (options (cddr m)))))))
96          (options methods)))))
97
98 (define-macro (makemob name . methods)
99   (define (options m)
100     (cond ((null? m) '((else #f)))
101           (else
102            (let ((option (caar m)) (body (cadar m)))
103              (cons `((,option) (apply ,body (cdr args))) (options (cdr m)))))))
104   (let ((m (options methods)))
105     `(define (,name . args)
106        (let ((option (car args)))
107          (case option
108            ((#:on) (mob-on ',name))
109            ((#:off) (mob-off ',name))
110            ,@m)))))
111
112
113 (define mob-on #f)
114 (define run-mobs #f)
115 (define mob-off #f)
116 (define refresh-running-mobs #f)
117 (define quit-all-mobs #f)
118
119 (let ((running-mobs '()) (mobs-to-add '()) (mobs-to-quit '()))
120   (set! mob-on
121         (lambda (mob)
122           (push mob mobs-to-add)))
123
124   (set! run-mobs
125         (lambda* (option #:key args function)
126           (define (run-mobs-rec mobs)
127             (cond ((null? mobs) #f)
128                   (else
129                    (cond (function (function)))
130                    (catch #t (lambda () (apply (car mobs) (cons option args))) (lambda (key . args) #f))
131                    (or #t (run-mobs-rec (cdr mobs))))))
132           (run-mobs-rec running-mobs)))
133
134   (set! mob-off
135         (lambda (mob)
136           (push mob mobs-to-quit)))
137
138   (set! refresh-running-mobs
139         (lambda ()
140           (do ((mob (pop mobs-to-add) (pop mobs-to-add))) ((null? mob))
141             (push mob running-mobs)
142             (catch #t (lambda () (mob #:init)) (lambda (key . args) #f)))
143           (set! running-mobs (reverse (lset-difference eq? running-mobs mobs-to-quit)))
144           (set! mobs-to-quit '())))
145
146   (set! quit-all-mobs
147         (lambda ()
148           (set! running-mobs '())
149           (set! mobs-to-add '())
150           (set! mobs-to-quit '()))))
151
152
153 (define (logic-mobs)
154   (run-mobs #:logic))
155
156 (define (render-mobs)
157   (run-mobs #:render #:function (lambda () (glLoadIdentity))))