Parse cl-tiled CELL objects into alists

This commit is contained in:
Jaidyn Ann 2023-06-15 18:14:39 -05:00
parent 53f34b4e17
commit 9d7961c6ad

View File

@ -120,32 +120,36 @@ into an alist by their “chunk” on the map."
(defun tile-layer-chunks (layer &optional (chunks '())) (defun tile-layer-chunks (layer &optional (chunks '()))
"Given a Tiled tile-layer (that is, graphics of the map), parse it into an "Given a Tiled tile-layer (that is, graphics of the map), parse it into an
alist of Tiled cell chunks." alist of Tiled cell chunks."
(let ((cells (cl-tiled:layer-cells layer))) (let ((cells (mapcar #'tiled-cell->cell (cl-tiled:layer-cells layer))))
(collect-items-into-groups (collect-items-into-groups
(cl-tiled:layer-cells layer) cells
(lambda (cell) (lambda (cell)
(getf (world-coords->screen-coords (tiled-cell-world-coords cell)) (world-coords-chunk (getf cell :coords)))
:chunk))
:groups chunks))) :groups chunks)))
(defun tiled-cell->cell (tiled-cell)
(list :coords (list :x (cl-tiled:cell-column tiled-cell)
:y (cl-tiled:cell-row tiled-cell))
:char (tiled-tile-character (cl-tiled:cell-tile tiled-cell))))
(defun matrix-write-tiled-map-chunk (matrix map chunk (defun matrix-write-tiled-map-chunk (matrix map chunk
&key (chunk-width 72) (chunk-height 20)) &key (chunk-width 72) (chunk-height 20))
"Draw a maps specific chunk (by its ID) to the matrix." "Draw a maps specific chunk (by its ID) to the matrix."
(mapcar (lambda (cell) (mapcar (lambda (cell)
(matrix-write-tiled-cell matrix cell)) (matrix-write-cell matrix cell))
(cdr (assoc chunk (getf map :tiles))))) (cdr (assoc chunk (getf map :tiles)))))
(defun matrix-write-tiled-cell (matrix cell) (defun matrix-write-cell (matrix cell)
"Set a matrice's (2d array's) element corresponding to "Set a matrice's (2d array's) element corresponding to a cell; that is, an
a Tiled cell's character-value, using it's column and row." alist containing a character (:CHAR) and :X & :Y coordinates."
(let ((coords (world-coords->screen-coords (tiled-cell-world-coords cell)))) (let ((coords (world-coords->screen-coords (getf cell :coords))))
(setf (aref matrix (setf (aref matrix
(getf coords :y) (getf coords :y)
(getf coords :x)) (getf coords :x))
(tiled-tile-character (getf cell :char))))
(cl-tiled:cell-tile cell)))))
(defun tiled-tile-character (tile) (defun tiled-tile-character (tile)
@ -158,23 +162,18 @@ with 15 characters-per-line."
32))) 32)))
(defun tiled-cell-world-coords (cell) (defun cell-at-world-coords-p (map-chunks coords)
"Return the world coordinates of a cell." "Return whether or not there is a cell at the given coordinates."
(list :x (cl-tiled:cell-column cell) :y (cl-tiled:cell-row cell)))
(defun tiled-cell-at-world-coords-p (map-chunks coords)
"Return whether or not there is a Tiled cell at the given coordinates."
(let ((chunk (world-coords-chunk coords))) (let ((chunk (world-coords-chunk coords)))
(member 't (cdr (assoc chunk map-chunks)) (member 't (cdr (assoc chunk map-chunks))
:test (lambda (ignored cell) :test (lambda (ignored cell)
(plist= (tiled-cell-world-coords cell) coords))))) (plist= (getf cell :coords) coords)))))
(defun walkable-tile-p (map x y) (defun walkable-tile-p (map x y)
"Return whether or not the given coordinates on the map are traversable for an entity." "Return whether or not the given coordinates on the map are traversable for an entity."
(not (tiled-cell-at-world-coords-p (getf map :bump-map) (not (cell-at-world-coords-p (getf map :bump-map)
(list :x x :y y)))) (list :x x :y y))))
(defun world-coords->screen-coords (world-coords &key (chunk-width 72) (chunk-height 20)) (defun world-coords->screen-coords (world-coords &key (chunk-width 72) (chunk-height 20))