-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathNobleScene.lua
More file actions
243 lines (218 loc) · 7.41 KB
/
NobleScene.lua
File metadata and controls
243 lines (218 loc) · 7.41 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
---
-- An abstract scene class.
-- Do not copy this file as a template for your scenes. Instead, your scenes will extend this class.
-- See <a href="../examples/SceneTemplate.lua.html">templates/SceneTemplate.lua</a> for a blank scene that you can copy and modify for your own scenes.
-- If you are using <a href="http://github.com/NobleRobot/NobleEngine-ProjectTemplate">NobleEngine-ProjectTemplate</a>,
-- see `scenes/ExampleScene.lua` for an implementation example.
-- @usage
-- YourSceneName = {}
-- class("YourSceneName").extends(NobleScene)
-- local scene = YourSceneName
--
-- @classmod NobleScene
--
NobleScene = {}
class("NobleScene").extends(Object)
--- Properties
-- @section properties
--- The name of this scene. Optional.
-- If you do not set this value, it will take on the scene's `className`.
NobleScene.name = ""
--- This is the background color of this scene.
--
NobleScene.backgroundColor = Graphics.kColorWhite
--- Tables
-- @section tables
--- All scenes have a default inputHandler which is made active when the scene starts.
-- If you do not define your scene's `inputHandler`, it is `nil` and input is disabled when this scene
-- starts.
-- @see Noble.Input.setHandler
--
-- @usage
-- YourSceneName.inputHandler = {
-- AButtonDown = function()
-- // Your code here
-- end,
-- AButtonHold = function()
-- // Your code here
-- end,
-- -- ...
-- -- ...
-- }
-- -- OR...
-- -- Use a non-scene-specific inputHandler, defined elsewhere.
-- YourSceneName.inputHandler = somePreviouslyDefinedInputHandler
-- -- OR...
-- -- Reuse another scene's inputHandler.
-- YourSceneName.inputHandler = SomeOtherSceneName.inputHandler
NobleScene.inputHandler = {}
--- When you add a sprite to your scene, it is put in this table so the scene can keep track of it.
--
-- This is intended as `read-only`. You should not modify this table directly.
-- @see addSprite
NobleScene.sprites = {}
--- Methods
-- @section Methods
--- Use this to add sprites to your scene instead of `playdate.graphics.sprite:add()`.
--
-- If your sprite is a `NobleSprite`, using `NobleSprite:add()` will also call this method.
--
-- Sprites added with this method that are tracked by the scene. Any not manually removed before transitioning to another scene are automatically removed in @{finish|finish}.
-- @tparam playdate.graphics.sprite __sprite The sprite to add to the scene.
-- @see NobleSprite:add
-- @see removeSprite
function NobleScene:addSprite(__sprite)
if (__sprite.isNobleSprite == true) then
__sprite:superAdd()
else
__sprite:add()
end
if (table.indexOfElement(self.sprites, __sprite) == nil) then
table.insert(self.sprites, __sprite)
end
end
--- Use this to remove sprites from your scene instead of `playdate.graphics.sprite:remove()`.
--
-- If your sprite is a `NobleSprite`, using `NobleSprite:remove()` will also call this method.
--
-- Sprites not manually removed before transitioning to another scene are automatically removed in @{finish|finish}.
-- @tparam playdate.graphics.sprite __sprite The sprite to add to the scene.
-- @see NobleSprite:remove
-- @see addSprite
function NobleScene:removeSprite(__sprite)
if (__sprite.isNobleSprite == true) then
__sprite:superRemove()
else
__sprite:remove()
end
if (table.indexOfElement(self.sprites, __sprite) ~= nil) then
table.remove(self.sprites, table.indexOfElement(self.sprites, __sprite))
end
end
--- Callbacks
-- @section callbacks
--- Implement this in your scene if you have code to run when your scene's object is created.
--
-- @usage
-- function YourSceneName:init()
-- YourSceneName.super.init(self)
-- --[Your code here]--
-- end
--
function NobleScene:init()
self.name = self.className
self.sprites = {}
end
--- Implement if you want to run code as the transition to this scene begins, such as UI animation, triggers, etc.
--
-- @usage
-- function YourSceneName:enter()
-- YourSceneName.super.enter(self)
-- --[Your code here]--
-- end
--
function NobleScene:enter() end
--- Implement if you have code to run once the transition to this scene is complete. This method signifies the full activation of a scene. If this scene's `inputHandler` is defined, it is enabled now.
-- @see inputHandler
-- @usage
-- function YourSceneName:start()
-- YourSceneName.super.start(self)
-- --[Your code here]--
-- end
--
function NobleScene:start()
Noble.Input.setHandler(self.inputHandler)
Graphics.sprite.redrawBackground()
end
--- Implement to run scene-specific code on every frame while this scene is active.
-- <strong>NOTE:</strong> you may use coroutine.yield() here, because it only runs inside of playdate.update(), which is a coroutine.
--
-- @usage
-- function YourSceneName:update()
-- YourSceneName.super.update(self)
-- --[Your code here]--
-- end
--
function NobleScene:update() end
--- Implement this function to draw background visual elements in your scene.
--- This runs when the engine need to redraw a background area.
--- By default it runs every frame and fills the background with self.backgroundColor. All arguments are optional.
--- Use `Graphics.sprite.setAlwaysRedraw(false)` after `Noble.new()` to optimize partial redraw.
--
-- @usage
-- function YourSceneName:drawBackground(__x, __y, __width, __height)
-- YourSceneName.super.drawBackground(self) -- optional, invokes default behavior.
-- --[Your code here]--
-- end
--
function NobleScene:drawBackground(__x, __y, __width, __height)
__x = __x or 0
__y = __y or 0
__width = __width or Display.getWidth()
__height = __height or Display.getHeight()
-- Cache the currently set color/pattern.
local color <const> = Graphics.getColor()
local color_type <const> = type(color)
-- Draw background.
Graphics.setColor(self.backgroundColor)
Graphics.fillRect(__x, __y, __width, __height)
-- Reset color/pattern from cache.
if color_type == 'number' then
Graphics.setColor(color)
elseif color_type == 'table' then
Graphics.setPattern(color)
end
end
--- Implement this in your scene if you have "goodbye" code to run when a transition to another scene
-- begins, such as UI animation, saving to disk, etc.
--
-- @usage
-- function YourSceneName:exit()
-- YourSceneName.super.exit(self)
-- --[Your code here]--
-- end
--
function NobleScene:exit()
for _, sprite in ipairs(self.sprites) do
sprite:setUpdatesEnabled(false)
sprite:setCollisionsEnabled(false)
end
end
--- Implement this in your scene if you have code to run when a transition to another scene
-- is complete, such as resetting variables.
--
-- @usage
-- function YourSceneName:finish()
-- YourSceneName.super.finish(self)
-- --[Your code here]--
-- end
--
function NobleScene:finish()
for _, sprite in ipairs(self.sprites) do
if (sprite.isNobleSprite) then
sprite:superRemove()
else
sprite:remove()
end
end
end
--- `pause()` / `resume()`
--
-- Implement one or both of these in your scene if you want something to happen when the game is paused/unpaused
-- by the system. The Playdate SDK does not require you to write pause logic, but these are useful if you want a
-- custom menu image (see Playdate SDK for more details), want to obscure game elements to prevent players from
-- cheating in a time-sensitive game, want to count the number of times the player pauses the game, etc.
--
-- @usage
-- function YourSceneName:pause()
-- YourSceneName.super.pause(self)
-- --[Your code here]--
-- end
function NobleScene:pause() end
--- <span></span>
-- @usage
-- function YourSceneName:resume()
-- YourSceneName.super.resume(self)
-- --[Your code here]--
-- end
function NobleScene:resume() end