]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela_mobs.scm
(no commit message)
[gacela.git] / src / gacela_mobs.scm
index de76938d5f14d41e5f2c802897b286d7896105b1..c0507f5af6eb529a4a61a44f6fe1ac060cc8cb5a 100755 (executable)
 
 ;;; Mobs Factory
 
-(define add-mob-lambda #f)
-(define kill-mob-symbol #f)
+(define show-mob-hash #f)
+(define hide-mob-hash #f)
 (define get-active-mobs #f)
 (define mobs-changed? #f)
 
-(let ((active-mobs '()) (changed #f))
-  (set! add-mob-lambda
-       (lambda (mob)
-         (pushnew mob active-mobs)
+(let ((active-mobs (make-hash-table)) (changed #f))
+  (set! show-mob-hash
+       (lambda (key mob)
+         (hash-set! active-mobs key mob)
          (set! changed #t)))
 
-  (set! kill-mob-symbol
-       (lambda (mob)
-         (set! active-mobs (lset-difference eq? active-mobs (list mob)))
+  (set! hide-mob-hash
+       (lambda (key)
+         (hash-remove! key)
          (set! changed #t)))
 
   (set! get-active-mobs
        (lambda* (#:optional (refreshed #t))
          (set! changed (not refreshed))
-         active-mobs))
+         (hash-map->list (lambda (k v) v) active-mobs)))
 
   (set! mobs-changed?
        (lambda () changed)))
 
 
-(define-macro (add-mob mob)
-  `(add-mob-lambda (lambda (option) (,mob option))))
+(define-macro (show-mob mob)
+  `(show-mob-hash ',mob (lambda (option) (,mob option))))
 
-(define-macro (kill-mob mob)
-  `(kill-mob-symbol ',mob))
+(define-macro (hide-mob mob)
+  `(hide-mob-hash ',mob))
 
 (define (process-mobs mobs)
   (for-each (lambda (m) (m #:render)) mobs))
 
+
+;;; Actions and looks for mobs
+
+(defmacro make-behaviour (name attr &rest code)
+  `(defun ,(get-behaviour-fun-name name) (object-attr)
+     (let ,(mapcar #'attribute-definition attr)
+       ,@code
+       ,(cons 'progn (mapcar #'attribute-save (reverse attr)))
+       object-attr)))
+
+(defun get-behaviour-fun-name (name)
+  (intern (concatenate 'string "BEHAVIOUR-" (string-upcase (string name))) 'gacela))
+
+(defun attribute-name (attribute)
+  (intern (string attribute) 'keyword))
+
+(define (attribute-definition attribute)
+  (let* ((name (cond ((list? attribute) (car attribute))
+                    (else attribute)))
+        (pname (attribute-name name))
+        (value (cond ((listp attribute) (cadr attribute)))))
+    `(,name (getf object-attr ,pname ,value))))
+
+(defun attribute-save (attribute)
+  (let* ((name (cond ((listp attribute) (car attribute))
+                    (t attribute)))
+        (pname (attribute-name name)))
+    `(setf (getf object-attr ,pname) ,name)))
+
+(define-macro (lambda-look . 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 ()
+                 (glPushMatrix)
+                 ,@look-lines
+                 (glPopMatrix)))))
+
+
+;;; Making mobs
+
 (define-macro (define-mob mob-head . look)
   (let ((name (car mob-head)) (attr (cdr mob-head)))
-    `(begin
-       (define ,name #f)
-       (let ((attr ',attr))
-        (set! ,name
-              (lambda (option)
-                (case option
-                  ((#:render)
-                   (glPushMatrix)
-                   ,@(map (lambda (x) (if (string? x) `(draw-image ,x) x)) look)
-                   (glPopMatrix)))))))))
+    `(define ,name
+       (lambda-mob ,attr ,@look))))
+
+(define-macro (lambda-mob attr . look)
+  `(let ((mob #f))
+     (set! mob
+          (let ((attr ',attr) (actions '()) (renders '()))
+            (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-renders)
+                 renders)
+                ((set-renders)
+                 (if (not (null? params)) (set! renders (car params))))))))
+     (cond ((not (null? ',look))
+           (display ',look)
+           (newline)))
+     mob))