-
Notifications
You must be signed in to change notification settings - Fork 2
/
score.lua
182 lines (148 loc) · 3.24 KB
/
score.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
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
-- Mark H Carolan
--
-- Stroople project
-- © 2010 Mark H Carolan, Gregory S Hooper
module(..., package.seeall)
require "globals"
require "highscores"
require "ui"
local returnFunction
local background
local homeButton
local scoreboard
local yourScore
local hits, misses
local soundCorrect = media.newEventSound( "correct.aif" )
local soundIncorrect = media.newEventSound( "incorrect.aif" )
local soundMiss = media.newEventSound( "miss.aif" )
local missSoundPlaying = false
local prevFrameTime, prevTimeoutTime
local levelTimes =
{
0, 0, 0, 0
}
function getHitsAndMisses()
return hits, misses
end
function getCurLevelTime()
return globals.getLevelAggregateTime() + (system.getTimer() - globals.getLevelStartTime())
end
function testSound()
if globals.isSoundOn() then
media.playEventSound(soundCorrect)
end
end
function startLevel(n)
hits = 0
misses = 0
globals.setLevelID(n)
globals.resetLevelAggregateTime()
local now = system.getTimer()
globals.setLevelStartTime(now)
prevFrameTime = now
prevTimeoutTime = now
end
function isTimedOut(timeout)
local now = system.getTimer()
local timeoutTime = now - prevTimeoutTime
if timeoutTime >= timeout then
prevTimeoutTime = now
return true
end
return false
end
function elapsedSinceLastFrame()
local now = system.getTimer()
local elapsed = now - prevFrameTime
prevFrameTime = now
return elapsed
end
function pause()
local elapsed = system.getTimer() - globals.getLevelStartTime()
globals.addToLevelAggregateTime(elapsed)
end
function resume()
globals.setLevelStartTime(system.getTimer())
prevFrameTime = globals.getLevelStartTime()
end
function endLevel(endScore)
return highscores.isHighScore(getCurLevelTime(), endScore)
end
function setScore(n)
globals.setScore(n)
end
function getScore()
return globals.getScore()
end
function add(n)
hits = hits + 1
globals.addScore(n)
lightHit(true)
if globals.isSoundOn() then
media.playEventSound(soundCorrect)
end
end
function sub(n)
misses = misses + n
globals.addScore(-n)
lightMiss(true)
if globals.isSoundOn() then
media.playEventSound(soundIncorrect)
end
end
local function soundMissComplete()
missSoundPlaying = false
end
function miss(n)
misses = misses + n
globals.addScore(-n)
lightMiss(true)
if globals.isSoundOn() and not missSoundPlaying then
missSoundPlaying = true
media.playEventSound(soundMiss, soundMissComplete)
end
end
function frame()
lightHit()
lightMiss()
end
local function removeMenuButtons()
homeButton:removeSelf()
homeButton = nil
scoreboard:removeSelf()
scoreboard = nil
if yourScore then
yourScore:removeSelf()
yourScore = nil
end
end
local function onMainMenu()
background.isVisible = false
removeMenuButtons()
returnFunction()
end
function showScoreMenu(hsTime)
background.isVisible = true
background.parent:insert(background)
scoreboard = highscores.newScoreboard(hsTime)
if not homeButton then
homeButton = ui.newButton
{
default = "home.png",
over = "homeH.png",
onRelease = onMainMenu,
x = globals.W2,
y = globals.H - 30
}
end
end
function init(returnFunc)
returnFunction = returnFunc
background = display.newImage("background.png")
end
function cleanup()
scoreboard:removeSelf()
scoreboard = nil
removeMenuButtons()
background:removeSelf()
end