show-mob-hash
hide-mob-hash
hide-all-mobs
- get-mob-function-name)
+ 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)
+ lambda-mob
+ define-checking-mobs)
#:re-export (get-current-color
set-current-color
with-color
(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)
+ (set! current-mob-id (m 'get-mob-id))
(glmatrix-block (m)))
- mobs))
+ mobs)
+ (set! current-mob-id #f))
;;; Making mobs
(hash-set! mob-functions mob-name name)))
name))
-(define-macro (the-mob type attr publish fun-name)
- (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)
- (,fun-name 123)))))))
+(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)))))
+ (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 (cdr head)))
- `(define (,fun-name data)
- (catch #t
- (lambda* () ,@body)
- (lambda (key . args) #f))))
+ (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)))
+ `(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))
(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 () ,attr ,fun-name))))))
+ (the-mob ',name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)) ,fun-name))))))
(define-macro (lambda-mob attr . body)
(let ((fun-name (gensym)))
`(begin
(define-mob-function ,(cons fun-name attr) ,@body)
- (the-mob 'undefined ,attr '() ,fun-name))))
+ (the-mob 'undefined '() ,fun-name))))
-;;; Collisions
+;;; Functions for checking mobs (collisions and more)
-;; (define-macro (lambda-mob-data attr . body)
-;; `(lambda ,attr ,@body))
+(define (map-mobs fun type)
+ (let ((mobs (filter (lambda (m) (and (eq? (m 'get-type) type) (not (eq? (m 'get-mob-id) (get-current-mob-id))))) (get-active-mobs))))
+ (map (lambda (m) (fun (m 'get-data))) mobs)))
-;; (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)))
+(define-macro (define-checking-mobs head mob-def . body)
+ (let ((type (car mob-def)) (attr (cdr mob-def)))
+ `(define ,head
+ (map-mobs
+ (lambda (m)
+ (let ,(map (lambda (a) `(,(car a) (assoc-ref m ',(cadr a)))) attr)
+ ,@body))
+ ',type))))