X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=src%2Fgacela_mobs.scm;h=e0e2edd8ba5f7c718b7b6807a6354a98f2faec9e;hb=ca3edcecf937f854c1b5d9eeac566d85dc749cd0;hp=ed55a007e85251326cc4033768d7b8d87355b755;hpb=e82142f2231e5aacf47e3f0cf5e82679d4291af2;p=gacela.git diff --git a/src/gacela_mobs.scm b/src/gacela_mobs.scm index ed55a00..e0e2edd 100755 --- a/src/gacela_mobs.scm +++ b/src/gacela_mobs.scm @@ -49,126 +49,39 @@ (define-macro (show-mob mob) - `(show-mob-hash ',mob (lambda (option) (,mob option)))) + (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-mob-actions mobs) - (for-each (lambda (m) (m 'run-actions)) mobs)) - -(define (render-mobs mobs) - (for-each (lambda (m) (m 'render)) mobs)) - - -;;; Actions and looks for mobs - -(define (get-attr list name default) - (let ((value (assoc-ref list name))) - (cond (value (car value)) - (else default)))) - -(define (attr-def attr) - (let ((name (car attr)) - (value (cadr attr))) - `(,name (get-attr attributes ',name ',value)))) - -(define (attr-save attr) - (let ((name (car attr))) - `(set! attributes (assoc-set! attributes ',name (list ,name))))) - -(define-macro (define-action action-head . code) - (let ((name (car action-head)) (attr (cdr action-head))) - `(define ,name - (lambda-action ,attr ,@code)))) - -(define-macro (lambda-action attr . code) - `(lambda (attributes) - (let ,(map attr-def attr) - ,@code - ,(cons 'begin (map attr-save attr)) - attributes))) - -(define-macro (lambda-look attr . look) - (define (process-look look) - (cond ((null? look) (values '() '())) - (else - (let ((line (car look))) - (receive (lines images) (process-look (cdr look)) - (cond ((string? line) - (let ((var (gensym))) - (values (cons `(draw-texture ,var) lines) - (cons `(,var (load-texture ,line)) images)))) - (else - (values (cons line lines) - images)))))))) - - (receive (look-lines look-images) (process-look look) - `(let ,look-images - (lambda (attributes) - (let ,(map attr-def attr) - (glPushMatrix) - ,@look-lines - (glPopMatrix)))))) +(define (run-mobs mobs) + (for-each + (lambda (m) + (glPushMatrix) + (m) + (glPopMatrix)) + mobs)) ;;; Making mobs -(define-macro (define-mob mob-head . look) +(define-macro (define-mob mob-head . body) (let ((name (car mob-head)) (attr (cdr mob-head))) - `(define ,name - (lambda-mob ,attr ,@look)))) - -(define-macro (lambda-mob attr . look) - `(let ((mob #f)) - (set! mob - (let ((attr ',attr) (actions '()) (looks '())) - (lambda (option . params) - (case option - ((get-attr) - attr) - ((set-attr) - (if (not (null? params)) (set! attr (car params)))) - ((get-actions) - actions) - ((set-actions) - (if (not (null? params)) (set! actions (car params)))) - ((get-looks) - looks) - ((set-looks) - (if (not (null? params)) (set! looks (car params)))) - ((run-actions) - (for-each - (lambda (action) - (set! attr ((cdr action) attr))) - actions)) - ((render) - (for-each - (lambda (look) - ((cdr look) attr)) - looks)))))) - (cond ((not (null? ',look)) - (mob 'set-looks - (list (cons - 'default-look - (lambda-look ,attr ,@look)))))) - mob)) - -(define (get-mob-attr mob var) - (let ((value (assoc-ref (mob 'get-attr) var))) - (if value (car value) #f))) - -(define (set-mob-attr! mob var value) - (mob 'set-attr (assoc-set! (mob 'get-attr) var (list value)))) - -(define (add-mob-action mob name action) - (mob 'set-actions (assoc-set! (mob 'get-actions) name action))) - -(define (quit-mob-action mob name) - (mob 'set-actions (assoc-remove! (mob 'get-actions) name))) - -(define (add-mob-look mob name look) - (mob 'set-looks (assoc-set! (mob 'get-looks) name look))) - -(define (quit-mob-look mob name) - (mob 'set-looks (assoc-remove! (mob 'get-looks) name))) + `(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)))))))