]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela_mobs.scm
Working with cllisions.
[gacela.git] / src / gacela_mobs.scm
index d273556d51de2247e9259141cc38a1c947a50ac0..a0d2bf7cda4def0b7dbd78b7c4e31c258a64b5c7 100755 (executable)
 
 ;;; Mobs Factory
 
-(define-macro (hash-add-mob hash-table mob)
-  `(hash-set! ,hash-table (procedure-name ,mob) (lambda () (,mob))))
+(define show-mob-hash #f)
+(define hide-mob-hash #f)
+(define refresh-active-mobs #f)
+(define get-active-mobs #f)
+(define hide-all-mobs #f)
+(define mobs-changed? #f)
 
-(define-macro (hash-remove-mob hash-table mob)
-  `(hash-remove! ,hash-table (procedure-name ,mob)))
+(let ((mobs-table (make-hash-table))
+      (active-mobs '())
+      (changed #f))
 
+  (set! show-mob-hash
+       (lambda (mob)
+         (hash-set! mobs-table (mob 'get-mob-id) mob)
+         (set! changed #t)))
 
-(define add-mob #f)
-(define kill-mob #f)
-(define get-mob-world #f)
-(define mobs #f)
-(define vreload #f)
+  (set! hide-mob-hash
+       (lambda (mob-id)
+         (hash-remove! mobs-table mob-id)
+         (set! changed #t)))
 
-(let ((active-mobs (make-hash-table)) (reload #f))
-  (set! add-mob
-       (lambda (mob)
-         (hash-add-mob active-mobs mob)
-         (set! reload #t)))
+  (set! refresh-active-mobs
+       (lambda ()
+         (cond (changed
+                (set! changed #f)
+                (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table))))))
 
-  (set! kill-mob
-       (lambda (mob)
-         (hash-remove-mob active-mobs mob)
-         (set! reload #t)))
+  (set! get-active-mobs
+       (lambda () active-mobs))
 
-  (set! get-mob-world
+  (set! hide-all-mobs
        (lambda ()
-         (cond (reload
-                (set! reload #f)
-                (hash-map->list (lambda (k v) v) active-mobs))
-               (else #f))))
+         (set! changed #t)
+         (hash-clear! mobs-table)))
+
+  (set! mobs-changed?
+       (lambda () changed)))
+
+
+(define-macro (show-mob mob)
+  (cond ((list? mob)
+        `(let ((m ,mob))
+           (show-mob-hash m)))
+       (else
+        `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
+
+(define-macro (hide-mob 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 #:optional (mobs (get-active-mobs)))
+  (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* ,(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))
+       (time-symbol (gensym))
+       (data-symbol (gensym)))
+    `(let ((,mob-id-symbol (gensym))
+          (,type-symbol ,type)
+          (,time-symbol 0)
+          (,data-symbol '())
+          ,@attr)
+       (lambda* (#:optional (option #f))
+        (define (kill-me)
+          (hide-mob-hash ,mob-id-symbol))
+        (define (save-data)
+          (let ((time (get-frame-time)))
+            (cond ((not (= time ,time-symbol))
+                   (set! ,time-symbol time)
+                   (set! ,data-symbol ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish)))))))
+        (define (get-data)
+          ,data-symbol)
+        (define (filter-mobs type fun)
+          #t)
+        (define (map-mobs fun type)
+          (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) ,mob-id-symbol)))) (get-active-mobs))))
+            (map (lambda (m) (fun (m 'get-data))) mobs)))
+        (case option
+          ((get-mob-id)
+           ,mob-id-symbol)
+          ((get-type)
+           ,type-symbol)
+          ((get-data)
+           (save-data)
+           ,data-symbol)
+          (else
+           (save-data)
+           (catch #t
+                  (lambda () ,@body)
+                  (lambda (key . args) #f))))))))
+
+(define-macro (lambda-mob attr . body)
+  `(the-mob 'undefined ,attr '() ,@body))
+
+
+;;; Collisions
+
+(define-macro (lambda-mob-data attr . body)
+  `(lambda ,attr ,@body))
 
-  (set! mobs (lambda () active-mobs))
-  (set! vreload (lambda () reload)))
+(define-macro (define-collision-check name mobs . body)
+  `(defmacro* ,name (#:optional m)
+     `(let ,(cond (m `((mob-id (,m 'get-mob-id)) (mob-type (,m 'get-type))))
+                 (else `()))
+       
+       mob-id)))