-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPaintTownScene.lua
More file actions
322 lines (273 loc) · 13.4 KB
/
PaintTownScene.lua
File metadata and controls
322 lines (273 loc) · 13.4 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
--Paint The Town Mode
--TODO: rework this to the new non-competitive mode
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 = {} -- show cell area data/image tiles
local CellTapSensors = {} -- Not detecting taps in this mode. This is the highlight layer.
local ctsGroup = display.newGroup()
ctsGroup.x = -8
ctsGroup.y = 10
local timerResults = nil
local PaintTownMapUpdateCountdown = 8 --wait this many loops over the main update before doing a network call.
local firstRun = true
local locationText = ""
local directionArrow = ""
local debugText = {}
local locationName = ""
local zoom = ""
local arrowPainted = false
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()
bigGrid = not bigGrid
local sceneGroup = scene.view
timer.pause(timerResults)
for i = 1, #cellCollection do cellCollection[i]:removeSelf() end
for i = 1, #CellTapSensors do CellTapSensors[i]:removeSelf() end
cellCollection = {}
CellTapSensors = {}
if (bigGrid) then -- zoomed in grid
CreateRectangleGrid(3, 320, 400, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(61, 16, 20, ctsGroup, CellTapSensors, "painttown") -- rectangular Cell11 grid with a color fill
ctsGroup.x = -8
ctsGroup.y = 10
else -- zoomed out grid
CreateRectangleGrid(5, 160, 200, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(90, 8, 10, ctsGroup, CellTapSensors, "painttown") -- rectangular Cell11 grid with a color fill
ctsGroup.x = -4
ctsGroup.y = 5
end
--Move these to the back first, so that the map tiles will be behind them.
for square = 1, #CellTapSensors do
-- check each spot based on current cell, modified by gridX and gridY
CellTapSensors[square]:toBack()
end
for square = 1, #cellCollection do
-- check each spot based on current cell, modified by gridX and gridY
cellCollection[square]:toBack()
end
reorderUI()
forceRedraw = true
timer.resume(timerResults)
end
local function GoToSceneSelect()
local options = {effect = "flip", time = 125}
composer.gotoScene("SceneSelect", options)
return true
end
local function UpdateLocalOptimized()
-- This now needs to be 2 loops, because the cell tables are different sizes.
-- First loop for map tiles
-- Then loop for touch event rectangles that get shaded.
if timerResults == nil then
timerResults = timer.performWithDelay(150, UpdateLocalOptimized, -1)
end
if (debugLocal) then print("start UpdateLocalOptimized") end
if (currentPlusCode == "") then
if (debugLocal) then print("skipping, no location.") end
return
end
if (not playerInBounds) then
return
end
if (debug) then debugText.text = dump(lastLocationEvent) end
local plusCodeNoPlus = removePlus(currentPlusCode)
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
if currentPlusCode ~= previousPlusCode then
ClaimPaintTownCell(plusCodeNoPlus)
innerForceRedraw = true
end
previousPlusCode = currentPlusCode
-- draw this place's name on screen, or an empty string if its not a place.
local terrainInfo = LoadTerrainData(plusCodeNoPlus) -- terrainInfo is a whole row from the DB.
locationName.text = terrainInfo[3]; --name
if locationName.text == "" then
locationName.text = terrainInfo[4] --area type name
end
PaintTownMapUpdateCountdown = PaintTownMapUpdateCountdown -1
-- Step 1: set background MAC map tiles for the Cell8. Should be much simpler than old loop.
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)
if (PaintTownMapUpdateCountdown == 0) then
GetPaintTownMapData8(plusCodeNoPlus)
end
GetMapData8(plusCodeNoPlus)
checkTileGeneration(plusCodeNoPlus, "mapTiles")
-- 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 -- or imageExists == 0 --if I check for 0, this is always nil? if I check for nil, this is true when images are present?
--imageExists = doesFileExist(plusCodeNoPlus .. "-11.png", system.CachesDirectory)
--end
if (imageExists == true) then -- not sure why this is true when file is found and 0 when its not? -- or imageExists == 0
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
end
end
if (debug) then print("done with map cells") end
-- Step 2: set up event listener grid. These need Cell10s
local baselinePlusCode = currentPlusCode:sub(1,8) .. "+FF"
if (innerForceRedraw) then --Also no need to do all of this unless we shifted our Cell8 location.
for square = 1, #CellTapSensors do
CellTapSensors[square].fill = {0, 0}
local thisSquaresPluscode = baselinePlusCode
local shiftChar7 = math.floor(CellTapSensors[square].gridY / 20)
local shiftChar8 = math.floor(CellTapSensors[square].gridX / 20)
local shiftChar9 = math.floor(CellTapSensors[square].gridY % 20)
local shiftChar10 = math.floor(CellTapSensors[square].gridX % 20)
thisSquaresPluscode = shiftCell(thisSquaresPluscode, shiftChar7, 7)
thisSquaresPluscode = shiftCell(thisSquaresPluscode, shiftChar8, 8)
thisSquaresPluscode = shiftCell(thisSquaresPluscode, shiftChar9, 9)
thisSquaresPluscode = shiftCell(thisSquaresPluscode, shiftChar10, 10)
local idCheck = removePlus(thisSquaresPluscode)
CellTapSensors[square].pluscode = thisSquaresPluscode
if (requestedPaintTownCells[idCheck] ~= nil) then
CellTapSensors[square].fill = requestedPaintTownCells[idCheck]
end
end
end
if PaintTownMapUpdateCountdown == 0 then
PaintTownMapUpdateCountdown = 30 -- 60 loops is roughly 10 seconds of time passing. 30 = 5s
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
local shift = CODE_ALPHABET_:find(currentPlusCode:sub(11, 11)) - 11
local shift2 = CODE_ALPHABET_:find(currentPlusCode:sub(10, 10)) - 10
if (bigGrid) then -- zoomed in grid
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
locationText:toFront()
timeText:toFront()
directionArrow:toFront()
locationName:toFront()
if (debugLocal) then print("end updateLocalOptimized") end
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
function scene:create(event)
if (debug) then print("creating painttown scene") end
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
sceneGroup:insert(ctsGroup)
contrastSquare = display.newRect(sceneGroup, display.contentCenterX, 220, 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);
if (bigGrid) then
CreateRectangleGrid(3, 320, 400, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(60, 16, 20, ctsGroup, CellTapSensors, "painttown") -- rectangular Cell11 grid with color fill
else
-- original values, but too small to interact with.
CreateRectangleGrid(3, 160, 200, sceneGroup, cellCollection) -- rectangular Cell11 grid with map tiles
CreateRectangleGrid(60, 5, 4, ctsGroup, CellTapSensors, "painttown") -- rectangular Cell11 grid with color fill
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()
header = display.newImageRect(sceneGroup, "themables/PaintTown.png",300, 100)
header.x = display.contentCenterX
header.y = 100
header:addEventListener("tap", GoToSceneSelect)
header:toFront()
zoom = display.newImageRect(sceneGroup, "themables/ToggleZoom.png", 100, 100)
zoom.anchorX = 0
zoom.x = 50
zoom.y = 100
zoom:addEventListener("tap", ToggleZoom)
zoom:toFront()
if (debug) then
debugText = display.newText(sceneGroup, "location data", display.contentCenterX, 1180, 600, 0, native.systemFont, 22)
debugText:setFillColor(0, 0, 0);
debugText:toFront()
end
contrastSquare:toFront()
if (debug) then print("created PaintTown scene") end
end
function reorderUI()
ctsGroup:toFront()
header:toFront()
zoom:toFront()
directionArrow:toFront()
end
function scene:show(event)
if (debug) then print("showing painttown 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(1000, testDrift, -1) end
reorderUI()
end
if (debug) then print("showed painttown scene") end
end
function scene:hide(event)
if (debug) then print("hiding painttown 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 painttown 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