The situation is:
Code: Select all
(defclass square ()
((type :initarg :type :initform :wall :accessor square-type))) ; more slots will be added later
;; The default level
(defvar *level*)
(defclass level ()
((width :initarg :width :reader level-width)
(height :initarg :height :reader level-height)
(data :reader level-data)))
(defmethod initialize-instance :after ((l level) &key)
(with-slots (width height data)
(setf data (make-array (list width height)))
(loop for x from 0 below width do
(loop for y from 0 below height do
(setf (aref data x y) (make-instance 'square))))))
Code: Select all
(defclass square ()
((type :initarg :type :initform :wall))) ; without accessors
(defconstant +square-slots+ '(type))
(labels ((define-square-accessor (accessor-name slot-name)
(eval
`(defmacro ,accessor-name (x y &optional (level '*level*))
`(slot-value (aref (level-data ,level) ,x ,y) ',',slot-name)))))
(dolist (slot +square-slots+)
(define-square-accessor
(intern (concatenate 'string "SQUARE-" (string slot)))
slot)))
Is this code any good, or is there a better way?