From dc2148cb393865ce70dec83b6d668e0055cfae2b Mon Sep 17 00:00:00 2001 From: entar Date: Sat, 21 Feb 2026 22:40:49 +0700 Subject: [PATCH 1/5] Added State handling, merged .vscode settings --- .vscode/settings.json | 3 +- src/Core/State/BaseState.luau | 19 +++++++++++ src/Core/State/init.luau | 52 +++++++++++++++++++++++++++++- src/Game/States/Menu/init.lua | 5 +++ src/Game/States/PlayState/init.lua | 5 +++ 5 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/Game/States/Menu/init.lua create mode 100644 src/Game/States/PlayState/init.lua diff --git a/.vscode/settings.json b/.vscode/settings.json index c59f616..9791312 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,6 @@ ], "luau-lsp.platform.type": "roblox", "luau-lsp.fflags.enableNewSolver": true, - "luau-lsp.sourcemap.enabled": false + "luau-lsp.sourcemap.enabled": false, + "stylua.targetReleaseVersion": "latest" } \ No newline at end of file diff --git a/src/Core/State/BaseState.luau b/src/Core/State/BaseState.luau index 6301db7..079f5e5 100644 --- a/src/Core/State/BaseState.luau +++ b/src/Core/State/BaseState.luau @@ -1,5 +1,11 @@ --!strict +--[[ + Set up logging +--]] +local Logging = require("../../../Packages/Logging") +local Logger = Logging.GetLogger("StateLogger") + local BaseState = {} BaseState.__index = BaseState @@ -22,6 +28,9 @@ local DEFAULT_EVENTS = { Destroying = function() return end, + Drawing = function() + return + end, } ------------------------------------------------------------------------------------------------------------------------- @@ -82,6 +91,16 @@ function BaseState.Destroying(self: BaseState, Callback: () -> ()): () self._Events.Destroying = Callback end +--[[ + Calls a state function. +]] +function BaseState.Call(self: BaseState, Function: keyof): () + if not self._Events[Function] then + Logger:Warn("This state doesn't have this function") + end + self._Events[Function]() +end + ------------------------------------------------------------------------------------------------------------------------- return BaseState diff --git a/src/Core/State/init.luau b/src/Core/State/init.luau index 772b83b..5adab83 100644 --- a/src/Core/State/init.luau +++ b/src/Core/State/init.luau @@ -7,6 +7,56 @@ ------------------------------------------------------------------------------------------------------------------------- +--[[ + This has the BaseState class which other states build on +--]] +local BaseStateModule = require("@self/BaseState") +local BaseState = BaseStateModule.new() + +--[[ + All the states are preloaded and stored in this table +--]] +local States = {}:: {[string]: BaseStateModule.BaseState} + +local CurrentState: BaseStateModule.BaseState = BaseState + +for _, State in next, love.filesystem.getDirectoryItems("Game/States") do + local Path = string.format("Game.States.%s", State) + States[State] = require(Path) :: BaseStateModule.BaseState +end + +--[[ + Sets up the state and gets everything done +--]] +local function SetState(Name: string, ...) + if CurrentState then + CurrentState._Events.Destroying() + table.clear(CurrentState) + CurrentState = BaseState + end + + CurrentState = table.clone(States[Name]) + assert(CurrentState, "Current State is nil.") + + CurrentState._Events.Preloading(...) + CurrentState._Events.Loading() + CurrentState._Events.Postloading() +end + +--[[ + Connecting the love functions to the CurrentState +--]] +function love.update(delta) + CurrentState:Call("Updating", delta) +end + +function love.draw() + CurrentState:Call("Drawing") +end + +SetState("Menu") + return { - BaseState = require("@self/BaseState"), + BaseState = BaseState, + Switch = SetState } diff --git a/src/Game/States/Menu/init.lua b/src/Game/States/Menu/init.lua new file mode 100644 index 0000000..a05c963 --- /dev/null +++ b/src/Game/States/Menu/init.lua @@ -0,0 +1,5 @@ +local States = require("../../Core/State") + +local State = States.BaseState.new() + +return State \ No newline at end of file diff --git a/src/Game/States/PlayState/init.lua b/src/Game/States/PlayState/init.lua new file mode 100644 index 0000000..a05c963 --- /dev/null +++ b/src/Game/States/PlayState/init.lua @@ -0,0 +1,5 @@ +local States = require("../../Core/State") + +local State = States.BaseState.new() + +return State \ No newline at end of file From 142d31ee52c0db765530e2014d61a7c91549385c Mon Sep 17 00:00:00 2001 From: entar Date: Sat, 21 Feb 2026 22:57:26 +0700 Subject: [PATCH 2/5] Fixed everything up with stylua and selene, added my stuff to .gitignore --- .gitignore | 5 +++++ src/Core/State/init.luau | 36 +++++++++++++++--------------- src/Game/States/Menu/init.lua | 2 +- src/Game/States/PlayState/init.lua | 2 +- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/.gitignore b/.gitignore index 7c88dde..15a791a 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,8 @@ venv # Love2D .build dist + +rokit.exe +selene.exe +stylua.exe +kaledis.exe \ No newline at end of file diff --git a/src/Core/State/init.luau b/src/Core/State/init.luau index 5adab83..3546584 100644 --- a/src/Core/State/init.luau +++ b/src/Core/State/init.luau @@ -16,47 +16,47 @@ local BaseState = BaseStateModule.new() --[[ All the states are preloaded and stored in this table --]] -local States = {}:: {[string]: BaseStateModule.BaseState} +local States = {} :: { [string]: BaseStateModule.BaseState } local CurrentState: BaseStateModule.BaseState = BaseState for _, State in next, love.filesystem.getDirectoryItems("Game/States") do - local Path = string.format("Game.States.%s", State) - States[State] = require(Path) :: BaseStateModule.BaseState + local Path = string.format("Game.States.%s", State) + States[State] = require(Path) :: BaseStateModule.BaseState end --[[ Sets up the state and gets everything done --]] local function SetState(Name: string, ...) - if CurrentState then - CurrentState._Events.Destroying() - table.clear(CurrentState) - CurrentState = BaseState - end - - CurrentState = table.clone(States[Name]) - assert(CurrentState, "Current State is nil.") - - CurrentState._Events.Preloading(...) - CurrentState._Events.Loading() - CurrentState._Events.Postloading() + if CurrentState then + CurrentState._Events.Destroying() + table.clear(CurrentState) + CurrentState = BaseState + end + + CurrentState = table.clone(States[Name]) + assert(CurrentState, "Current State is nil.") + + CurrentState._Events.Preloading(...) + CurrentState._Events.Loading() + CurrentState._Events.Postloading() end --[[ Connecting the love functions to the CurrentState --]] function love.update(delta) - CurrentState:Call("Updating", delta) + CurrentState:Call("Updating", delta) end function love.draw() - CurrentState:Call("Drawing") + CurrentState:Call("Drawing") end SetState("Menu") return { BaseState = BaseState, - Switch = SetState + Switch = SetState, } diff --git a/src/Game/States/Menu/init.lua b/src/Game/States/Menu/init.lua index a05c963..610386d 100644 --- a/src/Game/States/Menu/init.lua +++ b/src/Game/States/Menu/init.lua @@ -2,4 +2,4 @@ local States = require("../../Core/State") local State = States.BaseState.new() -return State \ No newline at end of file +return State diff --git a/src/Game/States/PlayState/init.lua b/src/Game/States/PlayState/init.lua index a05c963..610386d 100644 --- a/src/Game/States/PlayState/init.lua +++ b/src/Game/States/PlayState/init.lua @@ -2,4 +2,4 @@ local States = require("../../Core/State") local State = States.BaseState.new() -return State \ No newline at end of file +return State From 469c3c82345541bcfe5ec2c610ec875e6a1a485f Mon Sep 17 00:00:00 2001 From: Entar <129566967+Entarno54@users.noreply.github.com> Date: Sun, 22 Feb 2026 11:13:45 +0700 Subject: [PATCH 3/5] Logging documentation for BaseState corrected Co-authored-by: Cayla --- src/Core/State/BaseState.luau | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/State/BaseState.luau b/src/Core/State/BaseState.luau index 079f5e5..4fcbefe 100644 --- a/src/Core/State/BaseState.luau +++ b/src/Core/State/BaseState.luau @@ -4,7 +4,7 @@ Set up logging --]] local Logging = require("../../../Packages/Logging") -local Logger = Logging.GetLogger("StateLogger") +local Logger = Logging.GetLogger("Core.State.BaseState") local BaseState = {} BaseState.__index = BaseState From 5ac6b72adfcb6bbcc4ee316493313e946155e0a8 Mon Sep 17 00:00:00 2001 From: Entar <129566967+Entarno54@users.noreply.github.com> Date: Sun, 22 Feb 2026 11:14:06 +0700 Subject: [PATCH 4/5] BaseState Function documentation corrected Co-authored-by: Cayla --- src/Core/State/BaseState.luau | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Core/State/BaseState.luau b/src/Core/State/BaseState.luau index 4fcbefe..250d3aa 100644 --- a/src/Core/State/BaseState.luau +++ b/src/Core/State/BaseState.luau @@ -93,6 +93,8 @@ end --[[ Calls a state function. + + @param Function -- The function to call. ]] function BaseState.Call(self: BaseState, Function: keyof): () if not self._Events[Function] then From 483b4cfc4956bdb83cd0af473df5d13a4c0f651b Mon Sep 17 00:00:00 2001 From: Entar <129566967+Entarno54@users.noreply.github.com> Date: Sun, 22 Feb 2026 11:14:22 +0700 Subject: [PATCH 5/5] BaseState Switch documentation corrected Co-authored-by: Cayla --- src/Core/State/init.luau | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Core/State/init.luau b/src/Core/State/init.luau index 3546584..35a945e 100644 --- a/src/Core/State/init.luau +++ b/src/Core/State/init.luau @@ -26,7 +26,9 @@ for _, State in next, love.filesystem.getDirectoryItems("Game/States") do end --[[ - Sets up the state and gets everything done + Switches the current state and loads a new one. + + @param Name -- The name of the new state. --]] local function SetState(Name: string, ...) if CurrentState then