]> git.jsancho.org Git - gacela.git/blob - src/gacela_mobs.scm
(no commit message)
[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   (cond ((list? mob)
53          `(let ((m ,mob))
54             (show-mob-hash (m 'get-mob-id) m)))
55         (else
56          `(show-mob-hash (,mob 'get-mob-id) (lambda () (,mob))))))
57
58 (define-macro (hide-mob mob)
59   `(hide-mob-hash ',mob))
60
61 (define (run-mobs-logic mobs)
62   (for-each (lambda (m) (m 'run-logic)) mobs))
63
64 (define (render-mobs mobs)
65   (for-each (lambda (m) (m 'render)) mobs))
66
67
68 ;;; Logics and looks for mobs
69
70 (define (get-attr list name default)
71   (let ((value (assoc-ref list name)))
72     (cond (value (car value))
73           (else default))))
74
75 (define (attr-def attr)
76   (let ((name (car attr))
77         (value (cadr attr)))
78     `(,name (get-attr attributes ',name ',value))))
79
80 (define (attr-save attr)
81   (let ((name (car attr)))
82     `(set! attributes (assoc-set! attributes ',name (list ,name)))))
83
84 (define-macro (define-mob-logic logic-head . code)
85   (let ((name (car logic-head)) (attr (cdr logic-head)))
86     `(define ,name
87        (lambda-mob-logic ,attr ,@code))))
88
89 (define-macro (lambda-mob-logic attr . code)
90   `(lambda (attributes)
91      (let ,(map attr-def attr)
92        ,@code
93        ,(cons 'begin (map attr-save attr))
94        attributes)))
95
96 (define-macro (define-mob-look look-head . code)
97   (let ((name (car look-head)) (attr (cdr look-head)))
98     `(define ,name
99        (lambda-mob-look ,attr ,@code))))
100
101 (define-macro (lambda-mob-look attr . look)
102   (define (process-look look)
103     (cond ((null? look) (values '() '()))
104           (else
105            (let ((line (car look)))
106              (receive (lines images) (process-look (cdr look))
107                       (cond ((string? line)
108                              (let ((var (gensym)))
109                                (values (cons `(draw-texture ,var) lines)
110                                        (cons `(,var (load-texture ,line)) images))))
111                             (else
112                              (values (cons line lines)
113                                      images))))))))
114
115   (receive (look-lines look-images) (process-look look)
116            `(let ,look-images
117                 (lambda (attributes)
118                   (let ,(map attr-def attr)
119                     (glPushMatrix)
120                     ,@look-lines
121                     (glPopMatrix))))))
122
123
124 ;;; Making mobs
125
126 (define-macro (define-mob mob-head . look)
127   (let ((name (car mob-head)) (attr (cdr mob-head)))
128     `(define ,name
129        (lambda-mob ,attr ,@look))))
130
131 (define-macro (lambda-mob attr . look)
132   `(let ((mob #f))
133      (set! mob
134            (let ((attr ',attr) (actions '()) (looks '()))
135              (lambda (option . params)
136                (case option
137                  ((get-attr)
138                   attr)
139                  ((set-attr)
140                   (if (not (null? params)) (set! attr (car params))))
141                  ((get-actions)
142                   actions)
143                  ((set-actions)
144                   (if (not (null? params)) (set! actions (car params))))
145                  ((get-looks)
146                   looks)
147                  ((set-looks)
148                   (if (not (null? params)) (set! looks (car params))))
149                  ((run-actions)
150                   (for-each
151                    (lambda (action)
152                      (set! attr ((cdr action) attr)))
153                    actions))
154                  ((render)
155                   (for-each
156                    (lambda (look)
157                      ((cdr look) attr))
158                    looks))))))
159      (cond ((not (null? ',look))
160             (mob 'set-looks
161                  (list (cons
162                         'default-look
163                         (lambda-look ,attr ,@look))))))
164      mob))
165
166 (define-macro (define-mob mob-def)
167   (let ((name (car mob-def)) (def (cdr mob-def)))
168     `(define ,name
169        (lambda-mob ,@def))))
170
171 (defmacro* lambda-mob (#:key (attr '(quote ())) (logic #f) (look #f))
172   `(let ((attr ,attr) (logic ,logic) (look ,look))
173      (lambda (option . params)
174        (case option
175          ((get-attr)
176           attr)
177          ((set-attr)
178           (if (not (null? params)) (set! attr (car params))))
179          ((get-logic)
180           logic)
181          ((set-logic)
182           (if (not (null? params)) (set! logic (car params))))
183          ((get-look)
184           look)
185          ((set-look)
186           (if (not (null? params)) (set! look (car params))))
187          ((run-logic)
188           (cond (logic
189                  (catch #t
190                         (lambda () (set! attr (logic attr)))
191                         (lambda (key . args) #f)))))
192          ((render)
193           (cond (look
194                  (catch #t
195                         (lambda () (look attr))
196                         (lambda (key . args) #f)))))))))
197
198
199 (define (get-mob-attr mob var)
200   (let ((value (assoc-ref (mob 'get-attr) var)))
201     (if value (car value) #f)))
202
203 (define (set-mob-attr! mob var value)
204   (mob 'set-attr (assoc-set! (mob 'get-attr) var (list value))))
205
206 (define (set-mob-logic! mob logic)
207   (mob 'set-logic logic))
208
209 (define (set-mob-look! mob look)
210   (mob 'set-look look))
211
212
213
214
215
216 (define-macro (define-mob mob-head . body)
217   (let ((name (car mob-head)) (attr (cdr mob-head)))
218     `(define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
219        (lambda-mob ,attr ,@body))))
220
221 (define-macro (lambda-mob attr . body)
222   `(lambda ()
223      (let ,(cons '(mob-id (gensym)) attr)
224        (lambda* (#:optional (option #f))
225          (case option
226            ((get-mob-id)
227             mob-id)
228            (else
229             (catch #t
230                    (lambda () ,@body)
231                    (lambda (key . args) #f))))))))