Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ venv
# Love2D
.build
dist

rokit.exe
selene.exe
stylua.exe
kaledis.exe
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
21 changes: 21 additions & 0 deletions src/Core/State/BaseState.luau
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -22,6 +28,9 @@ local DEFAULT_EVENTS = {
Destroying = function()
return
end,
Drawing = function()
return
end,
}

-------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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<typeof(DEFAULT_EVENTS)>): ()
if not self._Events[Function] then
Logger:Warn("This state doesn't have this function")
end
self._Events[Function]()
end

-------------------------------------------------------------------------------------------------------------------------

return BaseState
54 changes: 53 additions & 1 deletion src/Core/State/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
5 changes: 5 additions & 0 deletions src/Game/States/Menu/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local States = require("../../Core/State")

local State = States.BaseState.new()

return State
5 changes: 5 additions & 0 deletions src/Game/States/PlayState/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local States = require("../../Core/State")

local State = States.BaseState.new()

return State