X-Git-Url: https://git.jsancho.org/?a=blobdiff_plain;f=gacela_mobs.lisp;h=0b05c5d97a7379ce1e05b5339e00447a6ec15deb;hb=8b8cbb5b5b97e337dd6790aa7959a2dd68853e1b;hp=558a1b686c7b79e4d59d776442ccbe2189389349;hpb=678a1561ca858c34fecf04792831caf68559c2ac;p=gacela.git diff --git a/gacela_mobs.lisp b/gacela_mobs.lisp index 558a1b6..0b05c5d 100755 --- a/gacela_mobs.lisp +++ b/gacela_mobs.lisp @@ -15,19 +15,20 @@ ;;; along with this program. If not, see . -(in-package :gacela) - ;;; World of Mob -(defmacro defmob (name variables &key init logic render) - `(let ((make-name ',(intern (concatenate 'string "MAKE-" (string name))))) - (setf (symbol-function make-name) - (makemob ,variables :init ,init :logic ,logic :render ,render)) - make-name)) -(defmacro makemob (variables &key init logic render) - `(lambda - ,(if (null variables) () (cons '&key variables)) - (mob-structure ,variables ,init ,logic ,render))) +(in-package :gacela) + +(defmacro makemob (name &rest methods) + `(defun ,name (&rest args &aux (option (car args))) + ,(union + `(case option + (:on (mob-on #',name)) + (:off (mob-off #',name))) + (labels ((options (m &aux (option (car m)) (body (cadr m))) + (cond ((null m) nil) + (t (cons (list option `(apply ,body (cdr args))) (options (cddr m))))))) + (options methods))))) (defmacro mob-structure (variables init logic render) `(list @@ -42,18 +43,24 @@ (values-list ,(cons 'list (mapcar #'car+ variables))))) nil)))) -(defun init-mob (mob) - (funcall (getf mob :init))) - -(defun logic-mob (mob) - (funcall (getf mob :logic))) - -(defun render-mob (mob) - (funcall (getf mob :render))) - (let (running-mobs mobs-to-add mobs-to-quit) (defun mob-on (mob) (push mob mobs-to-add)) + (defun logic-mobs () + (dolist (mob running-mobs) (funcall mob :logic))) + + (defun render-mobs () + (dolist (mob running-mobs) (funcall mob :render))) + (defun mob-off (mob) - (push mob mobs-to-quit))) + (push mob mobs-to-quit)) + + (defun refresh-running-mobs () + (do ((mob (pop mobs-to-add) (pop mobs-to-add))) ((null mob)) + (push mob running-mobs) + (funcall mob :init)) + (setq running-mobs (reverse (set-difference running-mobs mobs-to-quit :test #'equal)))) + + (defun quit-all-mobs () + (setq running-mobs nil mobs-to-add nil mobs-to-quit nil)))