]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela_mobs.scm
Publishing mob data and defining mobs events.
[gacela.git] / src / gacela_mobs.scm
index e0e2edd8ba5f7c718b7b6807a6354a98f2faec9e..8cca9329ec3f1041df77dc3fdf9919bc7c856adf 100755 (executable)
@@ -31,7 +31,7 @@
 
   (set! hide-mob-hash
        (lambda (key)
-         (hash-remove! key)
+         (hash-remove! active-mobs key)
          (set! changed #t)))
 
   (set! get-active-mobs
         `(show-mob-hash (,mob 'get-mob-id) (lambda () (,mob))))))
 
 (define-macro (hide-mob mob)
-  `(hide-mob-hash ',mob))
+  (cond ((list? mob)
+        `(let ((m ,mob))
+           (hide-mob-hash (m 'get-mob-id))))
+       (else
+        `(hide-mob-hash (,mob 'get-mob-id)))))
 
 (define (run-mobs mobs)
+  (clear-mob-data)
   (for-each
    (lambda (m)
      (glPushMatrix)
 (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)))))
+       (lambda* ,(if (null? attr) '() `(#:key ,@attr))
+        (the-mob ',name () ,attr ,@body)))))
+
+(define-macro (the-mob type attr publish . body)
+  (let ((mob-id-symbol (gensym))
+       (type-symbol (gensym)))
+    `(let ((,mob-id-symbol (gensym))
+          (,type-symbol ,type)
+          ,@attr)
+       (lambda* (#:optional (option #f))
+        (define (kill-me)
+          (hide-mob-hash ,mob-id-symbol))
+        (case option
+          ((get-mob-id)
+           ,mob-id-symbol)
+          ((get-type)
+           ,type-symbol)
+          (else
+           (catch #t
+                  (lambda () ,@body)
+                  (lambda (key . args) #f))
+           (cond ((not (null? ',publish))
+                  (display ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish)))
+                  (publish-mob-data ,mob-id-symbol ,type-symbol ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish)))))
+           (newline)))))))
 
 (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)))))))
+  `(the-mob 'undefined ,attr '() ,@body))
+
+
+;;; Events Engine
+
+(define publish-mob-data #f)
+(define clear-mob-data #f)
+(define def-mobs-event #f)
+
+(define published-data (make-hash-table))
+(define mobs-events (make-hash-table))
+
+(let ((nop #f))
+  (set! publish-mob-data
+       (lambda (mob-id mob-type data)
+         (let ((t (hash-ref published-data mob-type))
+               (i (cons mob-id data)))
+           (hash-set! published-data mob-type
+                      (cond (t (cons i t))
+                            (else (list i)))))))
+
+  (set! clear-mob-data
+       (lambda ()
+         (hash-clear! published-data)))
+
+  (set! def-mobs-event
+       (lambda (type1 type2 fun)
+         #f))
+)
+
+(define-macro (define-mobs-event type1 type2 . body)
+  `(def-mobs-event
+     ,type1
+     ,type2
+     ,(cond ((null? body) #f)
+           (else `(lambda () ,@body)))))