-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSceneManager.lua
More file actions
109 lines (93 loc) · 2.78 KB
/
SceneManager.lua
File metadata and controls
109 lines (93 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
local utf8 = require("utf8")
-- Manages different game scenes and transitions between them
SceneManager = {}
SceneManager.__index = SceneManager
function SceneManager:new(ctx)
return setmetatable({
scenes = {},
sceneStack = {}, -- stack to manage overlay scenes
paused = false, -- if paused, only update the top scene
ctx = ctx
}, self)
end
function SceneManager:addScene(scene)
self.scenes[scene.name] = scene
scene.sceneManager = self
end
-- Change to a new scene, clearing the scene stack
function SceneManager:changeScene(sceneId)
while #self.sceneStack > 0 do
self:popScene()
end
self:pushScene(sceneId)
end
function SceneManager:pushScene(sceneId)
local scene = self.scenes[sceneId]
if not scene then
error("Scene not found: " .. tostring(sceneId))
end
table.insert(self.sceneStack, scene)
scene:enter()
end
function SceneManager:popScene()
if #self.sceneStack == 0 then
return
end
local scene = table.remove(self.sceneStack)
scene:exit()
end
function SceneManager:getScene(name)
return self.scenes[name]
end
function SceneManager:getCurrentScene()
return self.sceneStack[#self.sceneStack]
end
function SceneManager:update(dt)
if self.paused then
self:getCurrentScene():update(dt)
else
for _, scene in ipairs(self.sceneStack) do
scene:update(dt)
end
end
end
function SceneManager:draw()
for _, scene in ipairs(self.sceneStack) do
scene:draw()
end
local currentScene = self:getCurrentScene()
currentScene:drawInputInterface()
end
function SceneManager:keypressed(key)
local currentScene = self:getCurrentScene()
local uiInput = self.ctx.ui.input
if key == "backspace" and utf8.offset(uiInput, -1) then
uiInput = string.sub(uiInput, 1, utf8.offset(uiInput, -1) - 1)
self.ctx.ui.input = uiInput
currentScene:updateSuggestedCommand()
return
end
if key == "return" then
local userInput = uiInput
if currentScene.suggestedCommand and currentScene.suggestedCommandAutocomplete then
userInput = currentScene.suggestedCommand
end
userInput = userInput:match("^%s*(.-)%s*$")
self.ctx.ui.input = "" -- clear user input field
currentScene:updateSuggestedCommand()
currentScene:handleInput(userInput)
return
end
currentScene:keypressed(key)
end
function SceneManager:textinput(t)
self.ctx.ui.input = self.ctx.ui.input .. t
self.ctx.ui.messageLeft = "" -- Clears placeholder that shares the input area
self:getCurrentScene():textinput(t)
end
function SceneManager:wheelmoved(x, y)
self:getCurrentScene():wheelmoved(x, y)
end
function SceneManager:pause(val)
self.paused = val
end