Rename state-loop’s loop functions to “state-functions”

Also document all current state-functions.
This commit is contained in:
Jaidyn Ann 2023-06-09 16:14:46 -05:00
parent 94335468b3
commit 0cfd2deae0
3 changed files with 82 additions and 52 deletions

View File

@ -33,53 +33,83 @@
(in-package :flora-search-aurora) (in-package :flora-search-aurora)
(defun state-loop (loops &optional (last-matrix (make-screen-matrix)) (matrix (make-screen-matrix))) (defun state-loop
(when loops (states &key (last-matrix (make-screen-matrix)) (matrix (make-screen-matrix)) (state-params nil))
(let ((loop-result "Begin the games loop, which executes (henceforthly called) state-functions over and over again
(apply (car loops) (list matrix)))) until Hell freezes over and a new king reigns supreme.
(print-screen-matrix (matrix-delta last-matrix matrix)) Given a list of state-functions, states, it will execute the first function.
Each state-function must take at least a single parameter, a matrix of characters. A state-function
should edit this matrix in-place, replacing its elements with characters that will later be printed
to the terminal.
If this function returns NIL, then it will be removed from state-function list and the next
function in the list will be run in the next iteration. If no more functions remain, the loop
terminates.
If this function returns a list, then the given list will be used as additional parameters to the
state-function in the next iteration, in addition to the aforementioned matrix.
If this function returns a function, then the returned function will go to the front of the
state-function list, and will be ran in the next iteration onward.
If this function returns anything else, then itll simply be run again in the next iteration.
Make note to add a delay to your state functions, or well, yknow. Your computer will overheat,
or something ¯\_()_/¯"
(when states
(let ((state-result
(apply (car states) (cons matrix state-params)))) ;; Run the latest-added update/draw loop
(print-screen-matrix (matrix-delta last-matrix matrix)) ;; Print its results.
(force-output) (force-output)
(state-loop (state-loop
(cond ((functionp loop-result) (cond ((functionp state-result)
(cons loop-result loops)) (cons state-result states))
((not loop-result) ((not state-result)
(cdr loops)) (cdr states))
('t loops)) ('t states))
matrix)))) :last-matrix matrix
:state-params (when (listp state-result)
state-result)))))
(defun make-main-menu-loop () (defun make-main-menu-state ()
"Return a state-function for the games main menu, for use with #'state-loop."
(let ((main-menu (let ((main-menu
`(((LABEL . "CRY OUT") (SELECTED . T) (FUNCTION . ,(lambda () (print "AAAAAA")))) `(((LABEL . "PLAY")
((LABEL . "RUN AWAY") (FUNCTION . ,(lambda () nil))) (SELECTION . 100) (SELECTED . T)
(FUNCTION . ,(lambda () (make-main-overworld-state))))
((LABEL . "SUBMENU") ((LABEL . "SUBMENU")
(SELECTION . -100) (FUNCTION . ,(lambda () (make-options-menu-state))))
(FUNCTION . ,(make-options-menu-loop)))))) ((LABEL . "QUIT") (FUNCTION . ,(lambda () nil))))))
(lambda (matrix) (lambda (matrix)
(menu-loop matrix main-menu)))) (menu-state matrix main-menu))))
(defun make-options-menu-loop () (defun make-options-menu-state ()
"Return a state-function for the options menu, for use with #'state-loop."
(let ((options-menu (let ((options-menu
`(((LABEL . "IDK") (SELECTION . 100) (SELECTED . T) `(((LABEL . "IDK")
(FUNCTION . ,(lambda () (print "¯\_(ツ)_/¯")))) (SELECTION . 100) (SELECTED . T)
((LABEL . "GO BACK") (FUNCTION . ,(lambda () nil)))))) (FUNCTION . ,(lambda () (print "¯\_(ツ)_/¯"))))
((LABEL . "GO BACK")
(FUNCTION . ,(lambda () nil))))))
(lambda (matrix) (lambda (matrix)
(menu-loop matrix options-menu)))) (menu-state matrix options-menu))))
(defun make-main-overworld-loop () (defun make-main-overworld-state ()
"Return a state-function for the games overworld (the majority of the game), for use with
#'state-loop."
(lambda (matrix) (lambda (matrix)
(overworld-loop matrix "/home/jaidyn/.local/src/games/flora search aurora/res/map.tmx"))) (overworld-state matrix "/home/jaidyn/.local/src/games/flora search aurora/res/map.tmx")))
(defun main () (defun main ()
"A pathetic fascimile of a main loop. Look, I'm still tinkering!" "A pathetic fascimile of a main loop. What does it do? WHAST DOES TI DODOO?"
(cl-charms:with-curses () (cl-charms:with-curses ()
(cl-charms:enable-raw-input :interpret-control-characters 't) (cl-charms:enable-raw-input :interpret-control-characters 't)
(clear-screen) (clear-screen)
(state-loop (list (make-main-menu-loop) (state-loop (list (make-main-menu-state)))))
(make-main-overworld-loop)))))
(main) (main) ;; — Knock-knock
;; — Whos there?
;; — Yo momma!
;; — “Yo momma” who?
;; — Yo mommas a sweet lady, and I think shes swell!

View File

@ -21,7 +21,7 @@
(: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.ui)
(:export #:overworld-loop)) (:export #:overworld-state))
(in-package :flora-search-aurora.overworld) (in-package :flora-search-aurora.overworld)
@ -30,30 +30,29 @@
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
;;; Overworld loop ;;; Overworld loop
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
(defun overworld-loop (matrix map) (defun overworld-state (matrix map)
"The state loop to be used for displaying/processing/input-managing "Render the given map to the matrix and take user-input for one frame.
with menus." A state-function for use with #'state-loop."
(let ((map (if (or (stringp map) (pathnamep map)) (let ((map (if (or (stringp map) (pathnamep map))
(cl-tiled:load-map map) (cl-tiled:load-map map)
map))) map)))
(sleep .02) (sleep .02)
(overworld-loop-draw matrix map) (overworld-state-draw matrix map)
(overworld-loop-update map))) (overworld-state-update map)))
(defun overworld-loop-draw (matrix map) (defun overworld-state-draw (matrix map)
"Drawing for." "Draw the overworld map to the given matrix.
A core part of #'overworld-state."
(matrix-write-tiled-map matrix map)) (matrix-write-tiled-map matrix map))
(defun overworld-state-update (map)
(defun overworld-loop-update (map) "Do nothing, lol.
"The update loop for menus. It processes all input, state, etc, and Core part of #'overworld-state."
returns the new state of the menu."
't) 't)
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
;;; Mapping & map-rendering ;;; Mapping & map-rendering

23
ui.lisp
View File

@ -19,7 +19,7 @@
(defpackage :flora-search-aurora.ui (defpackage :flora-search-aurora.ui
(:use :cl :flora-search-aurora.display :flora-search-aurora.input :assoc-utils) (:use :cl :flora-search-aurora.display :flora-search-aurora.input :assoc-utils)
(:export #:menu-loop #:render-menu-strip :label :selection :selected)) (:export #:menu-state #:render-menu-strip :label :selection :selected))
(in-package :flora-search-aurora.ui) (in-package :flora-search-aurora.ui)
@ -27,22 +27,23 @@
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
;;; Menu loops ;;; Menu loops
;;; ——————————————————————————————————— ;;; ———————————————————————————————————
(defun menu-loop (matrix menu-alist) (defun menu-state (matrix menu-alist)
"The state loop to be used for displaying/processing/input-managing "Render a menu in menu-alist format to the given matrix, and process user-input.
with menus." A state-function for use with the #'state-loop."
(sleep .02) (sleep .02)
(menu-loop-draw matrix menu-alist) (menu-state-draw matrix menu-alist)
(menu-loop-update menu-alist)) (menu-state-update menu-alist))
(defun menu-loop-draw (matrix menu-alist) (defun menu-state-draw (matrix menu-alist)
"Drawing for." "Render a menu in menu-alist format to the given matrix.
A core part of #'menu-state."
(render-menu-strip matrix menu-alist 0 0)) (render-menu-strip matrix menu-alist 0 0))
(defun menu-loop-update (menu-alist) (defun menu-state-update (menu-alist)
"The update loop for menus. It processes all input, state, etc, and "Update a menu that is, take user input and modify the menus alist appropriately.
returns the new state of the menu." A core part of #'menu-state."
(progress-menu-items menu-alist) (progress-menu-items menu-alist)
(process-menu-input menu-alist)) (process-menu-input menu-alist))