Support for events triggered by moving to point

That is, a new “Trigger” Tiled object-type has
been made, which is just a rectangle with an
associated function. If the rectangle is stepped
upon (uwu step on me owo), the function is
triggered. Y’know, standard RPG stuffs.
This commit is contained in:
Jaidyn Ann 2023-06-24 22:17:42 -05:00
parent 1c2a371b38
commit b61739aa24
4 changed files with 114 additions and 38 deletions

View File

@ -187,6 +187,10 @@
:face "=w=~")))) :face "=w=~"))))
(defun bruh-trigger (&optional map)
(print "YAAAAAAA"))
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
;;; Destitute Gambler arc ;;; Destitute Gambler arc

View File

@ -30,6 +30,19 @@
(in-package :flora-search-aurora.overworld) (in-package :flora-search-aurora.overworld)
;;; ———————————————————————————————————
;;; Misc. Utils
;;; ———————————————————————————————————
(defun within-rectangle (point top-left-corner bottom-right-corner)
"With three coordinate plists, determine whether or not POINT resides within a
rectangle as defined by its TOP-LEFT-CORNER & BOTTOM-RIGHT-CORNER."
(and (<= (getf point :x) (getf bottom-right-corner :x))
(>= (getf point :x) (getf top-left-corner :x))
(<= (getf point :y) (getf bottom-right-corner :y))
(>= (getf point :y) (getf top-left-corner :y))))
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
;;; Accessors ;;; Accessors
@ -72,6 +85,7 @@
entities entities
:y-radius 4))) :y-radius 4)))
(defun cell-at-world-coords-p (map-chunks coords) (defun cell-at-world-coords-p (map-chunks coords)
"Return whether or not there is a cell at the given coordinates." "Return whether or not there is a cell at the given coordinates."
(let ((chunk (world-coords-chunk coords))) (let ((chunk (world-coords-chunk coords)))
@ -141,6 +155,14 @@ Returns parameters to be used in the next invocation of OVERWORLD-STATE."
:y (+ y (getf coords :y))))) :y (+ y (getf coords :y)))))
(defun trigger-at-coords (map world-coords)
(let ((chunk (world-coords-chunk world-coords)))
(loop for trigger in (cdr (assoc chunk (gethash :triggers map)))
do (when (within-rectangle world-coords
(getf trigger :coords) (getf trigger :bottom-coords))
(return trigger)))))
(defun move-entity-to (map entity &key (x 0) (y 0)) (defun move-entity-to (map entity &key (x 0) (y 0))
"Move the given entity to the given coordinates." "Move the given entity to the given coordinates."
(let ((old-chunk (world-coords-chunk (getf-entity-data map entity :coords))) (let ((old-chunk (world-coords-chunk (getf-entity-data map entity :coords)))
@ -157,7 +179,13 @@ Returns parameters to be used in the next invocation of OVERWORLD-STATE."
(cdr (getf-entity map entity))) (cdr (getf-entity map entity)))
;; Delete it from the old list… ;; Delete it from the old list…
(alexandria:deletef (assoc-utils:aget (gethash :entities map) old-chunk) entity (alexandria:deletef (assoc-utils:aget (gethash :entities map) old-chunk) entity
:test (lambda (id alist) (eq id (car alist))))))) :test (lambda (id alist) (eq id (car alist)))))
;; If moving the player-character, check for triggers! (Traps)
(when (eq entity 'player)
(let ((trigger (trigger-at-coords map (list :x x :y y))))
(if (and trigger (getf trigger :function))
(apply (intern (string-upcase (getf trigger :function)))
(list map)))))))

View File

@ -30,67 +30,94 @@
;;; Misc. utility ;;; Misc. utility
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
(defun collect-items-into-groups (list key-function &key (groups '())) (defun collect-items-into-groups (list key-function &key (groups '()))
"Given a LIST of items and a function categorizing an individual item "Given a LIST of items and a KEY-FUNCTION categorizing an individual item
(returning a category symbol for any given item), return an sorted (returning a category symbol for any given item), return an sorted
associative list." associative list built upon GROUPS.
If NIL is returned from KEY-FUNCTION, the given item is thrown out."
(loop for item in list (loop for item in list
do (let ((key (apply key-function (list item)))) do (let ((key (apply key-function (list item))))
(when key
(setf (assoc-utils:aget groups key) (setf (assoc-utils:aget groups key)
(append (assoc-utils:aget groups key) (append (assoc-utils:aget groups key)
(list item))))) (list item))))))
groups) groups)
(defun tiled-coords->world-coords (x y tiled-map)
"Given X & Y coordinates with a parsed Tiled map, return the appropriate
character-scale world coordinates in plist form."
(list :x (floor (/ x (cl-tiled:map-tile-width tiled-map)))
:y (floor (/ y (cl-tiled:map-tile-height tiled-map)))))
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
;;; Tile-layer parsing (graphics) ;;; Object-layer (Persons/Triggers)
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
(defun tiled-cell->cell (tiled-cell) (defun tiled-rectangle-p (tiled-obj)
"Convert a Tiled cell into a cell plist." "Whether or not a Tiled object is a valid rectangle."
(list :coords (list :x (cl-tiled:cell-column tiled-cell) (and (> (cl-tiled:rect-width tiled-obj) 0)
:y (cl-tiled:cell-row tiled-cell)) (> (cl-tiled:rect-height tiled-obj) 0)))
:char (tile-character (cl-tiled:cell-tile tiled-cell))))
(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-object->entity (tiled-obj tiled-map) (defun tiled-object->entity (tiled-obj tiled-map)
"Convert a Tiled object into an entity plist." "Convert a Tiled object into an entity plist."
(let ((properties (cl-tiled:properties tiled-obj))) (let ((properties (cl-tiled:properties tiled-obj)))
(when (not (tiled-rectangle-p tiled-obj))
(list (intern (string-upcase (gethash "id" properties))) (list (intern (string-upcase (gethash "id" properties)))
:coords (list :x (floor (/ (cl-tiled:object-x tiled-obj) :coords (tiled-coords->world-coords (cl-tiled:object-x tiled-obj)
(cl-tiled:map-tile-width tiled-map))) (cl-tiled:object-y tiled-obj)
:y (floor (/ (cl-tiled:object-y tiled-obj) tiled-map)
(cl-tiled:map-tile-height tiled-map))))
:face (gethash "normal_face" properties) :face (gethash "normal_face" properties)
:normal-face (gethash "normal_face" properties) :normal-face (gethash "normal_face" properties)
:talking-face (gethash "talking_face" properties) :talking-face (gethash "talking_face" properties)
:interact (gethash "interact" properties) :interact (gethash "interact" properties)
:direction (if (gethash "facing_right" properties) :direction (if (gethash "facing_right" properties)
'right 'right
'left)))) 'left)))))
(defun tiled-object->trigger (tiled-obj tiled-map)
"Convert a Tiled object into a trigger plist. That is, a rectangle with
a :FUNCTION to be triggered when its stepped upon."
(when (tiled-rectangle-p tiled-obj)
(let ((obj-x (cl-tiled:object-x tiled-obj))
(obj-y (cl-tiled:object-y tiled-obj))
(obj-width (cl-tiled:rect-width tiled-obj))
(obj-height (cl-tiled:rect-height tiled-obj)))
(list
:coords (tiled-coords->world-coords obj-x obj-y tiled-map)
:width obj-width
:height obj-height
:bottom-coords (tiled-coords->world-coords (+ obj-x obj-width) (+ obj-y obj-height) tiled-map)
:function (gethash "function" (cl-tiled:properties tiled-obj))))))
(defun object-layer-entities (layer &optional (entity-chunks '())) (defun object-layer-entities (layer &optional (entity-chunks '()))
"Convert all objects in an object layer into entity plists." "Convert all point objects in an object layer into entity plists."
(let ((entities (mapcar (lambda (object) (tiled-object->entity object (cl-tiled:layer-map layer))) (let ((entities (mapcar (lambda (object) (tiled-object->entity object (cl-tiled:layer-map layer)))
(layer-objects layer)))) (layer-objects layer))))
(collect-items-into-groups (collect-items-into-groups
entities entities
(lambda (entity) (lambda (entity)
(world-coords-chunk (getf (cdr entity) :coords))) (when entity
(world-coords-chunk (getf (cdr entity) :coords))))
:groups entity-chunks))) :groups entity-chunks)))
(defun object-layer-triggers (layer &optional (trigger-chunks '()))
"Convert all rectangle objects in an object layer into trigger plists."
(let ((triggers (mapcar (lambda (object) (tiled-object->trigger object (cl-tiled:layer-map layer)))
(layer-objects layer))))
(collect-items-into-groups
triggers
(lambda (trigger)
(when trigger
(world-coords-chunk (getf trigger :coords))))
:groups trigger-chunks)))
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
;;; Tile-layer parsing (graphics) ;;; Tile-layer parsing (graphics)
@ -151,6 +178,7 @@ with 15 characters-per-line."
(top-tiles '()) (top-tiles '())
(bump-map '()) (bump-map '())
(entities '()) (entities '())
(triggers '())
(hash (make-hash-table))) (hash (make-hash-table)))
(mapcar (lambda (layer) (mapcar (lambda (layer)
(typecase layer (typecase layer
@ -162,10 +190,12 @@ with 15 characters-per-line."
(setf top-tiles (tile-layer-chunks layer top-tiles)) (setf top-tiles (tile-layer-chunks layer top-tiles))
(setf tile-chunks (tile-layer-chunks layer tile-chunks)))) (setf tile-chunks (tile-layer-chunks layer tile-chunks))))
(cl-tiled.data-types:object-layer (cl-tiled.data-types:object-layer
(setf triggers (object-layer-triggers layer triggers))
(setf entities (object-layer-entities layer entities))))) (setf entities (object-layer-entities layer entities)))))
(cl-tiled:map-layers (cl-tiled:load-map map-file))) (cl-tiled:map-layers (cl-tiled:load-map map-file)))
(setf (gethash :tiles hash) tile-chunks) (setf (gethash :tiles hash) tile-chunks)
(setf (gethash :top-tiles hash) top-tiles) (setf (gethash :top-tiles hash) top-tiles)
(setf (gethash :bump-map hash) bump-map) (setf (gethash :bump-map hash) bump-map)
(setf (gethash :entities hash) entities) (setf (gethash :entities hash) entities)
(setf (gethash :triggers hash) triggers)
hash)) hash))

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<map version="1.10" tiledversion="1.10.1" orientation="orthogonal" renderorder="left-down" width="72" height="41" tilewidth="17" tileheight="17" infinite="0" nextlayerid="7" nextobjectid="6"> <map version="1.10" tiledversion="1.10.1" orientation="orthogonal" renderorder="left-down" width="72" height="41" tilewidth="17" tileheight="17" infinite="0" nextlayerid="7" nextobjectid="10">
<tileset firstgid="1" source="font.tsx"/> <tileset firstgid="1" source="font.tsx"/>
<layer id="4" name="Background" width="72" height="41"> <layer id="4" name="Background" width="72" height="41">
<data encoding="csv"> <data encoding="csv">
@ -192,5 +192,19 @@
</properties> </properties>
<point/> <point/>
</object> </object>
<object id="6" name="Bartender" type="Person" x="110" y="128.667">
<properties>
<property name="facing_right" type="bool" value="false"/>
<property name="id" value="casino-bartender"/>
<property name="interact" value="casino-bartender-interact"/>
<property name="normal_face" value="uvu~"/>
</properties>
<point/>
</object>
<object id="8" name="BRUH" type="Trigger" x="233.333" y="240" width="78" height="47.3333">
<properties>
<property name="function" value="bruh-trigger"/>
</properties>
</object>
</objectgroup> </objectgroup>
</map> </map>