-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameplay.lua
503 lines (388 loc) · 13.3 KB
/
gameplay.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
local composer = require( "composer" )
require("wordlookup")
require("ai")
local widget = require "widget"
local scene = composer.newScene()
-- forward declarations and other locals
local screenW, screenH, halfW, halfH = display.actualContentWidth, display.actualContentHeight, display.contentCenterX, display.contentCenterY
local sceneGroup = nil
local gridSize = 4
local tileSize = screenW / gridSize
local gridOffset = 150
local originalLetterGrid = {}
local letterGrid = {}
local labelGrid = {}
local tileGrid = {}
local selectedWord = ""
local selectedPoints = {}
local lastClicked = {x=nil, y=nil}
local wordLabel = nil
local healthAmountLabel = nil
local enemyHealthAmountLabel = nil
local damageLabel = nil
local enemyWordLabel = nil
local energyLabel = nil
local energyAmount = 0
local difficulty = nil
local powerIsActivated = false
local character
local energyRequired = {lovecraft = 1,will = 1}
-- Lovecraft Only stuff
local activatePowerBtn = nil
local destroyedTiles = {}
--
-- Shakespeare Only stuff
local tileToBeSwitched = nil
--
local energyTilePosition = {x=nil, y=nil}
local words = {}
local aiWords = {}
local playerHealth = 200
local enemyHealth = 200
local playerHealthBar
local enemyHealthBar
local lastEnemyWord = ""
local successSoundEffect = nil
local failSoundEffect = nil
function round(x)
return math.floor(x + 0.5)
end
function randomLetter()
letters = "AAAAAAAAABBCCDDDDEEEEEEEEEEEEFFGGGHHIIIIIIIIIJKLLLLMMNNNNNNOOOOOOOOPPQRRRRRRSSSSTTTTTTUUUUVVWWXYYZ"
index = math.random(string.len(letters))
return string.sub(letters, index, index)
end
function initializeLetterGrid()
for x=1, gridSize, 1 do
letterGrid[x] = {}
for y=1, gridSize, 1 do
letterGrid[x][y] = randomLetter()
end
end
for x=1, gridSize, 1 do
originalLetterGrid[x] = {}
for y=1, gridSize, 1 do
originalLetterGrid[x][y] = randomLetter()
end
end
end
function translateToGridX(pixelX)
return round( (pixelX - (0.5 * tileSize)) / tileSize + 1 )
end
function translateToGridY(pixelY)
return round( ((pixelY - gridOffset - (0.5 * tileSize)) / tileSize) + 1 )
end
function isAdjacentToLastClicked(x, y)
if #selectedPoints == 0 then
return true
end
local lastX = selectedPoints[#selectedPoints]["x"]
local lastY = selectedPoints[#selectedPoints]["y"]
local dX = math.abs(lastX - x)
local dY = math.abs(lastY - y)
if dX <= 1 and dY <= 1 then
return true
else
return false
end
end
function updateLetters()
for x=1, #labelGrid do
for y=1, #(labelGrid[x]) do
labelGrid[x][y].text = letterGrid[x][y]
end
end
end
function onTileTouch ( event )
if (event.phase == "began") then
gridX = translateToGridX(event.target.x)
gridY = translateToGridY(event.target.y)
if powerIsActivated and character == 'lovecraft' then
destroyedTiles[#destroyedTiles + 1] = {x=gridX, y=gridY}
local img = display.newImage('void.png', event.target.x, event.target.y)
img.height = event.target.width - 10
img.width = event.target.height - 10
sceneGroup:insert(img)
powerIsActivated = false
return
end
if powerIsActivated and character == 'will' then
if not tileToBeSwitched then
tileToBeSwitched = {x = gridX, y = gridY}
return
end
local tmp = letterGrid[gridX][gridY]
letterGrid[gridX][gridY] = letterGrid[tileToBeSwitched.x][tileToBeSwitched.y]
letterGrid[tileToBeSwitched.x][tileToBeSwitched.y] = tmp
updateLetters()
powerIsActivated = false
tileToBeSwitched = nil
return
end
if isAdjacentToLastClicked(gridX, gridY) then
letter = letterGrid[gridX][gridY]
selectedWord = selectedWord .. letter
local newPoint = {x=gridX, y=gridY}
selectedPoints[#selectedPoints + 1] = newPoint
wordLabel.text = selectedWord
damageLabel.text = tostring(string.len(selectedWord) * string.len(selectedWord))
tileGrid[gridX][gridY].fill.a = tileGrid[gridX][gridY].fill.a + 0.5
end
end
end
function drawTile(x, y)
local backgroundRect = display.newRect(x, y, tileSize, tileSize)
sceneGroup:insert(backgroundRect)
backgroundRect.fill = { 1 }
rect = display.newRect( x, y, tileSize, tileSize)
rect.strokeWidth = 2
rect:setStrokeColor( black )
rect:addEventListener( "touch", onTileTouch )
gridX = translateToGridX(x)
gridY = translateToGridY(y)
if ( gridX == energyTilePosition['x'] and gridY == energyTilePosition['y']) then
rect.fill = { 252 / 255, 246 / 255, 63 / 255, 1 }
else
rect.fill = { 66 / 255, 134 / 255, 244 / 255, 0.01 }
end
sceneGroup:insert(rect)
text = display.newText( { text=letterGrid[gridX][gridY], font=native.systemFontBold,
x=x, y=y } )
text:setFillColor( black )
sceneGroup:insert(text)
labelGrid[gridX][gridY] = text
tileGrid[gridX][gridY] = rect
end
-- Draws a row across entire screen
function drawRow(y)
for i=1, gridSize, 1 do
drawTile((i - 1) * tileSize + (0.5 * tileSize), y)
end
end
function drawGrid(y)
for i=1, gridSize, 1 do
drawRow(y + ((i - 1) * tileSize + (0.5 * tileSize)))
end
end
function initializeWords()
for line in io.lines(system.pathForFile('all-words.txt', system.ResourceDirectory)) do
words[#words + 1] = line
end
for line in io.lines(system.pathForFile('wiki-100k.txt', system.ResourceDirectory)) do
aiWords[#aiWords + 1] = line
end
end
function initializeLabelGrid()
for i=1, gridSize do
labelGrid[i] = {}
end
end
function initializeTileGrid()
for i=1, gridSize do
tileGrid[i] = {}
end
end
function whiteOutTiles()
for x=1, gridSize do
for y=1, gridSize do
tileGrid[x][y].fill = { 66 / 255, 134 / 255, 244 / 255, 0.01 }
end
end
tileGrid[energyTilePosition.x][energyTilePosition.y].fill = { 252 / 255, 246 / 255, 63 / 255, 1 }
end
local function updateEnemyHealthLabel()
enemyHealthBar.width = (enemyHealth / 200) * 100
enemyHealthBar.x = screenW - ((enemyHealthBar.width / 2) + 10)
end
local function refreshGrid()
whiteOutTiles()
for x=1, #letterGrid do
for y=1, #(letterGrid[x]) do
letterGrid[x][y] = randomLetter()
originalLetterGrid[x][y] = letterGrid[x][y]
end
end
updateLetters()
end
local function updateHealthLabel()
playerHealthBar.width = (playerHealth / 200) * 100
playerHealthBar.x = (playerHealthBar.width / 2) + 10
end
local function enemyAction()
enemyWordLabel.text = "Enemy making move..."
-- Remove destroyedTiles
for i=1,#destroyedTiles do
originalLetterGrid[destroyedTiles[i].x][destroyedTiles[i].y] = ""
end
local enemyWord = makeMove(originalLetterGrid, aiWords, difficulty)
lastEnemyWord = enemyWord
local wordLength = string.len(enemyWord)
playerHealth = playerHealth - wordLength * wordLength
enemyWordLabel.text = "Last Enemy Move: " .. enemyWord
updateHealthLabel()
if playerHealth <= 0 then
composer.gotoScene( "gameover", {
effect = "fade",
time = 400,
params = {
didWin = false,
enemyWord = lastEnemyWord
}
} )
end
end
local function onSubmitRelease ()
whiteOutTiles()
local validWord = table.binsearch(words, selectedWord) ~= nil
if (validWord) then
audio.play( successSoundEffect )
enemyHealth = enemyHealth - string.len(selectedWord) * string.len(selectedWord)
updateEnemyHealthLabel()
for i=1,#selectedPoints do
if (selectedPoints[i].x == energyTilePosition.x and selectedPoints[i].y == energyTilePosition.y) then
energyAmount = energyAmount + 1
end
end
energyLabel.text = "ENERGY: " .. tostring(energyAmount)
if enemyHealth <= 0 then
composer.gotoScene( "gameover", {
effect = "fade",
time = 400,
params = {
didWin = true,
}
} )
end
enemyAction()
energyTilePosition = { x = math.random(gridSize), y = math.random(gridSize) }
refreshGrid()
else
audio.play(failSoundEffect)
end
selectedWord = ""
wordLabel.text = selectedWord
damageLabel.text = "0"
selectedPoints = {}
end
function onPowerActivate()
print(character)
if energyAmount < energyRequired[character] then
audio.play(failSoundEffect)
return
end
energyAmount = energyAmount - energyRequired[character]
energyLabel.text = "ENERGY: 0"
powerIsActivated = true
end
function scene:create( event )
successSoundEffect = audio.loadSound("soundeffects/success.mp3")
failSoundEffect = audio.loadSound("soundeffects/failure.mp3")
composer.removeHidden( )
character = event.params.character
difficulty = event.params.difficulty
sceneGroup = self.view
-- Called when the scene's view does not exist.
--
-- INSERT code here to initialize the scene
-- e.g. add display objects to 'sceneGroup', add touch listeners, etc.
math.randomseed( os.time() )
initializeLetterGrid()
initializeWords()
initializeLabelGrid()
initializeTileGrid()
energyTilePosition={ x = math.random(gridSize), y = math.random(gridSize)}
-- create a grey rectangle as the backdrop
-- the physical screen will likely be a different shape than our defined content area
-- since we are going to position the background from it's top, left corner, draw the
-- background at the real top, left corner.
local background = display.newRect( display.screenOriginX, display.screenOriginY, screenW, screenH )
background.anchorX = 0
background.anchorY = 0
background:setFillColor( 0 )
sceneGroup:insert( background )
drawGrid(gridOffset)
local doneButton = widget.newButton {
label="Done",
labelColor = { default={255}, over={128} },
width=154, height=40,
onRelease = onSubmitRelease,
left=(halfW - (154 / 2) + 20), top=(screenH - 80)
}
wordLabel = display.newText( { text="", font=native.systemFontBold,
x=halfW, y=10, fontSize=40 } )
playerHealthBar = display.newRect(60, 90, 100, 25)
playerHealthBar:setFillColor(0, 1, 0)
enemyHealthBar = display.newRect(screenW - 60, 90, 100, 25)
enemyHealthBar:setFillColor(1, 0, 0)
enemyWordLabel = display.newText ( {text=tostring("Last Enemy Move: --"), font=native.systemFontBold,
x=halfW, y=120, fontSize=12})
damageLabel = display.newText( { text="0", font=native.systemFontBold, x = halfW, y = 50, fontSize=30 })
energyLabel = display.newText( { text="ENERGY: 0", font=native.systemFontBold, x = (halfW / 2), y = 50, fontSize = 15 })
energyLabel:setFillColor( 252 / 255, 246 / 255, 63 / 255, 1 )
activatePowerBtn = widget.newButton {
label="Activate",
labelColor = { default={255}, over={128} },
width=80, height=40,
onRelease = onPowerActivate,
left=(halfW / 2) - 40, top=(screenH - 80)
}
-- all display objects must be inserted into group
if activatePowerBtn then
sceneGroup:insert(activatePowerBtn)
end
sceneGroup:insert(energyLabel)
sceneGroup:insert(damageLabel)
sceneGroup:insert(enemyHealthBar)
sceneGroup:insert(playerHealthBar)
sceneGroup:insert(rect)
sceneGroup:insert(wordLabel)
sceneGroup:insert(doneButton)
sceneGroup:insert( enemyWordLabel )
end
function scene:show( event )
composer.removeHidden()
local sceneGroup = self.view
local phase = event.phase
if phase == "will" then
-- Called when the scene is still off screen and is about to move on screen
elseif phase == "did" then
-- Called when the scene is now on screen
--
-- INSERT code here to make the scene come alive
-- e.g. start timers, begin animation, play audio, etc.
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
sceneGroup.active = false
if event.phase == "will" then
-- Called when the scene is on screen and is about to move off screen
--
-- INSERT code here to pause the scene
-- e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == "did" then
-- Called when the scene is now off screen
end
end
function scene:destroy( event )
-- Called prior to the removal of scene's "view" (sceneGroup)
--
-- INSERT code here to cleanup the scene
-- e.g. remove display objects, remove touch listeners, save state, etc.
-- local sceneGroup = self.view
if doneButton then
doneButton:removeSelf();
doneButton = nil
end
audio.dispose( successSoundEffect )
audio.dispose( failSoundEffect )
end
---------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-----------------------------------------------------------------------------------------
return scene