X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=src%2Fgacela_mobs.scm;h=e0e2edd8ba5f7c718b7b6807a6354a98f2faec9e;hb=ca3edcecf937f854c1b5d9eeac566d85dc749cd0;hp=12a0ef1d8aa90163e40a2469adfda912ac2b4184;hpb=9e55bd787fa48db3ce0b8bde5ce40539b301d30a;p=gacela.git diff --git a/src/gacela_mobs.scm b/src/gacela_mobs.scm index 12a0ef1..e0e2edd 100755 --- a/src/gacela_mobs.scm +++ b/src/gacela_mobs.scm @@ -15,99 +15,73 @@ ;;; along with this program. If not, see . -(use-modules (srfi srfi-1)) - -;;; Actions for mobs - -(define-macro (define-action action-def . code) - (let ((name (car action-def)) (attr (cdr action-def))) - `(define (,name mob-attr) - (let ,(map attribute-definition attr) - ,@code - ,(cons 'list (map attribute-result attr)))))) - -(define (attribute-definition attribute) - (let ((name (if (list? attribute) (car attribute) attribute)) - (value (if (list? attribute) (cadr attribute) #f))) - `(,name (let ((v (assoc-ref mob-attr ',name))) (if v (cdr v) ,value))))) - -(define (attribute-result attribute) - (let ((name (if (list? attribute) (car attribute) attribute))) - `(list ',name ,name))) - - -;;; Mob Factory - -(define-macro (makemob name . methods) - `(define* (,name . args) - (let ((option (car args))) - ,(lset-union eq? - `(case option - (:on (mob-on ',name)) - (:off (mob-off ',name))) - (define (options m) - (let ((option (car m)) (body (cadr m))) - (cond ((null? m) '()) - (else (cons (list option `(apply ,body (cdr args))) (options (cddr m))))))) - (options methods))))) - -(define-macro (makemob name . methods) - (define (options m) - (cond ((null? m) '((else #f))) - (else - (let ((option (caar m)) (body (cadar m))) - (cons `((,option) (apply ,body (cdr args))) (options (cdr m))))))) - (let ((m (options methods))) - `(define (,name . args) - (let ((option (car args))) - (case option - ((#:on) (mob-on ',name)) - ((#:off) (mob-off ',name)) - ,@m))))) - - -(define mob-on #f) -(define run-mobs #f) -(define mob-off #f) -(define refresh-running-mobs #f) -(define quit-all-mobs #f) - -(let ((running-mobs '()) (mobs-to-add '()) (mobs-to-quit '())) - (set! mob-on - (lambda (mob) - (push mob mobs-to-add))) - - (set! run-mobs - (lambda* (option #:key args function) - (define (run-mobs-rec mobs) - (cond ((null? mobs) #f) - (else - (cond (function (function))) - (catch #t (lambda () (apply (car mobs) (cons option args))) (lambda (key . args) #f)) - (or #t (run-mobs-rec (cdr mobs)))))) - (run-mobs-rec running-mobs))) - - (set! mob-off - (lambda (mob) - (push mob mobs-to-quit))) - - (set! refresh-running-mobs +;;; Mobs Factory + +(define show-mob-hash #f) +(define hide-mob-hash #f) +(define get-active-mobs #f) +(define clear-active-mobs #f) +(define mobs-changed? #f) + +(let ((active-mobs (make-hash-table)) (changed #f)) + (set! show-mob-hash + (lambda (key mob) + (hash-set! active-mobs key mob) + (set! changed #t))) + + (set! hide-mob-hash + (lambda (key) + (hash-remove! key) + (set! changed #t))) + + (set! get-active-mobs + (lambda* (#:optional (refreshed #t)) + (set! changed (not refreshed)) + (hash-map->list (lambda (k v) v) active-mobs))) + + (set! clear-active-mobs (lambda () - (do ((mob (pop mobs-to-add) (pop mobs-to-add))) ((null? mob)) - (push mob running-mobs) - (catch #t (lambda () (mob #:init)) (lambda (key . args) #f))) - (set! running-mobs (reverse (lset-difference eq? running-mobs mobs-to-quit))) - (set! mobs-to-quit '()))) - - (set! quit-all-mobs - (lambda () - (set! running-mobs '()) - (set! mobs-to-add '()) - (set! mobs-to-quit '())))) - - -(define (logic-mobs) - (run-mobs #:logic)) - -(define (render-mobs) - (run-mobs #:render #:function (lambda () (glLoadIdentity)))) + (set! changed #t) + (hash-clear! active-mobs))) + + (set! mobs-changed? + (lambda () changed))) + + +(define-macro (show-mob mob) + (cond ((list? mob) + `(let ((m ,mob)) + (show-mob-hash (m 'get-mob-id) m))) + (else + `(show-mob-hash (,mob 'get-mob-id) (lambda () (,mob)))))) + +(define-macro (hide-mob mob) + `(hide-mob-hash ',mob)) + +(define (run-mobs mobs) + (for-each + (lambda (m) + (glPushMatrix) + (m) + (glPopMatrix)) + mobs)) + + +;;; Making mobs + +(define-macro (define-mob mob-head . body) + (let ((name (car mob-head)) (attr (cdr mob-head))) + `(define ,(string->symbol (string-concatenate (list "make-" (symbol->string name)))) + (lambda () + (lambda-mob ,attr ,@body))))) + +(define-macro (lambda-mob attr . body) + `(let ,(cons '(mob-id (gensym)) attr) + (lambda* (#:optional (option #f)) + (case option + ((get-mob-id) + mob-id) + (else + (catch #t + (lambda () ,@body) + (lambda (key . args) #f)))))))