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