-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.lua
105 lines (88 loc) · 2.13 KB
/
grid.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
local Tile = require 'tile'
local grid = {}
function grid:load(size)
self.tiles = {}
self.size = size
self.tileSize = 50
self:createTiles()
self.width = math.sqrt(#self.tiles)
self.height = self.width
self:setTileCoords()
self:pickSpecials()
self:pickDeadlies(3)
self:setTriggers()
end
function grid:update(dt)
for k,tile in ipairs(self.tiles) do
tile:update(dt)
end
end
function grid:draw()
for _,tile in ipairs(self.tiles) do
tile:draw(self.tileSize)
end
end
function grid:createTiles()
for i = 1, self.size do
local tile = Tile(false)
tile.pos = i
table.insert(self.tiles, tile)
end
end
function grid:setTileCoords()
local i = 1
for h = 1, self.width do
for w = 1, self.height do
x, y = self:coords(w - 1, h - 1)
self.tiles[i]:setCoords(x, y)
i = i + 1
end
end
end
function grid:coords(w, h)
local offset = (love.graphics.getWidth() / 2) - ((self.width / 2) * self.tileSize)
return offset + (w * self.tileSize), 50 + h * self.tileSize
end
function grid:getTile(pos)
return self.tiles[pos]
end
function grid:setTriggers()
for _, trap in ipairs(self.traps) do
for i, tile in ipairs(self.tiles) do
if not tile.triggersGranny then
tile.triggersGranny = i - 1 == trap
or i + 1 == trap
or i - self.width == trap
or i + self.width == trap
end
end
end
end
function grid:pickDeadlies(amount)
local deadlies = {}
while #deadlies < amount do
local index = love.math.random(1, self.size - 2)
if index ~= self.size - self.width and index ~= self.size - (self.width + 1) then
table.insert(deadlies, index)
end
end
for _, tile in ipairs(deadlies) do self.tiles[tile]:setDeadly() end
self.traps = deadlies
end
function grid:pickSpecials()
local highSpecials = {}
while #highSpecials < 10 do
table.insert(highSpecials, love.math.random(1, self.size / 2))
end
for _, tile in ipairs(highSpecials) do
self.tiles[tile]:setRandomHighValueItem()
end
local lowSpecials = {}
while #lowSpecials < 36 do
table.insert(lowSpecials, love.math.random(self.size / 4, self.size - 1))
end
for _, tile in ipairs(lowSpecials) do
self.tiles[tile]:setRandomLowValueItem()
end
end
return grid