-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoorLock.lua
309 lines (261 loc) · 8.43 KB
/
doorLock.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
-----------------------------------------------------------------------------------------
--
-- doorLock.lua
--
-- Door unlocking activity by Ryan Bains-Jordan
-----------------------------------------------------------------------------------------
-- Get local reference to the game globals
local game = globalGame
-- Create the act object
local act = game.newAct()
-- Act Requisites
local widget = require( "widget" )
------------------------- Start of Activity --------------------------------
local screen = {}
local button = {}
local numPic = {}
local numberCreate = {}
local numberDisplay = {}
local backTapped
local defaultPassKey = "1234"
local keyedCorrectly
local numberLocation
local refreshScreen
local clearKeyPad
local keyPressed
local colorNumbers = display.newGroup()
local sound = {}
-- When the Back Button is Pushed
function backTapped()
if game.cheatMode then
game.doorUnlocked = true
end
game.gotoAct( "mainAct", { effect = "slideRight", time = 500 } )
end
-- Checks to see if the numbers entered match the lock code
function checkKey()
local passKey = game.doorCode or defaultPassKey
if table.maxn(numberDisplay) < 4 then
return false
else
local keyEntered = numberDisplay[1]..numberDisplay[2]..numberDisplay[3]..numberDisplay[4]
if keyEntered == passKey then
return true
else
return false
end
end
end
-- Number Spritesheet Options
local options =
{
width = 27,
height = 50,
numFrames = 10,
sheetContentWidth = 270,
sheetContentHeight = 50
}
-- Normal, Green, & Red Numbers
local number = graphics.newImageSheet( "media/doorLock/numbers.png", options ) -- Red Number Sheet
local numberR = graphics.newImageSheet( "media/doorLock/numbers.png", options ) -- Red Number Sheet
local numberG = graphics.newImageSheet( "media/doorLock/numbers2.png", options ) -- Green Number Sheet
-- Calls the Blink Function Stored in Each Number Object
function flashTheNumbers()
for i = 1, #colorNumbers, 1 do
colorNumbers[i].blink( colorNumbers[i] )
end
end
-- Destroys the Number Objects
function removeFlashedNumbers()
for i = #colorNumbers, 1, -1 do
display.remove( colorNumbers[i] )
table.remove( colorNumbers[i] )
end
clearKeyPad() -- Remove Previously Typed Numbers
-- If door unlocked successfully then go back to mainAct to enter the room
if game.doorUnlocked then
game.gotoAct( "mainAct", { effect = "crossFade", time = 500 } )
end
end
-- Creates New Number Objects of the Requested Color
function colorToFlash( flashColor )
for i = 1, #numberDisplay, 1 do
local newNumber
if flashColor == "Green" then
newNumber = display.newImage( colorNumbers, numberG, numberDisplay[i]+1 )
else
newNumber = display.newImage( colorNumbers, numberR, numberDisplay[i]+1 )
end
newNumber.x = numberLocation( i )
newNumber.y = screen.y
newNumber.blink = function( self )
if self.isVisible then
self.isVisible = false
else
self.isVisible = true
end
end
table.insert( colorNumbers, newNumber )
end
-- Blink the Numbers and then Destroy
timer.performWithDelay( 200, flashTheNumbers, 6 )
timer.performWithDelay( 1200, removeFlashedNumbers )
end
-- Returns the X Value for the Display Position
function numberLocation( idx )
if idx == 1 then
return screen.x - 60
elseif idx == 2 then
return screen.x - 20
elseif idx == 3 then
return screen.x + 20
else
return screen.x + 60
end
end
-- Refresh the Screen every time the table is affected
function refreshScreen( )
-- Remove All Existing Number Objects
for i = #numberCreate, 1, -1 do
display.remove(numberCreate[i])
table.remove(numberCreate, i)
end
-- Flash Green if Correct, Flash Red if Incorrect, or Add a New Number to the Display
if keyedCorrectly == true then -- Create Green Numbers if Correct
game.doorUnlocked = true -- Tell mainAct that we unlocked the door
keyedCorrectly = nil
colorToFlash( "Green" )
elseif keyedCorrectly == false then -- Create Red Numbers if Wrong
keyedCorrectly = nil
colorToFlash( "Red" )
else
if table.maxn(numberDisplay) <= 4 then -- Else Create Normal Red Numbers
for i = 1, #numberDisplay, 1 do
local newNumber = display.newImage( act.group, number, numberDisplay[i]+1)
newNumber.x = numberLocation( i )
newNumber.y = screen.y
newNumber.blink = function( self )
if self.isVisible then
self.isVisible = false
else
self.isVisible = true
end
end
table.insert( numberCreate, newNumber )
end
end
end
end
-- Clears the Screen
function clearKeyPad()
for i = #numberDisplay, 1, -1 do
table.remove( numberDisplay, i )
end
print("Clearing Key Pad")
end
-- The Listener for the Buttons
function keyPressed( event )
local key = event.target.name
local r = math.random( 1, 4 )
game.playSound( sound.button[r] )
-- If Clear Key is Pressed
if key == "clr" then
clearKeyPad()
-- If Enter Key is Pressed
elseif key == "ent" then
if checkKey() then
keyedCorrectly = true
game.playSound( sound.pass )
if game.saveState.soundOn then
system.vibrate( )
end
else
keyedCorrectly = false
game.playSound( sound.fail )
end
-- If Any Other Key is Pressed
elseif key ~= "clr" and key ~= "ent" then
if table.maxn(numberDisplay) < 4 then
table.insert( numberDisplay, key )
end
end
if table.maxn(numberDisplay) <= 4 then
refreshScreen()
end
end
-- Convenience Function
local function createButton( scene, name, x, y, file, file2 )
local b = widget.newButton {
defaultFile = file,
overFile = file2,
width = 50,
height = 50,
x = x, y = y,
onPress = keyPressed
}
scene:insert(b)
b.name = name
return b
end
-- Initiate the Activity (Create)
function act:init()
-- Background
local bg = display.newRect( act.group, act.xCenter, act.yCenter, act.width, act.height )
bg:setFillColor( 0.3 ) -- dark gray
-- Title Bar
act:makeTitleBar( "", backTapped ) -- title set in act:prepare()
-- Sound
sound.button = {
act:loadSound( "sounds/button1.wav" ),
act:loadSound( "sounds/button2.wav" ),
act:loadSound( "sounds/button3.wav" ),
act:loadSound( "sounds/button4.wav" )
}
sound.pass = act:loadSound( "sounds/pass.wav" )
sound.fail = act:loadSound( "sounds/fail.wav" )
-- Screen
screen = act:newImage( "screen.png", { width=200 })
screen.x = act.xCenter
screen.y = act.yCenter - 120
-- Button Panel
local panel = act:newImage( "panel.png", { width=250 })
panel.x = act.xCenter
panel.y = act.yCenter + 70
-- Buttons
button.one = createButton( act.group, 1, panel.x - 60, panel.y - 90,
"media/doorLock/buttons/1key.png", "media/doorLock/buttons/1key_p.png" )
button.two = createButton( act.group, 2, panel.x, panel.y - 90,
"media/doorLock/buttons/2key.png", "media/doorLock/buttons/2key_p.png" )
button.three = createButton( act.group, 3, panel.x + 60, panel.y - 90,
"media/doorLock/buttons/3key.png", "media/doorLock/buttons/3key_p.png" )
button.four = createButton( act.group, 4, panel.x - 60, panel.y - 30,
"media/doorLock/buttons/4key.png", "media/doorLock/buttons/4key_p.png" )
button.five = createButton( act.group, 5, panel.x, panel.y - 30,
"media/doorLock/buttons/5key.png", "media/doorLock/buttons/5key_p.png" )
button.six = createButton( act.group, 6, panel.x + 60, panel.y - 30,
"media/doorLock/buttons/6key.png", "media/doorLock/buttons/6key_p.png" )
button.seven = createButton( act.group, 7, panel.x - 60, panel.y + 30,
"media/doorLock/buttons/7key.png", "media/doorLock/buttons/7key_p.png" )
button.eight = createButton( act.group, 8, panel.x, panel.y + 30,
"media/doorLock/buttons/8key.png", "media/doorLock/buttons/8key_p.png" )
button.nine = createButton( act.group, 9, panel.x + 60, panel.y + 30,
"media/doorLock/buttons/9key.png", "media/doorLock/buttons/9key_p.png" )
button.clear = createButton( act.group, "clr", panel.x - 60, panel.y + 90,
"media/doorLock/buttons/clear.png", "media/doorLock/buttons/clear_p.png" )
button.zero = createButton( act.group, 0, panel.x, panel.y + 90,
"media/doorLock/buttons/0key.png", "media/doorLock/buttons/0key_p.png" )
button.enter = createButton( act.group, "ent", panel.x + 60, panel.y + 90,
"media/doorLock/buttons/enter.png", "media/doorLock/buttons/enter_p.png" )
end
-- Prepare the act
function act:prepare()
-- Set title bar text to the name of the room
if game.lockedRoom then
act.title.text = game.lockedRoom.name
else
act.title.text = "Locked Door"
end
end
------------------------- End of Activity --------------------------------
-- Corona needs the scene object returned from the act file
return act.scene