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 `(let ((,mob-id-symbol (gensym))
100 (lambda* (#:optional (option #f))
102 (hide-mob-hash ,mob-id-symbol))
104 (let ((time (get-frame-time)))
105 (cond ((not (= time ,time-symbol))
106 (set! ,time-symbol time)
107 (set! ,data-symbol ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish)))))))
110 (define (filter-mobs type fun)
112 (define (map-mobs fun type)
113 (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) ,mob-id-symbol)))) (get-active-mobs))))
114 (map (lambda (m) (fun (m 'get-data))) mobs)))
127 (lambda (key . args) #f))))))))
129 (define-macro (lambda-mob attr . body)
130 `(the-mob 'undefined ,attr '() ,@body))
135 (define-macro (lambda-mob-data attr . body)
136 `(lambda ,attr ,@body))
138 (define-macro (define-collision-check name mobs . body)
139 `(defmacro* ,name (#:optional m)
140 `(let ,(cond (m `((mob-id (,m 'get-mob-id)) (mob-type (,m 'get-type))))