-
Notifications
You must be signed in to change notification settings - Fork 3
/
Play.lua
114 lines (95 loc) · 3.35 KB
/
Play.lua
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
local pd <const> = playdate
local gfx <const> = Graphics
Play = {}
class("Play").extends(BaseScene)
local items
local itemTimer
local player
local playerSlot
local ground <const> = gfx.image.new("/assets/images/ground")
local tree <const> = gfx.image.new("/assets/images/tree")
local grass <const> = gfx.image.new("/assets/images/grass")
local collected = 0
-- Input is mostly handled by the player
Play.inputHandler = {
BButtonHeld = function()
Noble.GameData.set("items", items, playerSlot) -- Save the player's collected items
Noble.transition(Title)
end
}
function Play:init()
Play.super.init(self)
collected = 0
playerSlot = tonumber(Noble.Settings.get("playerSlot"))
items = Noble.GameData.get("items", playerSlot)
player = Player(100, 96, playerSlot)
end
function Play:enter()
Play.super.enter(self)
-- Using regular sprites because of convenience
local tree_sprite = gfx.sprite.new(tree)
tree_sprite:setCenter(0.5, 1) -- This sets the anchor to the bottom center
tree_sprite:moveTo(144, 104)
self:addSprite(tree_sprite)
local bush_sprite = gfx.sprite.new(tree) -- This sprite is centered at 0.5, 0.5
bush_sprite:moveTo(32, 100)
self:addSprite(bush_sprite)
local grass1 = gfx.sprite.new(grass)
grass1:setCenter(0.5, 1)
grass1:moveTo(50, 105)
self:addSprite(grass1)
local grass2 = gfx.sprite.new(grass)
grass2:setCenter(0.5, 1)
grass2:moveTo(130, 105)
self:addSprite(grass2)
local ground_sprite = gfx.sprite.new(ground)
ground_sprite:setCenter(0.5, 0) -- This sets the anchor to the top center
ground_sprite:moveTo(100, 104)
self:addSprite(ground_sprite)
self:addSprite(player)
player.active = true -- Setting active sets the state, so we don't set it until the scene enters
playdate.display.setScale(2) -- Set the screen scale so the 16x16 pixels are bigger
end
function Play:collectItem(_, value)
collected += 1
local key = tostring(value) -- Convert to string so the items work like a dictionary and not an array
if items[key] == nil then
items[key] = 0
end
items[key] += 1
end
-- This runs once a transition from another scene is complete.
function Play:start()
Play.super.start(self)
self.menu:addMenuItem("menu", function() Noble.transition(Title) end)
Noble.showFPS = true
Signal:add("collected", self, self.collectItem) -- Add our callback when an item is collected
itemTimer = Timer.new(1000, function() -- Once a second, spawn an item at a random position
Item(math.random(10, 190), -10)
end)
itemTimer.repeats = true
end
-- This runs as as soon as a transition to another scene begins.
function Play:exit()
Play.super.exit(self)
Noble.showFPS = false
itemTimer:remove()
Signal:remove("collected", self.collectItem)
Noble.GameData.set("items", items, playerSlot) -- Save the player's collected items
end
-- This runs once a transition to another scene completes.
function Play:finish()
Play.super.finish(self)
playdate.display.setScale(1) -- Reset the scale when exiting
end
-- This is called before the system pauses the game
function Play:pause()
Play.super.pause(self)
-- Draw the number of collected items to the menu image
local _img = gfx.image.new(400, 240, gfx.kColorWhite)
gfx.pushContext(_img)
gfx.drawTextAligned("*Collected*", 100, 110, kTextAlignment.center)
gfx.drawTextAligned(collected, 100, 130, kTextAlignment.center)
gfx.popContext()
pd.setMenuImage(_img)
end