-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmapRegion.lua
147 lines (110 loc) · 3.2 KB
/
mapRegion.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
MapRegion = {}
MapRegion.__index = MapRegion
function MapRegion:new(x, y, cols, rows, incomingTiles, tileCount)
local self = setmetatable({}, MapRegion)
incomingTiles = incomingTiles or {}
tileCount = tileCount or 0
self.x = x
self.y = y
self.rows = rows
self.cols = cols
self.className = "MapRegion"
self.tileIndexes = {}
if self.rows <= 0 then self.rows = 1 end
if self.cols <= 0 then self.cols = 1 end
local newTiles = {}
-- reduce the included tileset to only those that fit within our rect
local rowMin = self.y
local rowMax = self.y + self.rows - 1
local colMin = self.x
local colMax = self.x + self.cols - 1
if tileCount > 0 then
for i=1,tileCount do
local t = incomingTiles[i]
-- if not t then
-- tprint(self.tiles)
-- end
if t.row >= rowMin and
t.row <= rowMax and
t.col >= colMin and
t.col <= colMax then
newTiles[#newTiles+1] = t
self.tileIndexes[#self.tileIndexes+1] = t.index
end
end
end
self.tiles = newTiles
self.tileCount = #newTiles
function self:rect()
return {x=(self.x - 1) * tileWidth, y=(self.y - 1) * tileHeight, width=self.cols * tileWidth, height=self.rows * tileHeight}
end
function self:indexForCoordinates(row, col)
return (self.cols * (row - 1)) + col
end
function self:intersectsRegion(region)
local intersects = false
if self.x + self.cols - 1 >= region.x or
region.x + region.cols - 1 <= self.x or
self.y + self.rows - 1 >= region.y or
region.y + region.rows - 1 <= self.y then
intersects = true
end
return intersects
end
function self:directionForCoordinates(x, y)
if y == 1 then
if x == 1 then
return DIRECTION_NW
elseif x == self.cols then
return DIRECTION_NE
else
return DIRECTION_N
end
elseif y < self.rows then
if x == 1 then
return DIRECTION_W
elseif x == self.cols then
return DIRECTION_E
else
return DIRECTION_C
end
elseif y == self.rows then
if x == 1 then
return DIRECTION_SW
elseif x == self.cols then
return DIRECTION_SE
else
return DIRECTION_S
end
end
end
function self:edgeTilesWithDirections()
local edges = {}
local directions = {}
local row = 1
local col = 1
for i=1, self.tileCount do
local isEdge = false
local t = self.tiles[i]
-- top/bottom
if row == 1 or row == self.rows or col == 1 or col == self.cols then
isEdge = true
end
if isEdge then
-- print("T: " .. t .. " Col: " .. col .. " Row: " .. row)
directions[#directions + 1] = self:directionForCoordinates(col, row)
edges[#edges+1] = t
end
col = col + 1
if col > self.cols then
col = 1
row = row + 1
end
end
return edges,directions
end
return self
end
function MapRegion:__tostring()
return self.className .. ": X: "..self.x.." Y: "..self.y.." Cols: "..self.cols.." Rows: "..self.rows .. " Tile Count: " .. #self.tiles
end