]> git.jsancho.org Git - gacela.git/blob - src/gacela_mobs.scm
12a0ef1d8aa90163e40a2469adfda912ac2b4184
[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 (use-modules (srfi srfi-1))
19
20 ;;; Actions for mobs
21
22 (define-macro (define-action action-def . code)
23   (let ((name (car action-def)) (attr (cdr action-def)))
24     `(define (,name mob-attr)
25        (let ,(map attribute-definition attr)
26          ,@code
27          ,(cons 'list (map attribute-result attr))))))
28
29 (define (attribute-definition attribute)
30   (let ((name (if (list? attribute) (car attribute) attribute))
31         (value (if (list? attribute) (cadr attribute) #f)))
32     `(,name (let ((v (assoc-ref mob-attr ',name))) (if v (cdr v) ,value)))))
33
34 (define (attribute-result attribute)
35   (let ((name (if (list? attribute) (car attribute) attribute)))
36     `(list ',name ,name)))
37
38
39 ;;; Mob Factory
40
41 (define-macro (makemob name . methods)
42   `(define* (,name . args)
43      (let ((option (car args)))
44        ,(lset-union eq?
45          `(case option
46             (:on (mob-on ',name))
47             (:off (mob-off ',name)))
48          (define (options m)
49            (let ((option (car m)) (body (cadr m)))
50              (cond ((null? m) '())
51                    (else (cons (list option `(apply ,body (cdr args))) (options (cddr m)))))))
52          (options methods)))))
53
54 (define-macro (makemob name . methods)
55   (define (options m)
56     (cond ((null? m) '((else #f)))
57           (else
58            (let ((option (caar m)) (body (cadar m)))
59              (cons `((,option) (apply ,body (cdr args))) (options (cdr m)))))))
60   (let ((m (options methods)))
61     `(define (,name . args)
62        (let ((option (car args)))
63          (case option
64            ((#:on) (mob-on ',name))
65            ((#:off) (mob-off ',name))
66            ,@m)))))
67
68
69 (define mob-on #f)
70 (define run-mobs #f)
71 (define mob-off #f)
72 (define refresh-running-mobs #f)
73 (define quit-all-mobs #f)
74
75 (let ((running-mobs '()) (mobs-to-add '()) (mobs-to-quit '()))
76   (set! mob-on
77         (lambda (mob)
78           (push mob mobs-to-add)))
79
80   (set! run-mobs
81         (lambda* (option #:key args function)
82           (define (run-mobs-rec mobs)
83             (cond ((null? mobs) #f)
84                   (else
85                    (cond (function (function)))
86                    (catch #t (lambda () (apply (car mobs) (cons option args))) (lambda (key . args) #f))
87                    (or #t (run-mobs-rec (cdr mobs))))))
88           (run-mobs-rec running-mobs)))
89
90   (set! mob-off
91         (lambda (mob)
92           (push mob mobs-to-quit)))
93
94   (set! refresh-running-mobs
95         (lambda ()
96           (do ((mob (pop mobs-to-add) (pop mobs-to-add))) ((null? mob))
97             (push mob running-mobs)
98             (catch #t (lambda () (mob #:init)) (lambda (key . args) #f)))
99           (set! running-mobs (reverse (lset-difference eq? running-mobs mobs-to-quit)))
100           (set! mobs-to-quit '())))
101
102   (set! quit-all-mobs
103         (lambda ()
104           (set! running-mobs '())
105           (set! mobs-to-add '())
106           (set! mobs-to-quit '()))))
107
108
109 (define (logic-mobs)
110   (run-mobs #:logic))
111
112 (define (render-mobs)
113   (run-mobs #:render #:function (lambda () (glLoadIdentity))))