+(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))