]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela.scm
Making collisions support
[gacela.git] / src / gacela.scm
index 83e7275e6dd316d2f5d88530a4c58d5e999a9651..6c109dac4baa0091ea8657547b159f5217726fc4 100644 (file)
            quit-gacela
            game-loop
            game-running?
-           set-game-code)
-  #:export-syntax (game)
+           set-game-code
+           show-mob-hash
+           hide-mob-hash
+           hide-all-mobs
+           get-current-mob-id
+           get-mob-function-name
+           map-mobs)
+  #:export-syntax (game
+                  show-mob
+                  hide-mob
+                  the-mob
+                  define-mob-function
+                  define-mob
+                  lambda-mob
+                  define-checking-mobs)
   #:re-export (get-current-color
               set-current-color
               with-color
@@ -53,7 +66,8 @@
               add-light
               set-camera
               camera-look
-              render-text))
+              render-text
+              get-frame-time))
 
 
 ;;; Resources Cache
   (set! loop-flag #f))
 
 (define (game-loop)
-;        (refresh-active-mobs)
+  (refresh-active-mobs)
   (set! loop-flag #t)
   (init-video *width-screen* *height-screen* *bpp-screen* #:title *title* #:mode *mode* #:fps *frames-per-second*)
   (while loop-flag
               (else
                (clear-screen)
                (to-origin)
-;                 (refresh-active-mobs)
+               (refresh-active-mobs)
                (if (procedure? game-code)
                    (catch #t
                           (lambda () (game-code))
                           (lambda (key . args) #f)))
-;                        (run-mobs)
+               (run-mobs)
                (flip-screen)
                (delay-frame))))
   (quit-video))
 
 (define mobs-table (make-hash-table))
 (define active-mobs '())
-(define changed #f))
+(define mobs-changed #f)
 
 (define (show-mob-hash mob)
   (hash-set! mobs-table (mob 'get-mob-id) mob)
-  (set! changed #t))
+  (set! mobs-changed #t))
 
 (define (hide-mob-hash mob-id)
   (hash-remove! mobs-table mob-id)
-  (set! changed #t))
+  (set! mobs-changed #t))
 
 (define (refresh-active-mobs)
-  (cond (changed
-        (set! changed #f)
+  (cond (mobs-changed
+        (set! mobs-changed #f)
         (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table)))))
 
 (define (get-active-mobs)
   active-mobs)
 
 (define (hide-all-mobs)
-  (set! changed #t)
+  (set! mobs-changed #t)
   (hash-clear! mobs-table))
 
 (define (mobs-changed?)
-  changed)
+  mobs-changed)
 
 
 (define-macro (show-mob mob)
        (else
         `(hide-mob-hash (,mob 'get-mob-id)))))
 
+(define current-mob-id #f)
+
+(define (get-current-mob-id)
+  current-mob-id)
+
 (define* (run-mobs #:optional (mobs (get-active-mobs)))
   (for-each
    (lambda (m)
-     (glPushMatrix)
-     (m)
-     (glPopMatrix))
-   mobs))
+     (set! current-mob-id (m 'get-mob-id))
+     (glmatrix-block (m)))
+   mobs)
+  (set! current-mob-id #f))
 
 
 ;;; 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))
+(define mob-functions (make-hash-table))
+
+(define (get-mob-function-name mob-name)
+  (let ((name (hash-ref mob-functions mob-name)))
+    (cond ((not name)
+          (set! name (gensym))
+          (hash-set! mob-functions mob-name name)))
+    name))
+
+(define-macro (the-mob type init-data fun-name)
+  `(let ((mob-id (gensym))
+        (mob-time 0)
+        (mob-data ,init-data)
+        (saved-data ,init-data))
+     (lambda* (#:optional (option #f))
+       (define (save-data)
+        (let ((time (get-frame-time)))
+          (cond ((not (= time mob-time))
+                 (set! mob-time time)
+                 (set! saved-data mob-data)))))
+;       (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)
+        ((get-type)
+         ,type)
+        ((get-data)
+         (save-data)
+         saved-data)
+        (else
+         (save-data)
+         (set! mob-data (,fun-name mob-id mob-data)))))))
+
+(define-macro (define-mob-function head . body)
+  (let ((fun-name (car head))
+       (attr (map (lambda (a) (if (list? a) a (list a #f))) (cdr head)))
+       (mob-id-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 (,fun-name ,mob-id-symbol ,data-symbol)
+       (define (kill-me)
+        (hide-mob-hash ,mob-id-symbol))
+       (let ,attr
+        ,@(map
+           (lambda (a)
+             `(let ((val (assoc-ref ,data-symbol ',(car a))))
+                (cond (val (set! ,(car a) val)))))
+           attr)
+        (catch #t
+               (lambda* () ,@body)
+               (lambda (key . args) #f))
+        (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr))))))
+
+(define-macro (define-mob mob-head . body)
+  (let* ((name (car mob-head)) (attr (cdr mob-head))
+        (fun-name (get-mob-function-name name)))
+    `(begin
+       (define-mob-function ,(cons fun-name attr) ,@body)
+       (define ,(string->symbol (string-concatenate (list "make-" (symbol->string name))))
+        (lambda* ,(if (null? attr) '() `(#:key ,@attr))
+          (the-mob ',name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)) ,fun-name))))))
 
 (define-macro (lambda-mob attr . body)
-  `(the-mob 'undefined ,attr '() ,@body))
+  (let ((fun-name (gensym)))
+    `(begin
+       (define-mob-function ,(cons fun-name attr) ,@body)
+       (the-mob 'undefined '() ,fun-name))))
+
 
+;;; Functions for checking mobs (collisions and more)
 
-;;; Collisions
+(define (map-mobs fun)
+  (let ((mobs (filter (lambda (m) (not (eq? (m 'get-mob-id) (get-current-mob-id)))) (get-active-mobs))))
+    (map fun mobs)))
 
+(define-macro (define-checking-mobs head mob-attr . body)
+  `(define ,head
+     
 ;; (define-macro (lambda-mob-data attr . body)
 ;;   `(lambda ,attr ,@body))