-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingleClickDetectorTestScene.lua
More file actions
417 lines (351 loc) · 15.9 KB
/
SingleClickDetectorTestScene.lua
File metadata and controls
417 lines (351 loc) · 15.9 KB
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
-- multiplayer Area Control mode scene.
local composer = require("composer")
local scene = composer.newScene()
require("UIParts")
require("database")
require("dataTracker")
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
local bigGrid = true
local cellCollection = {} -- main background map tiles
local overlayCollection = {} -- AreaControl map tiles, translucent
local touchDetector = {} -- Determines what cell10 was tapped on the screen.
local timerResults = nil
local firstRun = true
local locationText = ""
local explorePointText = ""
local scoreText = ""
local teamScoreText = ""
local timeText = ""
local directionArrow = ""
local scoreLog = ""
local debugText = {}
local locationName = ""
local arrowPainted = false
local scoreCheckCounter = 0
local team1Score = "0"
local team2Score = "0"
local team3Score = "0"
function team1ScoreListener(event)
if (event.status == 200) then
team1Score = event.response
end
end
function team2ScoreListener(event)
if (event.status == 200) then
team2Score = event.response
end
end
function team3ScoreListener(event)
if (event.status == 200) then
team3Score = event.response
end
end
local function testDrift()
if (os.time() % 2 == 0) then
currentPlusCode = shiftCell(currentPlusCode, 1, 9) -- move north
else
currentPlusCode = shiftCell(currentPlusCode, 1, 10) -- move west
end
end
local function ToggleZoom()
print("zoom tapped")
bigGrid = not bigGrid
local sceneGroup = scene.view
timer.pause(timerResults)
for i = 1, #cellCollection do cellCollection[i]:removeSelf() end
for i = 1, #overlayCollection do overlayCollection[i]:removeSelf() end
cellCollection = {}
overlayCollection = {}
if (bigGrid) then
CreateRectangleGrid(3, 320, 400, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(3, 320, 400, sceneGroup, overlayCollection) -- rectangular Cell11 grid with area control overlay
else
CreateRectangleGrid(5, 160, 200, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(5, 160, 200, sceneGroup, overlayCollection) -- rectangular Cell11 grid with area control overlay
end
for square = 1, #overlayCollection do
overlayCollection[square]:toBack()
cellCollection[square]:toBack() --same count
end
touchDetector:toBack()
directionArrow:toFront()
forceRedraw = true
timer.resume(timerResults)
return true
end
--"touch" event
local function DetectLocationClick(event)
print("Detecting location")
-- we have a click somewhere in our rectangle.
-- zoomed out grid is 5x5 lowres tiles, 800 x 1000 total, each pixel is 1 Cell 11
-- zoomed in grid is 3x3 highres tiles, 960 x 1200 total, each 2x2 pixels is 1 Cell 11
-- figure out how many pixels from the center of the item each tap is
-- divide those distances by 16 and 20 (or 8 and 10) to get cell10s away from center of image.
local screenX = event.x
local screenY = event.y
local centerX = display.contentCenterX
local centerY = display.contentCenterY
--remember that the CENTER of the center square is the center pixel on screen, not the SW corner
--so i have to shift info by half a square somewhere.
local pixelshiftX = screenX - centerX
local pixelshiftY = centerY - screenY --flips the sign to get things to line up correctly.
local plusCodeShiftX = 0
local plusCodeShiftY = 0
if (bigGrid) then
pixelshiftX = pixelshiftX + 8
pixelshiftY = pixelshiftY + 10
plusCodeShiftX = pixelshiftX / 16
plusCodeShiftY = pixelshiftY / 20
else
pixelshiftX = pixelshiftX + 4
pixelshiftY = pixelshiftY + 5
plusCodeShiftX = pixelshiftX / 8
plusCodeShiftY = pixelshiftY / 10
end
local newCell = currentPlusCode:sub(0,8) .. "+FF" --might be GG, depends on direction of shift
newCell = shiftCell(newCell, plusCodeShiftY, 9) --Y axis
newCell = shiftCell(newCell, plusCodeShiftX, 10) --X axis
print("Detected cell: " .. newCell)
tapData.text = "Cell Tapped: " .. newCell
local pluscodenoplus = removePlus(newCell)
local terrainInfo = LoadTerrainData(pluscodenoplus)
-- 3 is name, 4 is area type, 6 is mapDataID (privacyID)
if (terrainInfo[3] == "") then
tappedAreaName = terrainInfo[4]
else
tappedAreaName = terrainInfo[3]
end
tappedCell = newCell
tappedAreaScore = 0 --i don't save this locally, this requires a network call to get and update
tappedAreaMapDataId = terrainInfo[6]
composer.showOverlay("overlayMPAreaClaim", {isModal = true})
end
local function GoToSceneSelect()
print("back to scene select")
local options = {effect = "flip", time = 125}
composer.gotoScene("SceneSelect", options)
return true
end
local function tintArrow(teamID)
if (teamID == 1) then
directionArrow:setFillColor(1, 0, 0, .5)
elseif (teamID == 2) then
directionArrow:setFillColor(0, 1, 0, .5)
elseif (teamID == 3) then
directionArrow:setFillColor(0, 0, 1, .5)
end
end
local function UpdateLocalOptimized()
-- This needs to be 2 loops, because the cell tables are different sizes.
-- First loop for map tiles
-- Then loop for touch event rectangles.
if timerResults == nil then
timerResults = timer.performWithDelay(150, UpdateLocalOptimized, -1)
end
if not playerInBounds then
return
end
if (debugLocal) then print("start UpdateLocalOptimized") end
if (currentPlusCode == "") then
if (debugLocal) then print("skipping, no location.") end
return
end
if (debug) then debugText.text = dump(lastLocationEvent) end
if (timerResults ~= nil) then timer.pause(timerResults) end
local innerForceRedraw = forceRedraw or firstRun or (currentPlusCode:sub(1,8) ~= previousPlusCode:sub(1,8))
firstRun = false
forceRedraw = false
previousPlusCode = currentPlusCode
if (arrowPainted == false) then
if (factionID == 0) then
GetTeamAssignment()
else
tintArrow(factionID)
arrowPainted = true
end
end
-- Step 1: set background MAC map tiles for the Cell8.
if (innerForceRedraw == false) then -- none of this needs to get processed if we haven't moved and there's no new maptiles to refresh.
for square = 1, #cellCollection do
-- check each spot based on current cell, modified by gridX and gridY
local thisSquaresPluscode = currentPlusCode
thisSquaresPluscode = shiftCell(thisSquaresPluscode, cellCollection[square].gridX, 8)
thisSquaresPluscode = shiftCell(thisSquaresPluscode, cellCollection[square].gridY, 7)
cellCollection[square].pluscode = thisSquaresPluscode
local plusCodeNoPlus = removePlus(thisSquaresPluscode):sub(1, 8)
GetMapData8(plusCodeNoPlus)
local imageRequested = requestedMapTileCells[plusCodeNoPlus] -- read from DataTracker because we want to know if we can paint the cell or not.
local imageExists = doesFileExist(plusCodeNoPlus .. "-11.png", system.CachesDirectory)
if (imageRequested == nil) then
imageExists = doesFileExist(plusCodeNoPlus .. "-11.png", system.CachesDirectory)
end
if (imageExists == false or imageExists == nil) then
GetMapTile8(plusCodeNoPlus)
else
cellCollection[square].fill = {0.1, 0.1} -- required to make Solar2d actually update the texture.
local paint = {
type = "image",
filename = plusCodeNoPlus .. "-11.png",
baseDir = system.CachesDirectory
}
cellCollection[square].fill = paint
end
imageRequested = requestedMPMapTileCells[plusCodeNoPlus] -- read from DataTracker because we want to know if we can paint the cell or not.
imageExists = doesFileExist(plusCodeNoPlus .. "-AC-11.png", system.TemporaryDirectory)
if (imageRequested == nil) then
imageExists = doesFileExist(plusCodeNoPlus .. "-AC-11.png", system.TemporaryDirectory)
end
if (imageExists == false or imageExists == nil) then
GetTeamControlMapTile8(plusCodeNoPlus)
else
overlayCollection[square].fill = {0, 0} -- required to make Solar2d actually update the texture.
local paint = {
type = "image",
filename = plusCodeNoPlus .. "-AC-11.png",
baseDir = system.TemporaryDirectory
}
overlayCollection[square].fill = paint
end
end
end
--update team scores
scoreCheckCounter = scoreCheckCounter - 1
if (scoreCheckCounter <= 0) then
GetMyScore()
network.request(serverURL .. "Data/Global/scoreTeam1" .. defaultQueryString, "GET", team1ScoreListener)
network.request(serverURL .. "Data/Global/scoreTeam2" .. defaultQueryString, "GET", team2ScoreListener)
network.request(serverURL .. "Data/Global/scoreTeam3" .. defaultQueryString, "GET", team3ScoreListener)
scoreCheckCounter = 24
end
if (timerResults ~= nil) then timer.resume(timerResults) end
if (debugLocal) then print("grid done or skipped") end
locationText.text = "Current location:" .. currentPlusCode
explorePointText.text = "Explore Points: " .. Score()
scoreText.text = "Control Score: " .. composer.getVariable("myScore")
teamScoreText.text = "Red Team: " .. team1Score .. "\nGreen Team: " .. team2Score .. "\nBlue Team: " .. team3Score
timeText.text = "Current time:" .. os.date("%X")
directionArrow.rotation = currentHeading
--Remember, currentPlusCode has the +, so i want chars 10 and 11, not 9 and 10.
--Shift is how many blocks to move. Multiply it by how big each block is. These offsets place the arrow in the correct Cell10.
local shift = CODE_ALPHABET_:find(currentPlusCode:sub(11, 11)) - 11
local shift2 = CODE_ALPHABET_:find(currentPlusCode:sub(10, 10)) - 10
if (bigGrid) then
directionArrow.x = display.contentCenterX + (shift * 16) + 8
directionArrow.y = display.contentCenterY - (shift2 * 20) + 10
else
directionArrow.x = display.contentCenterX + (shift * 8) + 4
directionArrow.y = display.contentCenterY - (shift2 * 10) + 5
end
scoreLog.text = lastScoreLog
locationText:toFront()
explorePointText:toFront()
scoreText:toFront()
teamScoreText:toFront()
timeText:toFront()
directionArrow:toFront()
scoreLog:toFront()
locationName:toFront()
if (debugLocal) then print("end updateLocalOptimized") end
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
function scene:create(event)
composer.setVariable("myScore", "0")
if (debug) then print("creating MPAreaControlScene2") end
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
touchDetector = display.newRect(sceneGroup, display.contentCenterX, display.contentCenterY, 720, 1280)
touchDetector:addEventListener("tap", DetectLocationClick)
touchDetector.fill = {0, 0.1}
touchDetector:toBack()
contrastSquare = display.newRect(sceneGroup, display.contentCenterX, 270, 400, 200)
contrastSquare:setFillColor(.8, .8, .8, .7)
locationText = display.newText(sceneGroup, "Current location:" .. currentPlusCode, display.contentCenterX, 200, native.systemFont, 20)
timeText = display.newText(sceneGroup, "Current time:" .. os.date("%X"), display.contentCenterX, 220, native.systemFont, 20)
explorePointText = display.newText(sceneGroup, "Explore Points: ?", display.contentCenterX, 240, native.systemFont, 20)
scoreText = display.newText(sceneGroup, "Control Score: ?", display.contentCenterX, 260, native.systemFont, 20)
locationName = display.newText(sceneGroup, "", display.contentCenterX, 280, native.systemFont, 20)
teamScoreText = display.newText(sceneGroup, "Team Scores", display.contentCenterX, 300, native.systemFont, 20)
teamScoreText.anchorY = 0
scoreLog = display.newText(sceneGroup, "", display.contentCenterX, 1220, native.systemFont, 20)
locationText:setFillColor(0, 0, 0);
timeText:setFillColor(0, 0, 0);
explorePointText:setFillColor(0, 0, 0);
scoreText:setFillColor(0, 0, 0);
teamScoreText:setFillColor(0, 0, 0);
scoreLog:setFillColor(0, 0, 0);
locationName:setFillColor(0, 0, 0);
if (bigGrid) then
CreateRectangleGrid(3, 320, 400, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(3, 320, 400, sceneGroup, overlayCollection) -- rectangular Cell11 grid with map tiles
else
CreateRectangleGrid(5, 160, 200, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(5, 160, 200, sceneGroup, overlayCollection) -- rectangular Cell11 grid with map tiles
end
directionArrow = display.newImageRect(sceneGroup, "themables/arrow1.png", 16, 20)
directionArrow.x = display.contentCenterX
directionArrow.y = display.contentCenterY
directionArrow.anchorX = .5
directionArrow.anchorY = .5
directionArrow:toFront()
local header = display.newImageRect(sceneGroup, "themables/MultiplayerAreaControl.png",300, 100)
header.x = display.contentCenterX
header.y = 100
header:addEventListener("tap", GoToSceneSelect)
header:toFront()
local zoom = display.newImageRect(sceneGroup, "themables/ToggleZoom.png", 100, 100)
zoom.anchorX = 0
zoom.x = 50
zoom.y = 100
zoom:addEventListener("tap", ToggleZoom)
if (debug) then
debugText = display.newText(sceneGroup, "location data", display.contentCenterX, 1180, 600, 0, native.systemFont, 22)
debugText:toFront()
end
zoom:toFront()
contrastSquare:toFront()
if (debug) then print("created AreaControl scene") end
end
function scene:show(event)
if (debug) then print("showing AreaControl scene") end
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
-- Code here runs when the scene is still off screen (but is about to come on screen)
firstRun = true
elseif (phase == "did") then
-- Code here runs when the scene is entirely on screen
tintArrow(factionID)
timer.performWithDelay(50, UpdateLocalOptimized, 1)
if (debugGPS) then timer.performWithDelay(3000, testDrift, -1) end
end
end
function scene:hide(event)
if (debug) then print("hiding AreaControl scene") end
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
timer.cancel(timerResults)
timerResults = nil
elseif (phase == "did") then
-- Code here runs immediately after the scene goes entirely off screen
end
end
function scene:destroy(event)
if (debug) then print("destroying AreaControl scene") end
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)
-- -----------------------------------------------------------------------------------
return scene