forked from dyerw/Inkwell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameplay.lua
392 lines (304 loc) · 10 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
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 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 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
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 onTileTouch ( event )
if (event.phase == "began") then
gridX = translateToGridX(event.target.x)
gridY = translateToGridY(event.target.y)
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 )
rect.fill = { 66 / 255, 134 / 255, 244 / 255, 0.01 }
sceneGroup:insert(rect)
gridX = translateToGridX(x)
gridY = translateToGridY(y)
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.a = 0.01
end
end
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()
end
end
for x=1, #labelGrid do
for y=1, #(labelGrid[x]) do
labelGrid[x][y].text = letterGrid[x][y]
end
end
end
local function updateHealthLabel()
playerHealthBar.width = (playerHealth / 200) * 100
playerHealthBar.x = (playerHealthBar.width / 2) + 10
end
local function enemyAction()
print("hello")
enemyWordLabel.text = "Enemy making move..."
print("hi")
local enemyWord = makeMove(letterGrid, aiWords)
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 ()
print(selectedWord)
whiteOutTiles()
local validWord = table.binsearch(words, selectedWord) ~= nil
print(validWord)
if (validWord) then
audio.play( successSoundEffect )
enemyHealth = enemyHealth - string.len(selectedWord) * string.len(selectedWord)
updateEnemyHealthLabel()
if enemyHealth <= 0 then
composer.gotoScene( "gameover", {
effect = "fade",
time = 400,
params = {
didWin = true,
}
} )
end
enemyAction()
refreshGrid()
else
audio.play(failSoundEffect)
end
selectedWord = ""
wordLabel.text = selectedWord
damageLabel.text = "0"
selectedPoints = {}
end
function scene:create( event )
successSoundEffect = audio.loadSound("soundeffects/success.mp3")
failSoundEffect = audio.loadSound("soundeffects/failure.mp3")
composer.removeHidden( )
print("you chose to play as: " .. event.params.character)
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()
-- 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)), top=(screenH - 90)
}
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 })
-- all display objects must be inserted into group
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