(define show-mob-hash #f)
(define hide-mob-hash #f)
+(define refresh-active-mobs #f)
(define get-active-mobs #f)
-(define clear-active-mobs #f)
+(define hide-all-mobs #f)
(define mobs-changed? #f)
-(let ((active-mobs (make-hash-table)) (changed #f))
+(let ((mobs-table (make-hash-table))
+ (active-mobs '())
+ (changed #f))
+
(set! show-mob-hash
- (lambda (key mob)
- (hash-set! active-mobs key mob)
+ (lambda (mob)
+ (hash-set! mobs-table (mob 'get-mob-id) mob)
(set! changed #t)))
(set! hide-mob-hash
- (lambda (key)
- (hash-remove! key)
+ (lambda (mob-id)
+ (hash-remove! mobs-table mob-id)
(set! changed #t)))
+ (set! refresh-active-mobs
+ (lambda ()
+ (cond (changed
+ (set! changed #f)
+ (set! active-mobs (hash-map->list (lambda (k v) v) mobs-table))))))
+
(set! get-active-mobs
- (lambda* (#:optional (refreshed #t))
- (set! changed (not refreshed))
- (hash-map->list (lambda (k v) v) active-mobs)))
+ (lambda () active-mobs))
- (set! clear-active-mobs
+ (set! hide-all-mobs
(lambda ()
(set! changed #t)
- (hash-clear! active-mobs)))
+ (hash-clear! mobs-table)))
(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)))
+ (else
+ `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
(define-macro (hide-mob mob)
- `(hide-mob-hash ',mob))
-
-(define (run-mob-actions mobs)
- (for-each (lambda (m) (m 'run-actions)) mobs))
-
-(define (render-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))))
-
-(define (attr-save attr)
- (let ((name (car attr)))
- `(set! attributes (assoc-set! attributes ',name (list ,name)))))
-
-(define-macro (define-action action-head . code)
- (let ((name (car action-head)) (attr (cdr action-head)))
- `(define ,name
- (lambda-action ,attr ,@code))))
-
-(define-macro (lambda-action attr . code)
- `(lambda (attributes)
- (let ,(map attr-def attr)
- ,@code
- ,(cons 'begin (map attr-save attr))
- attributes)))
-
-(define-macro (lambda-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))))))
+ (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 . 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 '()) (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 '()) (action #f) (look #f))
- `(let ((attr ,attr) (action ,action) (look ,look))
- (lambda (option . params)
- (case option
- ((get-attr)
- attr)
- ((set-attr)
- (if (not (null? params)) (set! attr (car params))))
- ((get-action)
- action)
- ((set-action)
- (if (not (null? params)) (set! action (car params))))
- ((get-look)
- look)
- ((set-look)
- (if (not (null? params)) (set! look (car params))))
- ((run-mob)
- (lambda (action)
- (set! attr ((cdr action) attr)))
- actions))
- ((render)
- (for-each
- (lambda (look)
- ((cdr look) attr))
- looks))))))
-
-
-(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 (add-mob-action mob name action)
- (mob 'set-actions (assoc-set! (mob 'get-actions) name action)))
-
-(define (quit-mob-action mob name)
- (mob 'set-actions (assoc-remove! (mob 'get-actions) name)))
-
-(define (add-mob-look mob name look)
- (mob 'set-looks (assoc-set! (mob 'get-looks) name look)))
-
-(define (quit-mob-look mob name)
- (mob 'set-looks (assoc-remove! (mob 'get-looks) name)))
+ `(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))
+
+(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)))