]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela_mobs.scm
Events Engine running and returning data.
[gacela.git] / src / gacela_mobs.scm
index a9c13fa1acc8994713ee7f94f3eeb1a2c9a9b951..fc99b89d39c7f5690ff02bde86129772b3a9b52d 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))
-
-(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))))))
-
-
-;;; 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 '()) (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)
-       (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))
-
+  (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) (m 'publish-data)) mobs)
+  (run-mobs-events)
+  (for-each
+   (lambda (m)
+     (glPushMatrix)
+     (m)
+     (glPopMatrix))
+   mobs))
 
 
+;;; Making mobs
 
 (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-mob ,attr ,@body))))
-
-(define-macro (lambda-mob attr . body)
-  `(lambda ()
-     (let ,(cons '(mob-id (gensym)) attr)
+       (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)
+           ,mob-id-symbol)
+          ((get-type)
+           ,type-symbol)
+          ((publish-data)
+           (cond ((not (null? ',publish))
+                  (publish-mob-data ,mob-id-symbol ,type-symbol ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish))))))
           (else
            (catch #t
                   (lambda () ,@body)
                   (lambda (key . args) #f))))))))
+
+(define-macro (lambda-mob attr . body)
+  `(the-mob 'undefined ,attr '() ,@body))
+
+
+;;; Events Engine
+
+(define publish-mob-data #f)
+(define clear-mob-data #f)
+(define def-mobs-event #f)
+(define run-mobs-events #f)
+(define return-data #f)
+
+(define published-data (make-hash-table))
+(define mobs-events (make-hash-table))
+(define returned-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)))))))
+
+  (set! clear-mob-data
+       (lambda ()
+         (hash-clear! published-data)
+         (hash-clear! returned-data)))
+
+  (set! def-mobs-event
+       (lambda (pair fun)
+         (cond ((not fun)
+                (hash-remove! mobs-events pair))
+               (else
+                (hash-set! mobs-events pair fun)))))
+
+  (set! run-mobs-events
+       (lambda ()
+         (hash-for-each
+          (lambda (types fun)
+            (let ((t1 (hash-ref published-data (car types)))
+                  (t2 (hash-ref published-data (cadr types))))
+              (cond ((and t1 t2)
+                     (for-each
+                      (lambda (m1)
+                        (for-each
+                         (lambda (m2)
+                           (let ((res (catch #t
+                                             (lambda () (fun (cdr m1) (cdr m2)))
+                                             (lambda (key . args) #f))))
+                             (cond ((and (list? res) (>= (length res) 2))
+                                    (let ((id1 (car m1)) (id2 (car m2))
+                                          (r1 (car res)) (r2 (cadr res)))
+                                      (return-data id1 (cadr types) r1)
+                                      (return-data id2 (car types) r2)
+)))))
+                         t2))
+                      t1)))))
+          mobs-events)))
+
+  (set! return-data
+       (lambda (mob-id mob-type data)
+         (let* ((key (list mob-id mob-type))
+                (res (hash-ref returned-data key)))
+           (hash-set! returned-data key
+                      (cond (res (cons data res))
+                            (else (list data))))
+)))
+)
+
+(define-macro (define-mobs-event types-pair . body)
+  `(def-mobs-event
+     ',types-pair
+     ,(cond ((null? body) #f)
+           (else `(lambda (,(car types-pair) ,(cadr types-pair)) ,@body)))))