(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))
(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))
-
-(define (process-mobs mobs)
- (for-each (lambda (m) (m #:render)) mobs))
-
-
-;;; Actions 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))))
-
-;; (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)))))
+ (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))
;;; 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
- (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))
+ `(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
+ (display ,(cons 'list (map (lambda (x) `(cons ',(car x) ,(car x))) publish)))
+ (newline)
+ (catch #t
+ (lambda () ,@body)
+ (lambda (key . args) #f))))))))
+
+(define-macro (lambda-mob attr . body)
+ `(the-mob 'undefined ,attr '() ,@body))