Reorder overworld.tiled functions; overworld.util

No functional changes.
Re-order functions in overworld.tiled so SBCL
doesn't complain at me.
Split some functions from overworld.tiled into
a new overworld.util package. Also rename the
files in a nice way!
This commit is contained in:
Jaidyn Ann 2023-06-17 10:12:23 -05:00
parent d664996190
commit 6f60c76f4b
4 changed files with 139 additions and 82 deletions

View File

@ -22,7 +22,8 @@
(load "input.lisp") (load "input.lisp")
(load "display.lisp") (load "display.lisp")
(load "ui.lisp") (load "ui.lisp")
(load "tiled.lisp") (load "overworld.util.lisp")
(load "overworld.tiled.lisp")
(load "overworld.lisp") (load "overworld.lisp")
(defpackage :flora-search-aurora (defpackage :flora-search-aurora
@ -33,11 +34,12 @@
(in-package :flora-search-aurora) (in-package :flora-search-aurora)
(defun literary-girl-dialogue-2 (map)
(print "OWO"))
(defun literary-girl-dialogue (map) (defun literary-girl-dialogue (map)
(print "uwu")) (print "uwu")
(setf (getf-entity-data map 'literary-girl :interact) "literary-girl-dialogue-2"))
(defun state-loop (defun state-loop

View File

@ -18,11 +18,13 @@
;;;; the primary gameplay, the RPG-ish-ish bits). ;;;; the primary gameplay, the RPG-ish-ish bits).
(defpackage :flora-search-aurora.overworld (defpackage :flora-search-aurora.overworld
(:nicknames :fsa.o :overworld)
(:use :cl (:use :cl
:flora-search-aurora.input :flora-search-aurora.display :flora-search-aurora.input :flora-search-aurora.display :flora-search-aurora.ui
:flora-search-aurora.overworld.tiled :flora-search-aurora.overworld.tiled :flora-search-aurora.overworld.util)
:flora-search-aurora.ui) (:export #:overworld-state
(:export #:overworld-state :player)) #:getf-entity #:getf-entity-data
:player))
(in-package :flora-search-aurora.overworld) (in-package :flora-search-aurora.overworld)

View File

@ -13,43 +13,54 @@
;;;; You should have received a copy of the GNU General Public License ;;;; You should have received a copy of the GNU General Public License
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>. ;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;;; FLORA-SEARCH-AURORA.OVERWORLD ;;;; FLORA-SEARCH-AURORA.OVERWORLD.TILED
;;;; All game-functions and data relating to the “overworld” (that is, ;;;; Import a Tiled-format (TMX) map into the hash-table/plist/alist format
;;;; the primary gameplay, the RPG-ish-ish bits). ;;;; used by the overworld.
(defpackage :flora-search-aurora.overworld.tiled (defpackage :flora-search-aurora.overworld.tiled
(:use :cl) (:nicknames :fsa.o.t :overworld.tiled)
(:export #:load-map (:use :cl
#:world-coords-chunk #:world-coords->screen-coords)) :flora-search-aurora.overworld.util)
(:export #:load-map))
(in-package :flora-search-aurora.overworld.tiled) (in-package :flora-search-aurora.overworld.tiled)
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
;;; Tiled maps → Map lists ;;; Misc. utility
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
(defun load-map (map-file) (defun collect-items-into-groups (list key-function &key (groups '()))
"Parse a map-file into an plist of its data. This consists of: "Given a LIST of items and a function categorizing an individual item
:BUMP-MAP, an alist of tiles (keyed by chunk) in a collidable layer (returning a category symbol for any given item), return an sorted
:TILES, an alist of visible tiles (keyed by chunk). associative list."
:ENTITIES, a list of entity plists." (loop for item in list
(let ((tile-chunks '()) do (let ((key (apply key-function (list item))))
(bump-map '()) (setf (assoc-utils:aget groups key)
(entities '()) (append (assoc-utils:aget groups key)
(hash (make-hash-table))) (list item)))))
(mapcar (lambda (layer) groups)
(typecase layer
(cl-tiled.data-types:tile-layer
(when (gethash "colliding" (cl-tiled:properties layer) #'string-equal)
(setf bump-map (tile-layer-chunks layer bump-map))) ;;; ———————————————————————————————————
(setf tile-chunks (tile-layer-chunks layer tile-chunks))) ;;; Tile-layer parsing (graphics)
(cl-tiled.data-types:object-layer ;;; ———————————————————————————————————
(setf entities (object-layer-entities layer entities))))) (defun tiled-cell->cell (tiled-cell)
(cl-tiled:map-layers (cl-tiled:load-map map-file))) "Convert a Tiled cell into a cell plist."
(setf (gethash :tiles hash) tile-chunks) (list :coords (list :x (cl-tiled:cell-column tiled-cell)
(setf (gethash :bump-map hash) bump-map) :y (cl-tiled:cell-row tiled-cell))
(setf (gethash :entities hash) entities) :char (tile-character (cl-tiled:cell-tile tiled-cell))))
hash))
(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 object-layer-entities (layer &optional (entities '())) (defun object-layer-entities (layer &optional (entities '()))
@ -78,6 +89,17 @@
'left)))) 'left))))
;;; ———————————————————————————————————
;;; Tile-layer parsing (graphics)
;;; ———————————————————————————————————
(defun tiled-cell->cell (tiled-cell)
"Convert a Tiled cell into a cell plist."
(list :coords (list :x (cl-tiled:cell-column tiled-cell)
:y (cl-tiled:cell-row tiled-cell))
:char (tile-character (cl-tiled:cell-tile tiled-cell))))
(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."
@ -89,13 +111,6 @@ alist of Tiled cell “chunks”."
:groups chunks))) :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)
:y (cl-tiled:cell-row tiled-cell))
:char (tile-character (cl-tiled:cell-tile tiled-cell))))
(defun layer-objects (layer) (defun layer-objects (layer)
"Return all Tiled objects in the given object layer." "Return all Tiled objects in the given object layer."
(slot-value layer 'cl-tiled.data-types::objects)) (slot-value layer 'cl-tiled.data-types::objects))
@ -113,42 +128,27 @@ with 15 characters-per-line."
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
;;; Misc. utility ;;; Tiled maps → Map lists
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
(defun collect-items-into-groups (list key-function &key (groups '())) (defun load-map (map-file)
"Given a LIST of items and a function categorizing an individual item "Parse a map-file into an plist of its data. This consists of:
(returning a category symbol for any given item), return an sorted :BUMP-MAP, an alist of tiles (keyed by chunk) in a collidable layer
associative list." :TILES, an alist of visible tiles (keyed by chunk).
(loop for item in list :ENTITIES, a list of entity plists."
do (let ((key (apply key-function (list item)))) (let ((tile-chunks '())
(setf (assoc-utils:aget groups key) (bump-map '())
(append (assoc-utils:aget groups key) (entities '())
(list item))))) (hash (make-hash-table)))
groups) (mapcar (lambda (layer)
(typecase layer
(cl-tiled.data-types:tile-layer
(defun world-coords->screen-coords (world-coords &key (chunk-width 72) (chunk-height 20)) (when (gethash "colliding" (cl-tiled:properties layer) #'string-equal)
"Given a set of world coordinates, determine where this spot would be on the screen. (setf bump-map (tile-layer-chunks layer bump-map)))
The world is split into screen-sized chunks to this end. (setf tile-chunks (tile-layer-chunks layer tile-chunks)))
Chester P. Runk" (cl-tiled.data-types:object-layer
(let* ((chunk-x (floor (/ (getf world-coords :x) (setf entities (object-layer-entities layer entities)))))
chunk-width))) (cl-tiled:map-layers (cl-tiled:load-map map-file)))
(chunk-y (floor (/ (getf world-coords :y) (setf (gethash :tiles hash) tile-chunks)
chunk-height))) (setf (gethash :bump-map hash) bump-map)
(x (- (getf world-coords :x) (* chunk-x chunk-width))) (setf (gethash :entities hash) entities)
(y (- (getf world-coords :y) (* chunk-y chunk-height)))) hash))
(list :x x
:y y
:chunk (coords->symbol chunk-x chunk-y))))
(defun world-coords-chunk (coords)
(getf (world-coords->screen-coords coords) :chunk))
(defun coords->symbol (x y)
(intern (format nil "~A,~A" x y)))
(defun symbol->coords (coords-symbol)
(str:split #\, (symbol-name coords-symbol)))

53
overworld.util.lisp Normal file
View File

@ -0,0 +1,53 @@
;;;; Copyright © 2023, Jaidyn Ann <jadedctrl@posteo.at>
;;;;
;;;; This program is free software: you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License as
;;;; published by the Free Software Foundation, either version 3 of
;;;; the License, or (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;;; FLORA-SEARCH-AURORA.OVERWORLD.UTIL
;;;; Utility functions used by multiple overworld packages (overworld.tiled & overworld).
(defpackage :flora-search-aurora.overworld.util
(:nicknames :fsa.o.u :overworld.util)
(:use :cl)
(:export #:coords->symbol #:symbol->coords
#:world-coords->screen-coords
#:world-coords-chunk))
(in-package :flora-search-aurora.overworld.util)
(defun coords->symbol (x y)
(intern (format nil "~A,~A" x y)))
(defun symbol->coords (coords-symbol)
(str:split #\, (symbol-name coords-symbol)))
(defun world-coords->screen-coords (world-coords &key (chunk-width 72) (chunk-height 20))
"Given a set of world coordinates, determine where this spot would be on the screen.
The world is split into screen-sized chunks to this end.
Chester P. Runk"
(let* ((chunk-x (floor (/ (getf world-coords :x)
chunk-width)))
(chunk-y (floor (/ (getf world-coords :y)
chunk-height)))
(x (- (getf world-coords :x) (* chunk-x chunk-width)))
(y (- (getf world-coords :y) (* chunk-y chunk-height))))
(list :x x
:y y
:chunk (coords->symbol chunk-x chunk-y))))
(defun world-coords-chunk (coords)
(getf (world-coords->screen-coords coords) :chunk))