-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrill.lua
365 lines (261 loc) · 8.94 KB
/
drill.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Declare access to game and act variables
game = globalGame
act = game.newAct()
-- Require statements
widget = require( "widget" )
-- Declare constants
local W = act.xMax
local H = act.height
local XC = act.xCenter
local YC = act.yCenter
local YMIN = act.yMin
local YMAX = act.yMax
--Declare file local functions to be used
local initGame
local newFrame
local droppingBar
local risingBar
local timeLimit
local start
local finish
local reset
local tap
--Declare file local tables to be used
local bg -- Background image
local drillSpot -- Effectiveness of drill usage
local bar -- The bar being adjusted
local splashTop -- Splash subset for the start timer
local splashMiddle -- Splash subset for primary info bits
local splashBottom -- Splash subset for reset button
local startTimer -- Timer when beginning
local range -- Text for range user finished in
local waterText -- Displays how much water the user has earned
local resetButton -- The button that allows you to restart the game
local difficulty = {} -- Difficulty of drill game
local costText -- Text to represent the total cost of drilling out the water
local tapText -- Flashing tap text
local tapTime = 5 -- Time left until tap stops flashing
local tapTimerShow -- Timer to keep track of the tap text
local tapTimerHide -- Timer to keep track of the tap text
local drillSound = {} -- Sound container for drill sound
local endTimer -- Timer used when the player finishes the drill
function act:init()
-- Setup the background
bg = display.newRect( act.group, XC, YC, W, H )
bg.fill = { type = "image", filename = "media/drill/DrillOp.jpg" }
-- Create the worst range
drillSpot = display.newRect( act.group, 0, YC, W / 8, H )
drillSpot.anchorX = 0
drillSpot.fill = { type = "image", filename = "media/drill/DrillSpot.png" }
-- Create the bar
bar = display.newRect( act.group, 0, YMAX, W / 7, H / 4 )
bar.anchorX = 0
bar.anchorY = 1
bar.fill = { 0.36, 0.25, 0.13, 0.5 }
bar.difference = H / 2 - bar.height
-- Create the splash screen
splash = display.newRoundedRect( act.group, XC, YC, 200, 400, 25 )
splash.fill = { 0, 0, 0, .75 }
splash.stroke = { 1, 0, 0 }
-- Start timer function
startInit = 5
startTimer = display.newText( act.group, "Beginning in: " .. startInit, XC, YC - 100, native.systemFontBold, 25 )
startTimer.count = startInit
startTimer.fill = { 0, 0.42, 1 }
startTimer.isVisible = false
-- Create splash screen button
local options =
{
width = 80,
height = 40,
numFrames = 2,
sheetContentWidth = 160,
sheetContentHeight = 40
}
-- Create the button to take you back to the rover game
local buttonSheet = graphics.newImageSheet( "media/drillScan/Button.png", options )
resetButton = widget.newButton{ sheet = buttonSheet, defaultFrame = 1, overFrame = 2, label = "Rover", onPress = reset }
resetButton.x = XC
resetButton.y = YC + 140
act.group:insert( resetButton )
-- Intro screen
game.drillPlayed = false
-- Set difficulty
if game.drillDiff then
difficulty.drop = 0.5 + game.drillDiff * .4
difficulty.rise = 13 + math.log( game.drillDiff )
else
difficulty.drop = 3
difficulty.rise = 15
end
-- Intro screen
game.drillPlayed = false
if game.currentDrillCost == nil and game.currentLiters == nil then
game.currentDrillCost = 11
game.currentLiters = 50
end
-- Cost text
costText = display.newText( act.group, "Cost: " .. -game.currentDrillCost + math.abs( math.floor( bar.difference / 10 ) ) .. "%", XC, YC, native.systemFont, 25 )
costText.fill = { 0, 0.42, 1 }
costText.xScale, costText.yScale = 0.01, 0.01
costText.isVisible = false
-- Water Text
waterText = display.newText( act.group, "Water: " .. game.currentLiters .. " Liters", XC, YC - 20, native.systemFont, 25 )
waterText.fill = { 0, 0.42, 1 }
waterText.xScale, waterText.yScale = 0.01, 0.01
waterText.isVisible = false
-- Tap Timer Text
tapText = display.newText( act.group, "TAP", XC, YC - 75, native.systemFontBold, 35 )
tapText.fill = { 0, 0.42, 1 }
tapText.isVisible = false
-- Declare the introductory information group and check for a flag to hide/show it
infoGroup = display.newGroup()
infoGroup.x, infoGroup.y = XC, YC
local infoScreen = display.newRect( infoGroup, 0, 0, W, H )
infoScreen.fill = { 0, 0, 0, 0.7 }
local infoText1 = display.newText( infoGroup, "Rapidly tap the screen", 0, -30, native.systemFontBold, 20 )
local infoText1 = display.newText( infoGroup, "Aim to be in the blue range", 0, 0, native.systemFontBold, 20 )
local infoText1 = display.newText( infoGroup, "Return to the rover to continue", 0, 30, native.systemFontBold, 20 )
infoScreen:addEventListener( "touch", infoGroupDismiss )
act.group:insert( infoGroup )
game.drillStopped = false
end
function act:prepare()
resetButton.isVisible = false
game.playAmbientSound( "Engine.wav" )
if game.drillDone then
infoGroup.isVisible = false
start()
else
infoGroup.isVisible = true
end
end
function act:stop()
if endTimer then
timer.cancel( endTimer )
end
game.stopSound( drillSound.channel )
game.stopAmbientSound()
game.drillStopped = true
game.removeAct( "drillScan" )
game.removeAct( "drill" )
end
-- Generic new frame function
function newFrame()
droppingBar()
bar.difference = H / 2 - bar.height
costText.text = "Cost: " .. -game.currentDrillCost + math.abs( math.floor( bar.difference / 20 ) ) .. "%"
end
-- Dismisses the introduction information and sets a flag so it doesn't show up until the player restarts the application
function infoGroupDismiss( event )
if event.phase == "began" then
infoGroup.isVisible = false
game.drillDone = true
start()
end
return true
end
-- Function to begin all of the level's purposes
function start()
if startTimer.count > 0 and game.drillStopped == false then
startTimer.isVisible = true
startTimer.text = "Beginning in: " .. startTimer.count
startTimer.count = startTimer.count - 1
timer.performWithDelay( 1000, start )
elseif startTimer.count == 0 then
drillSound.sound = act:loadSound( "Drill.wav" )
drillSound.channel = game.playSound( drillSound.sound )
local function hideTimer()
startTimer.isVisible = false
end
tapTimerShow()
transition.to( startTimer, { time = 150, xScale = 0.01, yScale = 0.01, onComplete = hideTimer } )
costText.isVisible = true
transition.to( costText, { time = 150, xScale = 1, yScale = 1 } )
Runtime:addEventListener( "touch", risingBar )
Runtime:addEventListener( "enterFrame", newFrame )
-- Time limit
endTimer = timer.performWithDelay( 5000, timeLimit )
end
end
-- Function to show the tap text
function tapTimerShow()
tapText.isVisible = true
tapTime = tapTime - 0.23
timer.performWithDelay( 100, tapTimerHide )
end
-- Function to hide the tap text and test for the timer
function tapTimerHide()
tapText.isVisible = false
if tapTime > 0 then
timer.performWithDelay( 100, tapTimerShow )
end
end
-- Function to send you back to the mars rover game
function reset()
startTimer.count = 5
Runtime:removeEventListener( "touch", risingBar )
Runtime:removeEventListener( "enterFrame", newFrame )
bar.height = H / 4
costText.isVisible = false
range.isVisible = false
waterText.isVisible = false
game.removeAct( "drillScan" )
game.gotoAct( "rover", { effect = "zoomInOutFade", time = 333 } )
end
-- Per frame function to cause the bar to drop
function droppingBar()
if bar.height > 0 then
bar.height = bar.height - difficulty.drop
end
end
-- Touch function to cause the bar to rise
function risingBar( event )
if event.phase == "began" then
bar.height = bar.height + difficulty.rise
end
end
-- Function to control what happens when the player finishes drilling
function timeLimit()
endTimer = nil
Runtime:removeEventListener( "enterFrame", newFrame )
Runtime:removeEventListener( "touch", risingBar )
local x = math.abs( bar.difference )
range = display.newText( act.group, "", XC, YC + 20, native.systemFontBold, 25 )
range.fill = { 0, 0.42, 1 }
range.xScale, range.yScale = 0.01, 0.01
transition.to( range, { time = 100, xScale = 1, yScale = 1 } )
waterText.isVisible = true
transition.to( waterText, { time = 100, xScale = 1, yScale = 1 } )
if x <= 3 * H / 64 then
range.text = "Ideal range"
elseif x <= 6 * H / 32 then
range.text = "Good range"
elseif x <= 9 * H / 32 then
range.text = "Mediocre range"
elseif x <= 183 then
range.text = "Bad range"
elseif x <= H then
range.text = "Horrible range"
end
local energyCost = math.floor( -x / 10 )
local function resetVisible()
resetButton.isVisible = true
end
if game.energy() > ( -energyCost - game.currentDrillCost ) then
game.addEnergy( energyCost + game.currentDrillCost )
else
game.addEnergy( -game.energy() )
end
game.addWater( game.currentLiters)
game.currentLiters = 0
game.currentDrillCost = 0
game.drillDiff = 20.0
timer.performWithDelay( 1500, resetVisible )
end
return act.scene