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