- `(show-mob-hash (lambda* (#:optional (option #f)) (,mob option))))))
-
-(define-macro (hide-mob mob)
- (cond ((list? mob)
- `(let ((m ,mob))
- (hide-mob-hash (m 'get-mob-id))))
- (else
- `(hide-mob-hash (,mob 'get-mob-id)))))
-
-(define current-mob-id #f)
-
-(define translate-mob translate)
-
-(define (get-current-mob-id)
- current-mob-id)
-
-(define* (run-mobs #:optional (mobs (get-active-mobs)))
- (let ((sorted-mobs (sort mobs (lambda (m1 m2) (< (m1 'get-z-index) (m2 'get-z-index))))))
- (for-each
- (lambda (m)
- (set! current-mob-id (m 'get-mob-id))
- (glmatrix-block (m)))
- sorted-mobs)
- (set! current-mob-id #f)))
-
-
-;;; Making mobs
-
-(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 mob-name init-data)
- `(let ((mob-id (gensym))
- (mob-z-index 0)
- (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-z-index)
- mob-z-index)
- ((get-type)
- (procedure-name ,mob-name))
- ((get-data)
- (save-data)
- saved-data)
- (else
- (cond ((keyword? option)
- (assoc-ref saved-data (keyword->symbol option)))
- (else
- (save-data)
- (let ((res (,mob-name mob-id mob-data)))
- (set! mob-z-index (car res))
- (set! mob-data (cadr res))))))))))
-
-(define-macro (define-mob-function attr . body)
- (let ((attr (map (lambda (a) (if (list? a) a (list a #f))) attr))
- (mob-id-symbol (gensym))
- (mob-id-z-index (gensym))
- (data-symbol (gensym)))
- `(lambda (,mob-id-symbol ,data-symbol)
- (let ((,mob-id-z-index 0))
- (define (kill-me)
- (hide-mob-hash ,mob-id-symbol))
- (define* (translate x y #:optional (z 0))
- (cond ((3d-mode?)
- (translate-mob x y z))
- (else
- (set! ,mob-id-z-index (+ ,mob-id-z-index z))
- (translate-mob x y))))
- (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 ,mob-id-z-index (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))
- (make-fun-symbol (gensym))
- (mob-fun-symbol (gensym))
- (params-symbol (gensym)))
- `(define (,name . ,params-symbol)
- (define ,make-fun-symbol
- (lambda* ,(if (null? attr) '() `(#:key ,@attr))
- (the-mob ,name (list ,@(map (lambda (a) `(cons ',(car a) ,(car a))) attr)))))
- (define ,mob-fun-symbol
- (define-mob-function ,attr ,@body))
- (cond ((or (null? ,params-symbol) (keyword? (car ,params-symbol)))
- (apply ,make-fun-symbol ,params-symbol))
- (else
- (apply ,mob-fun-symbol ,params-symbol))))))
-
-(define-macro (lambda-mob 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)
-
-(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)))