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/.vscode/settings.json b/.vscode/settings.json index 0fc38f9..39cd0c4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,5 @@ ], "luau-lsp.platform.type": "standard", "luau-lsp.fflags.enableNewSolver": true, - "luau-lsp.sourcemap.enabled": false -} + "luau-lsp.sourcemap.enabled": false, + "stylua.targetReleaseVersion": "latest" diff --git a/src/Core/State/BaseState.luau b/src/Core/State/BaseState.luau index 6301db7..250d3aa 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("Core.State.BaseState") + local BaseState = {} BaseState.__index = BaseState @@ -22,6 +28,9 @@ local DEFAULT_EVENTS = { Destroying = function() return end, + Drawing = function() + return + end, } ------------------------------------------------------------------------------------------------------------------------- @@ -82,6 +91,18 @@ function BaseState.Destroying(self: BaseState, Callback: () -> ()): () self._Events.Destroying = Callback end +--[[ + Calls a state function. + + @param Function -- The function to call. +]] +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..35a945e 100644 --- a/src/Core/State/init.luau +++ b/src/Core/State/init.luau @@ -7,6 +7,58 @@ ------------------------------------------------------------------------------------------------------------------------- +--[[ + 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 + +--[[ + 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 + 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..610386d --- /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 diff --git a/src/Game/States/PlayState/init.lua b/src/Game/States/PlayState/init.lua new file mode 100644 index 0000000..610386d --- /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