-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen_play.lua
156 lines (129 loc) · 3.75 KB
/
screen_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
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
local Button = require 'lbutton'
local PausePane = require 'pausepane'
--local Result = require 'result'
local Tetris = require 'tetris'
local lg = love.graphics
local lm = love.mouse
local getTime = love.timer.getTime
local random = love.math.random
local GAME_WIDTH = 200
local GAME_HEIGHT = 280
---Get characters to use for the game
-- @param profile
-- @param levels array {true,false,...} with index associated with a skill lvl
-- @return array-table; WARNING: CAN BE EMPTY
local function getChars(profile, levels)
assert (profile, "profile must exist")
local t = {}
local t_count = 0
for l=1, #levels do
if levels[l] then
local chars = profile:getCharsAtLevel(l)
for i=1, #chars do
t[#t + 1] = chars[i]
end
end
end
return t
end
---Abstracted routine to create a table of buttons
local function makeButtons()
return {pause = Button.new(0,0, _G.quads.buttons.pause)}
end
local Play = {}
Play.enter = function(self)
self.paused = false
self.pausepane = self.pausepane or PausePane.new()
self.buttons = self.buttons or makeButtons()
self.profile = _G.profile
-- self.results = Result.new()
local chars = getChars(self.profile, _G.tetrisOpts)
self.game = Tetris.new(GAME_WIDTH,GAME_HEIGHT,
self.profile:getTimeUnit(), chars)
_G.tetrisOpts = nil
end
Play.setUnpaused = function(self)
self.paused = false
end
Play.setPaused = function(self)
self.paused = true
end
Play.handlePausedClick = function(self, mx, my)
local b = self.pausepane:getHot(mx,my)
if b == 'quit' then
--TODO? fetch results and add to profile
_G.setMode('tetrisselect')
elseif b == 'resume' then
self:setUnpaused()
end
end
Play.receiveClick = function(self, mx, my)
if self.paused then
self:handlePausedClick(mx, my)
elseif self.hot == 'pause' then
--if self.hot == 'pause' then
self:setPaused()
--end
end
end
Play.receiveKeypress = function(self, key)
if self.paused then
if key == 'escape' then
self:setUnpaused()
elseif key == 'return' then
_G.setMode('tetrisselect')
end
else
if key == 'escape' then
self:setPaused()
-- elseif key == ' ' then
-- self.spacedown = true
-- self.spacewhen = getTime()
else --TODO? handle dictionary-invalid key
self.game:receiveKey(key)
end
end
end
Play.receiveKeyrelease = function(self, key)
-- if self.paused then return end
-- if key == ' ' then
-- self.spacedown = false
-- self.spacewhen = getTime() - self.spacewhen
-- -- self.game:receivePulse(duration)
-- end
end
Play.update = function(self, dt)
if self.paused then
return --TODO: tetris paused update()
else
self.hot = nil
if self.buttons.pause:isMouseOver() then
self.hot = 'pause'
end
self.game:update(dt)
end
end
local function drawGameStatus(game)
--TODO: prettify game stats
local level, score, lives, maxlives = game:getStats()
lg.setColor(0,0,0)
lg.rectangle("fill", 400, 5, 100,100)
lg.setColor(0xff, 0xff, 0xff)
lg.print('score: ' .. score, 405, 8)
lg.print('level: ' .. level, 405, 18)
lg.print(string.format("lives: %d/%d", lives, maxlives),405, 28)
end
Play.draw = function(self)
lg.setColor(0xff, 0xff, 0xff)
if self.hot == 'pause' then
self.buttons.pause:draw(_G.atlases.hot)
else
self.buttons.pause:draw(_G.atlases.buttons)
end
self.game:draw(150, 10)
drawGameStatus(self.game)
if self.paused then
self.pausepane:draw()
end
end
return Play