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)))
84 (define-macro (define-mob mob-head . body)
85 (let ((name (car mob-head)) (attr (cdr mob-head)))
86 `(define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
87 (lambda* ,(if (null? attr) '() `(#:key ,@attr))
88 (the-mob ',name () ,attr ,@body)))))
90 (define-macro (the-mob type attr publish . body)
91 (let ((mob-id-symbol (gensym))
92 (type-symbol (gensym))
93 (time-symbol (gensym))
94 (data-symbol (gensym))
95 (save-symbol (gensym)))
96 `(let ((,mob-id-symbol (gensym))
101 (lambda* (#:optional (option #f))
103 (hide-mob-hash ,mob-id-symbol))
104 (define (,save-symbol)
105 (let ((time (get-frame-time)))
106 (cond ((not (= time ,time-symbol))
107 (set! ,time-symbol time)
108 (set! ,data-symbol ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish)))))))
109 (define (map-mobs fun type)
110 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) ,mob-id-symbol)))) (get-active-mobs))))
111 (map (lambda (m) (fun (m 'get-data))) mobs)))
124 (lambda (key . args) #f))))))))
126 (define-macro (lambda-mob attr . body)
127 `(the-mob 'undefined ,attr '() ,@body))