]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela_mobs.scm
(no commit message)
[gacela.git] / src / gacela_mobs.scm
index c0507f5af6eb529a4a61a44f6fe1ac060cc8cb5a..8c687dc5b42158dd01748059c7c88e552aa7c6da 100755 (executable)
@@ -20,6 +20,7 @@
 (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! changed (not refreshed))
          (hash-map->list (lambda (k v) v) active-mobs)))
 
+  (set! clear-active-mobs
+       (lambda ()
+         (set! changed #t)
+         (hash-clear! active-mobs)))
+
   (set! mobs-changed?
        (lambda () changed)))
 
 (define-macro (hide-mob mob)
   `(hide-mob-hash ',mob))
 
-(define (process-mobs mobs)
-  (for-each (lambda (m) (m #:render)) mobs))
+(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
 
-(defmacro make-behaviour (name attr &rest code)
-  `(defun ,(get-behaviour-fun-name name) (object-attr)
-     (let ,(mapcar #'attribute-definition attr)
-       ,@code
-       ,(cons 'progn (mapcar #'attribute-save (reverse attr)))
-       object-attr)))
+(define (get-attr list name default)
+  (let ((value (assoc-ref list name)))
+    (cond (value (car value))
+         (else default))))
 
-(defun get-behaviour-fun-name (name)
-  (intern (concatenate 'string "BEHAVIOUR-" (string-upcase (string name))) 'gacela))
+(define (attr-def attr)
+  (let ((name (car attr))
+       (value (cadr attr)))
+    `(,name (get-attr attributes ',name ',value))))
 
-(defun attribute-name (attribute)
-  (intern (string attribute) 'keyword))
+(define (attr-save attr)
+  (let ((name (car attr)))
+    `(set! attributes (assoc-set! attributes ',name (list ,name)))))
 
-(define (attribute-definition attribute)
-  (let* ((name (cond ((list? attribute) (car attribute))
-                    (else attribute)))
-        (pname (attribute-name name))
-        (value (cond ((listp attribute) (cadr attribute)))))
-    `(,name (getf object-attr ,pname ,value))))
+(define-macro (define-action action-head . code)
+  (let ((name (car action-head)) (attr (cdr action-head)))
+    `(define ,name
+       (lambda-action ,attr ,@code))))
 
-(defun attribute-save (attribute)
-  (let* ((name (cond ((listp attribute) (car attribute))
-                    (t attribute)))
-        (pname (attribute-name name)))
-    `(setf (getf object-attr ,pname) ,name)))
+(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-macro (lambda-look attr . look)
   (define (process-look look)
     (cond ((null? look) (values '() '()))
          (else
 
   (receive (look-lines look-images) (process-look look)
           `(let ,look-images
-               (lambda ()
-                 (glPushMatrix)
-                 ,@look-lines
-                 (glPopMatrix)))))
+               (lambda (attributes)
+                 (let ,(map attr-def attr)
+                   (glPushMatrix)
+                   ,@look-lines
+                   (glPopMatrix))))))
 
 
 ;;; Making mobs
 (define-macro (lambda-mob attr . look)
   `(let ((mob #f))
      (set! mob
-          (let ((attr ',attr) (actions '()) (renders '()))
+          (let ((attr ',attr) (actions '()) (looks '()))
             (lambda (option . params)
               (case option
                 ((get-attr)
                  actions)
                 ((set-actions)
                  (if (not (null? params)) (set! actions (car params))))
-                ((get-renders)
-                 renders)
-                ((set-renders)
-                 (if (not (null? params)) (set! renders (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))
-           (display ',look)
-           (newline)))
+           (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 '()) (action #f) (look #f))
+  `(let ((attr ,attr) (action ,action) (look ,look))
+     (lambda (option . params)
+       (case option
+        ((get-attr)
+         attr)
+        ((set-attr)
+         (if (not (null? params)) (set! attr (car params))))
+        ((get-action)
+         action)
+        ((set-action)
+         (if (not (null? params)) (set! action (car params))))
+        ((get-look)
+         look)
+        ((set-look)
+         (if (not (null? params)) (set! look (car params))))
+        ((run-mob)
+         (lambda (action)
+                    (set! attr ((cdr action) attr)))
+                  actions))
+                ((render)
+                 (for-each
+                  (lambda (look)
+                    ((cdr look) attr))
+                  looks))))))
+
+
+(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)))