]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela_mobs.scm
Mobs Events Engine
[gacela.git] / src / gacela_mobs.scm
index ede4235cb653b04fc96f7a1489b21a7e5eff6554..0ae561e8f6eecbdf6c848cc2d7664fa240aa1da0 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))
@@ -30,7 +31,7 @@
 
   (set! hide-mob-hash
        (lambda (key)
-         (hash-remove! key)
+         (hash-remove! active-mobs key)
          (set! changed #t)))
 
   (set! get-active-mobs
          (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 (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))
+  (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)
+  (for-each
+   (lambda (m)
+     (glPushMatrix)
+     (m)
+     (glPopMatrix))
+   mobs))
+
 
-(define (process-mobs mobs)
-  (for-each (lambda (m) (m #:render)) 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
-       (let ((attr ',attr))
-        (lambda (option)
-          (case option
-            ((#:render)
-             (glPushMatrix)
-             ,@(map (lambda (x) (if (string? x) `(draw-image ,x) x)) look)
-             (glPopMatrix))))))))
+    `(define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
+       (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)
+  `(the-mob 'undefined ,attr '() ,@body))
+
+
+;;; Events Engine
+
+(define publish-mob-data #f)
+(define published-data (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))))))))