]> git.jsancho.org Git - gacela.git/blob - src/gacela_mobs.scm
8c687dc5b42158dd01748059c7c88e552aa7c6da
[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 ;;; Mobs Factory
19
20 (define show-mob-hash #f)
21 (define hide-mob-hash #f)
22 (define get-active-mobs #f)
23 (define clear-active-mobs #f)
24 (define mobs-changed? #f)
25
26 (let ((active-mobs (make-hash-table)) (changed #f))
27   (set! show-mob-hash
28         (lambda (key mob)
29           (hash-set! active-mobs key mob)
30           (set! changed #t)))
31
32   (set! hide-mob-hash
33         (lambda (key)
34           (hash-remove! key)
35           (set! changed #t)))
36
37   (set! get-active-mobs
38         (lambda* (#:optional (refreshed #t))
39           (set! changed (not refreshed))
40           (hash-map->list (lambda (k v) v) active-mobs)))
41
42   (set! clear-active-mobs
43         (lambda ()
44           (set! changed #t)
45           (hash-clear! active-mobs)))
46
47   (set! mobs-changed?
48         (lambda () changed)))
49
50
51 (define-macro (show-mob mob)
52   `(show-mob-hash ',mob (lambda (option) (,mob option))))
53
54 (define-macro (hide-mob mob)
55   `(hide-mob-hash ',mob))
56
57 (define (run-mob-actions mobs)
58   (for-each (lambda (m) (m 'run-actions)) mobs))
59
60 (define (render-mobs mobs)
61   (for-each (lambda (m) (m 'render)) mobs))
62
63
64 ;;; Actions and looks for mobs
65
66 (define (get-attr list name default)
67   (let ((value (assoc-ref list name)))
68     (cond (value (car value))
69           (else default))))
70
71 (define (attr-def attr)
72   (let ((name (car attr))
73         (value (cadr attr)))
74     `(,name (get-attr attributes ',name ',value))))
75
76 (define (attr-save attr)
77   (let ((name (car attr)))
78     `(set! attributes (assoc-set! attributes ',name (list ,name)))))
79
80 (define-macro (define-action action-head . code)
81   (let ((name (car action-head)) (attr (cdr action-head)))
82     `(define ,name
83        (lambda-action ,attr ,@code))))
84
85 (define-macro (lambda-action attr . code)
86   `(lambda (attributes)
87      (let ,(map attr-def attr)
88        ,@code
89        ,(cons 'begin (map attr-save attr))
90        attributes)))
91
92 (define-macro (lambda-look attr . look)
93   (define (process-look look)
94     (cond ((null? look) (values '() '()))
95           (else
96            (let ((line (car look)))
97              (receive (lines images) (process-look (cdr look))
98                       (cond ((string? line)
99                              (let ((var (gensym)))
100                                (values (cons `(draw-texture ,var) lines)
101                                        (cons `(,var (load-texture ,line)) images))))
102                             (else
103                              (values (cons line lines)
104                                      images))))))))
105
106   (receive (look-lines look-images) (process-look look)
107            `(let ,look-images
108                 (lambda (attributes)
109                   (let ,(map attr-def attr)
110                     (glPushMatrix)
111                     ,@look-lines
112                     (glPopMatrix))))))
113
114
115 ;;; Making mobs
116
117 (define-macro (define-mob mob-head . look)
118   (let ((name (car mob-head)) (attr (cdr mob-head)))
119     `(define ,name
120        (lambda-mob ,attr ,@look))))
121
122 (define-macro (lambda-mob attr . look)
123   `(let ((mob #f))
124      (set! mob
125            (let ((attr ',attr) (actions '()) (looks '()))
126              (lambda (option . params)
127                (case option
128                  ((get-attr)
129                   attr)
130                  ((set-attr)
131                   (if (not (null? params)) (set! attr (car params))))
132                  ((get-actions)
133                   actions)
134                  ((set-actions)
135                   (if (not (null? params)) (set! actions (car params))))
136                  ((get-looks)
137                   looks)
138                  ((set-looks)
139                   (if (not (null? params)) (set! looks (car params))))
140                  ((run-actions)
141                   (for-each
142                    (lambda (action)
143                      (set! attr ((cdr action) attr)))
144                    actions))
145                  ((render)
146                   (for-each
147                    (lambda (look)
148                      ((cdr look) attr))
149                    looks))))))
150      (cond ((not (null? ',look))
151             (mob 'set-looks
152                  (list (cons
153                         'default-look
154                         (lambda-look ,attr ,@look))))))
155      mob))
156
157 (define-macro (define-mob mob-def)
158   (let ((name (car mob-def)) (def (cdr mob-def)))
159     `(define ,name
160        (lambda-mob ,def))))
161
162 (defmacro* lambda-mob (#:key (attr '()) (action #f) (look #f))
163   `(let ((attr ,attr) (action ,action) (look ,look))
164      (lambda (option . params)
165        (case option
166          ((get-attr)
167           attr)
168          ((set-attr)
169           (if (not (null? params)) (set! attr (car params))))
170          ((get-action)
171           action)
172          ((set-action)
173           (if (not (null? params)) (set! action (car params))))
174          ((get-look)
175           look)
176          ((set-look)
177           (if (not (null? params)) (set! look (car params))))
178          ((run-mob)
179           (lambda (action)
180                      (set! attr ((cdr action) attr)))
181                    actions))
182                  ((render)
183                   (for-each
184                    (lambda (look)
185                      ((cdr look) attr))
186                    looks))))))
187
188
189 (define (get-mob-attr mob var)
190   (let ((value (assoc-ref (mob 'get-attr) var)))
191     (if value (car value) #f)))
192
193 (define (set-mob-attr! mob var value)
194   (mob 'set-attr (assoc-set! (mob 'get-attr) var (list value))))
195
196 (define (add-mob-action mob name action)
197   (mob 'set-actions (assoc-set! (mob 'get-actions) name action)))
198
199 (define (quit-mob-action mob name)
200   (mob 'set-actions (assoc-remove! (mob 'get-actions) name)))
201
202 (define (add-mob-look mob name look)
203   (mob 'set-looks (assoc-set! (mob 'get-looks) name look)))
204
205 (define (quit-mob-look mob name)
206   (mob 'set-looks (assoc-remove! (mob 'get-looks) name)))