X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=src%2Fgacela_mobs.scm;h=927d8b138af7125823b8b549e1dcf20605211e83;hb=d2cbfa2d53c011c7c1939e14201fb41a322a9031;hp=c9371784fef713383a137617b1297a596f1395ed;hpb=cbfc77bb602ebda15d2d95786a2c2bb4b5970a63;p=gacela.git diff --git a/src/gacela_mobs.scm b/src/gacela_mobs.scm index c937178..927d8b1 100755 --- a/src/gacela_mobs.scm +++ b/src/gacela_mobs.scm @@ -51,30 +51,83 @@ (define (process-mobs mobs) (for-each (lambda (m) (m #:render)) mobs)) -(define-macro (define-mob mob-head . look) - (let ((name (car mob-head)) (attr (cdr mob-head))) + +;;; 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))) + `(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-mob ,attr ,@look)))) + (lambda-action ,attr ,@code)))) -(define-macro (lambda-mob attr . look) +(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 . look) (define (process-look look) (cond ((null? look) (values '() '())) (else (let ((line (car look))) (receive (lines images) (process-look (cdr look)) (cond ((string? line) - (values (cons `(draw-texture ,line) lines) - (cons line images))) + (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 ((attr ',attr)) - (lambda (option) - (case option - ((#:render) - (glPushMatrix) - ,@look-lines -; ,@(map (lambda (x) (if (string? x) `(draw-texture ,x) x)) look) - (glPopMatrix))))))) + `(let ,look-images + (lambda () + (glPushMatrix) + ,@look-lines + (glPopMatrix))))) + + +;;; Making mobs + +(define-macro (define-mob mob-head . look) + (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 '()) (renders '())) + (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-renders) + renders) + ((set-renders) + (if (not (null? params)) (set! renders (car params)))))))) + (cond ((not (null? ',look)) + (display ',look) + (newline))) + mob))