1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
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)
26 (let ((active-mobs (make-hash-table)) (changed #f))
29 (hash-set! active-mobs key mob)
38 (lambda* (#:optional (refreshed #t))
39 (set! changed (not refreshed))
40 (hash-map->list (lambda (k v) v) active-mobs)))
42 (set! clear-active-mobs
45 (hash-clear! active-mobs)))
51 (define-macro (show-mob mob)
52 `(show-mob-hash ',mob (lambda (option) (,mob option))))
54 (define-macro (hide-mob mob)
55 `(hide-mob-hash ',mob))
57 (define (run-mobs-logic mobs)
58 (for-each (lambda (m) (m 'run-logic)) mobs))
60 (define (render-mobs mobs)
61 (for-each (lambda (m) (m 'render)) mobs))
64 ;;; Logics and looks for mobs
66 (define (get-attr list name default)
67 (let ((value (assoc-ref list name)))
68 (cond (value (car value))
71 (define (attr-def attr)
72 (let ((name (car attr))
74 `(,name (get-attr attributes ',name ',value))))
76 (define (attr-save attr)
77 (let ((name (car attr)))
78 `(set! attributes (assoc-set! attributes ',name (list ,name)))))
80 (define-macro (define-mob-logic logic-head . code)
81 (let ((name (car logic-head)) (attr (cdr logic-head)))
83 (lambda-mob-logic ,attr ,@code))))
85 (define-macro (lambda-mob-logic attr . code)
87 (let ,(map attr-def attr)
89 ,(cons 'begin (map attr-save attr))
92 (define-macro (define-mob-look look-head . code)
93 (let ((name (car look-head)) (attr (cdr look-head)))
95 (lambda-mob-look ,attr ,@code))))
97 (define-macro (lambda-mob-look attr . look)
98 (define (process-look look)
99 (cond ((null? look) (values '() '()))
101 (let ((line (car look)))
102 (receive (lines images) (process-look (cdr look))
103 (cond ((string? line)
104 (let ((var (gensym)))
105 (values (cons `(draw-texture ,var) lines)
106 (cons `(,var (load-texture ,line)) images))))
108 (values (cons line lines)
111 (receive (look-lines look-images) (process-look look)
114 (let ,(map attr-def attr)
122 (define-macro (define-mob mob-head . look)
123 (let ((name (car mob-head)) (attr (cdr mob-head)))
125 (lambda-mob ,attr ,@look))))
127 (define-macro (lambda-mob attr . look)
130 (let ((attr ',attr) (actions '()) (looks '()))
131 (lambda (option . params)
136 (if (not (null? params)) (set! attr (car params))))
140 (if (not (null? params)) (set! actions (car params))))
144 (if (not (null? params)) (set! looks (car params))))
148 (set! attr ((cdr action) attr)))
155 (cond ((not (null? ',look))
159 (lambda-look ,attr ,@look))))))
162 (define-macro (define-mob mob-def)
163 (let ((name (car mob-def)) (def (cdr mob-def)))
167 (defmacro* lambda-mob (#:key (attr '()) (logic #f) (look #f))
168 `(let ((attr ,attr) (logic ,logic) (look ,look))
169 (lambda (option . params)
174 (if (not (null? params)) (set! attr (car params))))
178 (if (not (null? params)) (set! logic (car params))))
182 (if (not (null? params)) (set! look (car params))))
186 (lambda () (set! attr (logic attr)))
187 (lambda (key . args) #f)))))
191 (lambda () (look attr))
192 (lambda (key . args) #f)))))))))
195 (define (get-mob-attr mob var)
196 (let ((value (assoc-ref (mob 'get-attr) var)))
197 (if value (car value) #f)))
199 (define (set-mob-attr! mob var value)
200 (mob 'set-attr (assoc-set! (mob 'get-attr) var (list value))))
202 (define (set-mob-logic! mob logic)
203 (mob 'set-logic logic))
205 (define (set-mob-look! mob look)
206 (mob 'set-look look))