Switch MAP to being a hash-table instead of a plist

Having MAP be a hash-table makes it a little more
mutable (friendly for a game of this structure.
Because, y’know, we… I’m too lazy to explain this.
Read the code. Which I’m pretty sure is illegible.
Sorry… =w=""""") :w:
>o< cries cries cires criesd
This commit is contained in:
Jaidyn Ann 2023-06-16 12:41:19 -05:00
parent c801a2ddfb
commit 0f512e80ea
3 changed files with 30 additions and 22 deletions

View File

@ -35,7 +35,9 @@
(defun talk (map)
(print "AAAAA"))
(print (gethash :mom map))
(setf (gethash :mom map) 3434))
(defun state-loop

View File

@ -43,7 +43,7 @@ A state-function for use with STATE-LOOP."
(defun overworld-state-draw (matrix map)
"Draw the overworld map to the given matrix.
A core part of OVERWORLD-STATE."
(let* ((player-data (cdr (assoc 'player (getf map :entities))))
(let* ((player-data (cdr (assoc 'player (gethash :entities map))))
(chunk (world-coords-chunk (getf player-data :coords))))
(matrix-write-map-chunk matrix map chunk)
(matrix-write-entities matrix map)))
@ -66,7 +66,8 @@ Returns parameters to be used in the next invocation of OVERWORLD-STATE."
(let* ((input (normalize-char-plist (read-char-plist))))
(cond
((plist= input '(:modifier nil :char #\return))
(let ((interact (getf (cdr (assoc 'player (getf map :entities))) :interact)))
(let ((interact (getf (cdr (assoc 'player (gethash :entities map)))
:interact)))
(if interact
(apply (intern interact) (list map)))))
((plist= input '(:modifier nil :char #\→))
@ -81,7 +82,7 @@ Returns parameters to be used in the next invocation of OVERWORLD-STATE."
(defun move-entity (map entity &key (x 0) (y 0))
"Move an entity relative to its current position."
(let ((entity-plist (cdr (assoc entity (getf map :entities)))))
(let ((entity-plist (cdr (assoc entity (gethash :entities map)))))
(when (< x 0)
(setf (getf entity-plist :direction) 'left))
(when (> x 0)
@ -93,7 +94,7 @@ Returns parameters to be used in the next invocation of OVERWORLD-STATE."
(defun move-entity-to (map entity &key (x 0) (y 0))
"Move the given entity to the given coordinates."
(let ((entity-plist (cdr (assoc entity (getf map :entities)))))
(let ((entity-plist (cdr (assoc entity (gethash :entities map)))))
(when (walkable-tile-p map x y)
(setf (getf (getf entity-plist :coords) :x) x)
(setf (getf (getf entity-plist :coords) :y) y))))
@ -106,9 +107,9 @@ Returns parameters to be used in the next invocation of OVERWORLD-STATE."
(defun matrix-write-map-chunk (matrix map chunk
&key (chunk-width 72) (chunk-height 20))
"Draw a maps specific chunk (by its ID) to the matrix."
(mapcar (lambda (cell)
(mapcar (lambda (cell)
(matrix-write-cell matrix cell))
(cdr (assoc chunk (getf map :tiles)))))
(cdr (assoc chunk (gethash :tiles map)))))
(defun matrix-write-cell (matrix cell)
@ -131,7 +132,7 @@ alist containing a character (:CHAR) and :X & :Y coordinates."
(defun walkable-tile-p (map x y)
"Return whether or not the given coordinates on the map are traversable for an entity."
(not (cell-at-world-coords-p (getf map :bump-map)
(not (cell-at-world-coords-p (gethash :bump-map map)
(list :x x :y y))))
@ -143,7 +144,7 @@ alist containing a character (:CHAR) and :X & :Y coordinates."
"Draw all entities from an alist of entities to the matrix."
(mapcar (lambda (entity-assoc)
(matrix-write-entity matrix (cdr entity-assoc)))
(getf map :entities)))
(gethash :entities map)))
(defun matrix-write-entity (matrix entity-plist)

View File

@ -35,7 +35,8 @@
:ENTITIES, a list of entity plists."
(let ((tile-chunks '())
(bump-map '())
(entities '()))
(entities '())
(hash (make-hash-table)))
(mapcar (lambda (layer)
(typecase layer
(cl-tiled.data-types:tile-layer
@ -45,18 +46,10 @@
(cl-tiled.data-types:object-layer
(setf entities (object-layer-entities layer entities)))))
(cl-tiled:map-layers (cl-tiled:load-map map-file)))
(list :tiles tile-chunks :bump-map bump-map :entities entities)))
(defun tile-layer-chunks (layer &optional (chunks '()))
"Given a Tiled tile-layer (that is, graphics of the map), parse it into an
alist of Tiled cell chunks."
(let ((cells (mapcar #'tiled-cell->cell (cl-tiled:layer-cells layer))))
(collect-items-into-groups
cells
(lambda (cell)
(world-coords-chunk (getf cell :coords)))
:groups chunks)))
(setf (gethash :tiles hash) tile-chunks)
(setf (gethash :bump-map hash) bump-map)
(setf (gethash :entities hash) entities)
hash))
(defun object-layer-entities (layer &optional (entities '()))
@ -79,11 +72,23 @@ alist of Tiled cell “chunks”."
:y (floor (/ (cl-tiled:object-y tiled-obj)
(cl-tiled:map-tile-height tiled-map))))
:face (gethash "face" properties #'string-equal)
:interact (gethash "interact" properties #'string-equal)
:direction (if (gethash "facing_right" properties #'string-equal)
'right
'left))))
(defun tile-layer-chunks (layer &optional (chunks '()))
"Given a Tiled tile-layer (that is, graphics of the map), parse it into an
alist of Tiled cell chunks."
(let ((cells (mapcar #'tiled-cell->cell (cl-tiled:layer-cells layer))))
(collect-items-into-groups
cells
(lambda (cell)
(world-coords-chunk (getf cell :coords)))
:groups chunks)))
(defun tiled-cell->cell (tiled-cell)
"Convert a Tiled cell into a cell plist."
(list :coords (list :x (cl-tiled:cell-column tiled-cell)