-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasePlayableScene.lua
More file actions
357 lines (301 loc) · 14.5 KB
/
basePlayableScene.lua
File metadata and controls
357 lines (301 loc) · 14.5 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
-- the minimum stuff to get a scene with maptiles going. Copy and build upon it for new modes with maptiles.
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 gridzoom = 2 -- 1, 2, 3.
local cellCollection = {} -- main background map tiles
local overlayCollection = {} -- any overlay tiles needed.
local touchDetector = {} -- Determines what cell10 was tapped on the screen.
local timerResults = nil
local firstRun = true
local mapTileUpdater = nil
local locationText = ""
local timeText = ""
local directionArrow = ""
local debugText = {}
local locationName = ""
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")
gridzoom = gridzoom + 1
if (gridzoom > 3) then gridzoom = 1 end
timer.pause(timerResults)
for i = 1, #cellCollection do cellCollection[i]:removeSelf() end
for i = 1, #overlayCollection do overlayCollection[i]:removeSelf() end
cellCollection = {}
overlayCollection = {}
makeGrid()
directionArrow:toFront()
forceRedraw = true
timer.resume(timerResults)
return true
end
function makeGrid()
local sceneGroup = scene.view
if (gridzoom == 1) then
CreateRectangleGrid(3, 640, 800, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(3, 640, 800, sceneGroup, overlayCollection) -- rectangular Cell11 grid with overlay
elseif (gridzoom == 2) then
CreateRectangleGrid(3, 320, 400, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(3, 320, 400, sceneGroup, overlayCollection) -- rectangular Cell11 grid with overlay
elseif (gridzoom == 3) then
CreateRectangleGrid(5, 160, 200, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(5, 160, 200, sceneGroup, overlayCollection) -- rectangular Cell11 grid with overlay
end
for square = 1, #overlayCollection do
overlayCollection[square]:toBack()
cellCollection[square]:toBack() --same count
end
touchDetector:toBack()
end
--"tap" event
local function DetectLocationClick(event)
print("Detecting location")
-- we have a click somewhere in our rectangle.
-- gridzoom3 is 5x5 lowres tiles, 800 x 1000 total, each pixel is 1 Cell 11
-- gridzoom2 is 3x3 highres tiles, 960 x 1200 total, each 2x2 pixels is 1 Cell 11
-- gridzoom1 is 3x3 doubled highres tiles, 1960 x 2400 total, each 4x4 pixels is 1 cell 11.
-- figure out how many pixels from the center of the item each tap is
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 (gridzoom == 1) then
pixelshiftX = pixelshiftX + 16
pixelshiftY = pixelshiftY + 20
plusCodeShiftX = pixelshiftX / 32
plusCodeShiftY = pixelshiftY / 40
elseif (gridzoom == 2) then
pixelshiftX = pixelshiftX + 8
pixelshiftY = pixelshiftY + 10
plusCodeShiftX = pixelshiftX / 16
plusCodeShiftY = pixelshiftY / 20
elseif (gridzoom == 3) then
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 tap: " .. 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 UpdateLocalOptimized()
if timerResults == nil then
timerResults = timer.performWithDelay(450, 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
-- 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)
--checkTileGeneration(plusCodeNoPlus, "mapTiles")
local imageExists = doesFileExist(plusCodeNoPlus .. "-11.png", system.CachesDirectory)
if imageExists == true then
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
--Update this loop to pull the overlay tiles if needed
-- 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
if (timerResults ~= nil) then timer.resume(timerResults) end
if (debugLocal) then print("grid done or skipped") end
locationText.text = "Current location:" .. currentPlusCode
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 (gridzoom == 1) then
directionArrow.x = display.contentCenterX + (shift * 32) + 16
directionArrow.y = display.contentCenterY - (shift2 * 40) + 20
elseif (gridzoom == 2) then
directionArrow.x = display.contentCenterX + (shift * 16) + 8
directionArrow.y = display.contentCenterY - (shift2 * 20) + 10
elseif (gridzoom == 3) then
directionArrow.x = display.contentCenterX + (shift * 8) + 4
directionArrow.y = display.contentCenterY - (shift2 * 10) + 5
end
locationText:toFront()
timeText:toFront()
directionArrow:toFront()
locationName:toFront()
if (debugLocal) then print("end updateLocalOptimized") end
end
local function UpdateMapTiles()
--set this to run once a second or so.
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)
checkTileGeneration(plusCodeNoPlus, "mapTiles")
end -- for
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
function scene:create(event)
composer.setVariable("myScore", "0")
if (debug) then print("creating MPAreaControlScene2") end
local sceneGroup = self.view
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, 230, 400, 100)
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)
locationName = display.newText(sceneGroup, "", display.contentCenterX, 240, native.systemFont, 20)
locationText:setFillColor(0, 0, 0);
timeText:setFillColor(0, 0, 0);
locationName:setFillColor(0, 0, 0);
CreateRectangleGrid(3, 320, 400, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(3, 320, 400, sceneGroup, overlayCollection) -- rectangular Cell11 grid with overlay
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 baseline scene") end
end
function scene:show(event)
if (debug) then print("showing baseline 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
timer.performWithDelay(50, UpdateLocalOptimized, 1)
if (debugGPS) then timer.performWithDelay(3000, testDrift, -1) end
mapTileUpdater = timer.performWithDelay(2000, UpdateMapTiles, -1)
end
end
function scene:hide(event)
if (debug) then print("hiding baseline scene") end
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
timer.cancel(timerResults)
timerResults = nil
timer.cancel(mapTileUpdater)
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 baseline 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