X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=src%2Fgacela_mobs.scm;h=e0e2edd8ba5f7c718b7b6807a6354a98f2faec9e;hb=ca3edcecf937f854c1b5d9eeac566d85dc749cd0;hp=aeb65d96c5c6b9ccf63b1f4171b8089c4e4b0522;hpb=e1d58aa61dc8f521cc8a42b2f49208655603fc36;p=gacela.git diff --git a/src/gacela_mobs.scm b/src/gacela_mobs.scm index aeb65d9..e0e2edd 100755 --- a/src/gacela_mobs.scm +++ b/src/gacela_mobs.scm @@ -49,158 +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-mobs-logic mobs) - (for-each (lambda (m) (m 'run-logic)) mobs)) - -(define (render-mobs mobs) - (for-each (lambda (m) (m 'render)) mobs)) - - -;;; Logics 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-mob-logic logic-head . code) - (let ((name (car logic-head)) (attr (cdr logic-head))) - `(define ,name - (lambda-mob-logic ,attr ,@code)))) - -(define-macro (lambda-mob-logic attr . code) - `(lambda (attributes) - (let ,(map attr-def attr) - ,@code - ,(cons 'begin (map attr-save attr)) - attributes))) - -(define-macro (define-mob-look look-head . code) - (let ((name (car look-head)) (attr (cdr look-head))) - `(define ,name - (lambda-mob-look ,attr ,@code)))) - -(define-macro (lambda-mob-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-macro (define-mob mob-def) - (let ((name (car mob-def)) (def (cdr mob-def))) - `(define ,name - (lambda-mob ,@def)))) - -(defmacro* lambda-mob (#:key (attr '(quote ())) (logic #f) (look #f)) - `(let ((attr ,attr) (logic ,logic) (look ,look)) - (lambda (option . params) + `(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-attr) - attr) - ((set-attr) - (if (not (null? params)) (set! attr (car params)))) - ((get-logic) - logic) - ((set-logic) - (if (not (null? params)) (set! logic (car params)))) - ((get-look) - look) - ((set-look) - (if (not (null? params)) (set! look (car params)))) - ((run-logic) - (cond (logic - (catch #t - (lambda () (set! attr (logic attr))) - (lambda (key . args) #f))))) - ((render) - (cond (look - (catch #t - (lambda () (look attr)) - (lambda (key . args) #f))))))))) - - -(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 (set-mob-logic! mob logic) - (mob 'set-logic logic)) - -(define (set-mob-look! mob look) - (mob 'set-look look)) + ((get-mob-id) + mob-id) + (else + (catch #t + (lambda () ,@body) + (lambda (key . args) #f)))))))