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 refresh-active-mobs #f)
23 (define get-active-mobs #f)
24 (define hide-all-mobs #f)
25 (define mobs-changed? #f)
27 (let ((mobs-table (make-hash-table))
33 (hash-set! mobs-table (mob 'get-mob-id) mob)
38 (hash-remove! mobs-table mob-id)
41 (set! refresh-active-mobs
45 (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table))))))
48 (lambda () active-mobs))
53 (hash-clear! mobs-table)))
59 (define-macro (show-mob mob)
64 `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
66 (define-macro (hide-mob mob)
69 (hide-mob-hash (m 'get-mob-id))))
71 `(hide-mob-hash (,mob 'get-mob-id)))))
73 (define* (run-mobs #:optional (mobs (get-active-mobs)))
75 (for-each (lambda (m) (m 'publish-data)) mobs)
88 (define-macro (define-mob mob-head . body)
89 (let ((name (car mob-head)) (attr (cdr mob-head)))
90 `(define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
91 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
92 (the-mob ',name () ,attr ,@body)))))
94 (define-macro (the-mob type attr publish . body)
95 (let ((mob-id-symbol (gensym))
96 (type-symbol (gensym)))
97 `(let ((,mob-id-symbol (gensym))
100 (lambda* (#:optional (option #f))
102 (hide-mob-hash ,mob-id-symbol))
109 ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish)))
113 (lambda (key . args) #f))))))))
115 (define-macro (lambda-mob attr . body)
116 `(the-mob 'undefined ,attr '() ,@body))
121 (define def-mobs-event #f)
122 (define run-mobs-events #f)
123 (define clear-events-data #f)
125 (define mobs-events (make-hash-table))
126 (define returned-data (make-hash-table))
132 (hash-remove! mobs-events pair))
134 (hash-set! mobs-events pair fun)))))
136 (set! run-mobs-events
137 (lambda* (#:optional (mobs (get-active-mobs)))
140 (let* ((t1 (car types)) (t2 (cadr types))
141 (mobs-t1 (filter (lambda (m) (eq? (m 'get-type) t1)) mobs))
142 (mobs-t2 (filter (lambda (m) (eq? (m 'get-type) t2)) mobs)))
143 (cond ((not (or (null? mobs-t1) (null? mobs-t2)))
146 (let ((id1 (m1 'get-mob-id)))
149 (let ((id2 (m2 'get-mob-id)))
150 (cond ((not (eq? id1 id2))
152 (lambda () (fun (m1 'get-data) (m2 'get-data)))
153 (lambda (key . args) #f))))
154 (cond ((and (list? res) (>= (length res) 2))
155 (return-data id1 t2 (car res))
156 (return-data id2 t1 (cadr res)))))))))
162 (define (return-data mob-id mob-type data)
163 (let* ((key (list mob-id mob-type))
164 (res (hash-ref returned-data key)))
165 (hash-set! returned-data key
166 (cond (res (cons data res))
167 (else (list data))))))
169 (set! clear-events-data
171 (hash-clear! returned-data))))
173 (define-macro (define-mobs-event types-pair . body)
176 ,(cond ((null? body) #f)
177 (else `(lambda (,(car types-pair) ,(cadr types-pair)) ,@body)))))