forked from KaffeDiem/TowerDefence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaphelper.lua
331 lines (272 loc) · 9.9 KB
/
Maphelper.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
-- A simple helper function to get the distance between two points
function Map:distPoints(x1, y1, x2, y2)
return math.sqrt((x2 - x1)^2 + (y2 - y1)^2)
end
function Map:addTower(towerType, mapPlaceHolder, oldTile)
local newTowerPos = Vector(self.tileSelected[1], self.tileSelected[2])
-- Check if tower has already been placed at set location
local towerPlacedAlready = false
for _, tower in ipairs(self.towers) do
if tower.pos == newTowerPos then
towerPlacedAlready = true
end
end
-- If no towers have been placed at that location yet, then add a tower
if not towerPlacedAlready then
local towerType = towerType or -- TowerType is what kind of tower should go
Tower(newTowerPos, self.map, self.pos)
local cost = towerType.cost
-- If the cost is smaller than the available gold
if self.playerGold - cost > -1 then
self.playerGold = self.playerGold - cost
table.insert(self.towers, towerType)
self.map[newTowerPos.x][newTowerPos.y] = mapPlaceHolder
else
self.map[newTowerPos.x][newTowerPos.y] = oldTile
end
end
end
-- Take a table of tables as input with mobs, see waves.lua file for more info
-- Could be eg. the 'easy' table.
function Map:generateMobs(t)
math.randomseed(os.time())
for i = 1, WAVEAMOUNT do
local randWave = math.random(#t)
local wave = t[randWave] -- The random wave which is to be added.
for j = 1, #wave do -- Run trough the tables of t
table.insert(self.waves, wave[j])
end
table.insert(self.waves, -1)
end
end
-- Type is an integer, 0 means no mob and 1 is the default mob
function Map:addMob(type)
if type == 1 then
local Mob = Mob(self.mobSpawn, self.mobGoal, self.map, self.pos)
table.insert(self.mobs, Mob)
end
end
-- Handles movement of the map such that you can 'move around'
function Map:translation(dt)
self.mx, self.my = love.mouse.getPosition()
if (self.my < love.graphics.getHeight() * 0.1 and not keyboardOnly) or
love.keyboard.isDown('up') then
self.ty = self.ty + (self.screenMovementSpeed * dt)
end
if (self.my > love.graphics.getHeight() * 0.9 and not keyboardOnly) or
love.keyboard.isDown('down') then
self.ty = self.ty - (self.screenMovementSpeed * dt)
end
if (self.mx < love.graphics.getWidth() * 0.1 and not keyboardOnly) or
love.keyboard.isDown('left') then
self.tx = self.tx + (self.screenMovementSpeed * dt)
end
if (self.mx > love.graphics.getWidth() * 0.9 and not keyboardOnly) or
love.keyboard.isDown('right') then
self.tx = self.tx - (self.screenMovementSpeed * dt)
end
-- TODO: make sure that the mouse position gets scaled as well
if love.keyboard.isDown('-') then
self.sx = self.sx - (0.1 * dt)
self.sy = self.sy - (0.1 * dt)
end
if love.keyboard.isDown('+') then
self.sx = self.sx + (0.1 * dt)
self.sy = self.sy + (0.1 * dt)
end
end
-- Create a walkable TF map for pathfinding
function Map:createwalkableMap()
local tfMap = {}
for i = 1, #self.map do
tfMap[i] = {}
for j = 1, #self.map[i] do
local canWalk = false
for _, v in ipairs(self.walkable) do
if v == self.map[i][j] then
canWalk = true
end
end
table.insert(tfMap[i], canWalk)
end
end
self.tfMap = tfMap
end
function Map.checkValidPlacement(mapObj)
mapObj:createwalkableMap()
local path = Luafinding.FindPath(mapObj.mobSpawn, mapObj.mobGoal, mapObj.tfMap)
if path then return true end
return false
end
-- Update mobs paths
function Map:updateMobPaths()
for _, m in ipairs(self.mobs) do
m:updatePath(self.map)
end
end
-- This function is called from Map:update and handles placement of towers
-- on both mobile and desktop-- // FIXME this function is not currently in use
function Map:checkTower()
if self.tileSelected then
-- Get the position on where to place a tower
local towerPos = Vector(self.tileSelected[1], self.tileSelected[2])
local currentTile = self.map[towerPos.x][towerPos.y]
local placeholder = 29
-- If the user agent is on a mobile device, use specific settings
if MOBILE and self.tileSelectionMode == 'tower' then
-- If the user agent is a desktop device
else
if self.tileSelectionMode == 'tower'
and self.timerTowerPlacement:hasFinished()
and love.mouse.isDown(1) then
self.map[towerPos.x][towerPos.y] = placeholder
-- Add a tower if the placement is valid / not on mobs path
if Map.checkValidPlacement(self) then
self:addTower(nil, placeholder, currentTile)
self:updateMobPaths()
else
MAP:sendNotification(1, "Cannot block mobs paths")
self.map[towerPos.x][towerPos.y] = currentTile
end
self.timerTileChangedLast = timeNow
end
end
end
end
function Map:changeTiles() -- // TODO rewrite this whole function
if self.tileSelected ~= nil then
local timeNow = love.timer.getTime()
if timeNow > self.timerTileChanged + self.timerTileChangedLast then
-- ADD TOWERS FEATURE (for desktop) --
if self.tileSelectionMode == 'tower' and love.mouse.isDown(1) then
-- Getting new position and current tile
local towerPos = Vector(self.tileSelected[1], self.tileSelected[2])
local currentTile = self.map[towerPos.x][towerPos.y]
local placeholder = 29 -- // TODO fix the placeholder implementation
self.map[towerPos.x][towerPos.y] = placeholder
-- Add a tower if the placement is valid / not on mobs path
if Map.checkValidPlacement(self) then
self:addTower(nil, placeholder, currentTile)
self:updateMobPaths()
else
MAP:sendNotification(1, "Cannot block mobs paths")
self.map[towerPos.x][towerPos.y] = currentTile
end
self.timerTileChangedLast = timeNow
end
-- CHANGE TILE FEATURE --
if self.tileSelectionMode == 'changetile' then
local i, j = self.tileSelected[1], self.tileSelected[2]
if love.mouse.isDown(1) then
self.map[i][j] = self.map[i][j] + 1
if self.map[i][j] > 40 then self.map[i][j] = 1 end
self:updateMobPaths() -- Update paths if tile is changed
self.timerTileChangedLast = timeNow
else if love.mouse.isDown(2) then
self.map[i][j] = self.map[i][j] - 1
if self.map[i][j] < 1 then self.map[i][j] = 40 end
self:updateMobPaths() -- Update paths if tile is changed
self.timerTileChangedLast = timeNow
end
end
end
-- -- Mobile tower implementation --
-- if self.tileSelectionMode == 'tower' and MOBILE then
-- local buttonSize = 80
-- suit.layout:reset(love.graphics:getWidth()*0.8 - buttonSize / 2, love.graphics:getHeight()*0.9,
-- 10 * SCALE, 10 * SCALE
-- )
-- local buy_tower =
-- suit.Button("Buy", suit.layout:row(buttonSize, 30))
-- if buy_tower.hit then
-- local towerPos = Vector(self.tileSelected[1], self.tileSelected[2])
-- local currentTile = self.map[towerPos.x][towerPos.y]
-- local placeholder = 29 -- // TODO fix the placeholder implementation
-- self.map[towerPos.x][towerPos.y] = placeholder
-- -- Add a tower if the placement is valid / not on mobs path
-- if Map.checkValidPlacement(self) then
-- self:addTower(nil, placeholder, currentTile)
-- self:updateMobPaths()
-- else
-- MAP:sendNotification(1, "Cannot block mobs paths")
-- self.map[towerPos.x][towerPos.y] = currentTile
-- end
-- self.timerTileChangedLast = timeNow
-- end
-- end
end
end
end
-- Get the currently selected tile
function Map:getTileSelected()
if #self.tilesHovered < 1 then -- If no tiles are hovered then none selected
self.tileSelected = nil
end
local minDist = nil
for i = 1, #self.tilesHovered do
local distTileCursor = self:distPoints(
self.tmx, self.tmy,
self.tilesHovered[i][3] + (self.tileWidth*SCALE/2),
self.tilesHovered[i][4] + (self.tileHeight*SCALE/2)
)
if minDist == nil then -- Set first tile to current one
minDist = distTileCursor
self.tileSelected = self.tilesHovered[i]
elseif distTileCursor < minDist then
minDist = distTileCursor
self.tileSelected = self.tilesHovered[i] -- This tile is closer to cursor
end
end
end
-- Create a random map of size rows, cols
function Map.createRandomMap(rows, cols, walkable)
math.randomseed(os.time())
::retry::
local map = {}
local height = {}
local rows = rows or math.random(20, 30)
local cols = cols or math.random(7, 15)
-- //TODO make sure that the spawn is at a real tile
local spawn = Vector(1, math.random(rows))
local goal = Vector(rows, math.random(cols))
local walkable = walkable or {6, 16}
-- These are all the different levels
local levels = {
{1, 6, 6, 6, 6, 6, 14, 0, 0}, -- Sand, wood and empty space
{16, 16, 16, 16, 4, 0}, -- Lava, stone and empty space
{6, 6, 6, 6, 6, 6, 6, 31, 32, 33, 34, 0}, -- Woods
{1, 2, 15, 15, 15, 15, 0} -- Ice and snow
}
local randomLevel = math.random(#levels)
local variance = levels[randomLevel]
for i = 1, rows do
table.insert(map, {})
table.insert(height, {})
-- This is the loop in which tile are decided
for j = 1, cols do
table.insert(map[i], variance[math.random(#variance)])
table.insert(height[i], 0)
end
end
-- Create the true/false map
local tf = {}
for i = 1, #map do
tf[i] = {}
for j = 1, #map[i] do
local canWalk = false
for _, v in ipairs(walkable) do
if v == map[i][j] then
canWalk = true
end
end
table.insert(tf[i], canWalk)
end
end
if Luafinding.FindPath(spawn, goal, tf) == nil then
goto retry
end
return {map, height, spawn, goal}
end
function Map:sendNotification(time, message)
table.insert(self.notifications, Notification(time, message))
end