-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.tl
303 lines (243 loc) · 7.6 KB
/
hex.tl
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
--[[
local vec = require "vector"
local inspect = require "libs.inspect"
--local vec = require "libs.vector"
local gr = love.graphics
require "common"
--local vec2 = require "vector-light"
function getHexPolygonWidth(hex)
return dist(hex[3], hex[4], hex[11], hex[12])
end
function getHexPolygonHeight(hex)
return dist(hex[1], hex[2], hex[7], hex[8])
end
local Hex = {}
Hex.__index = Hex
function Hex:getVertices()
local vertices = {}
for i = 1, 6 do
table.insert(vertices, self[i])
end
return vertices
end
function Hex:getDebugCanvas()
local polyW = getHexPolygonWidth(self)
--local canvas = gr.newCanvas(getHexPolygonWidth(self),
--getHexPolygonHeight(self))
local t, w, h = self:getAABB()
local screenW, screenH = gr.getDimensions()
--local snap = 30
--t[1] = t[1] - snap
--t[2] = t[2] - snap
--t[3] = t[3] + snap
--t[4] = t[4] + snap
local canvas = gr.newCanvas(gr.getDimensions())
--local canvas = gr.newCanvas(w, h)
gr.setCanvas(canvas)
gr.clear(0, 0, 1)
gr.push()
--gr.scale(polyW / screenW, polyW / screenW)
local vi = 1
for i = 1, #self - 2, 2 do
gr.setColor{1, 1, 1, 1}
gr.print(string.format("(%d)", vi), self[i], self[i+1])
vi = vi + 1
end
gr.line(t[1], t[2], t[3], t[4])
gr.setColor{1, 0, 0}
gr.rectangle("line", t[1], t[2], w, h)
gr.pop()
gr.setCanvas()
return canvas
end
function Hex:setMesh(mesh, endindex)
self.mesh = mesh
self.endindex = endindex
end
function Hex:setVertexColor(index, color)
local vertex = {self.mesh:getVertex(self.endindex - index + 0)}
--print("vertex", inspect(vertex))
self.mesh:setVertex(self.endindex - index + 0, vertex)
end
function Hex:setColor(color)
for i = 1, 6 do
self:setVertexColor(i, color)
end
end
-- return {x1, y1, x2, y2 }, w * 2, h * 2
function Hex:getAABB()
local w, h = getHexPolygonWidth(self) / 2, getHexPolygonHeight(self) / 2
return {self.cx - w, self.cy - h, self.cx + w, self.cx + h}, w * 2, h * 2
end
function newHexPolygon(cx, cy, rad)
local hex = setmetatable({}, Hex)
local d = math.pi * 2 / 6
local c = 0
for i = 1, 7 do
local x, y = cx + math.sin(c) * rad, cy + math.cos(c) * rad
table.insert(hex, x)
table.insert(hex, y)
c = c + d
end
hex.rad = rad
hex.selected = false
hex.cx, hex.cy = cx, cy
return hex
end
function addVertex(array, x, y, color)
table.insert(array, {
math.floor(x), math.floor(y),
0, 0, -- u v
color[1], color[2], color[3], color[4]
})
return #array
end
function addHex(data, hex, color)
addVertex(data, hex.cx, hex.cy, color)
addVertex(data, hex[1], hex[2], color)
addVertex(data, hex[3], hex[4], color)
addVertex(data, hex.cx, hex.cy, color)
addVertex(data, hex[3], hex[4], color)
addVertex(data, hex[5], hex[6], color)
addVertex(data, hex.cx, hex.cy, color)
addVertex(data, hex[5], hex[6], color)
addVertex(data, hex[7], hex[8], color)
addVertex(data, hex.cx, hex.cy, color)
addVertex(data, hex[7], hex[8], color)
addVertex(data, hex[9], hex[10], color)
addVertex(data, hex.cx, hex.cy, color)
addVertex(data, hex[9], hex[10], color)
addVertex(data, hex[11], hex[12], color)
addVertex(data, hex.cx, hex.cy, color)
addVertex(data, hex[11], hex[12], color)
return addVertex(data, hex[1], hex[2], color)
end
function addVertex2(array, x, y)
table.insert(array, {
math.floor(x), math.floor(y),
--math.floor(x), math.floor(y),
0, 0, -- u v
1, 1, 1, 1,
})
end
-- добавляет линию из пары треугольников и шести вершин
function addLine(array, x1, y1, x2, y2)
--TODO Оптимизировать вектора
local len = dist(x1, y1, x2, y2)
local dirx, diry = vec(x2 - x1) / len, vec(y2 - y1) / len
local dir1, dir2 = vec(x2 - x1, y2 - y1) / len, vec(x1 - x2, y1 - y2) / len
local width = 3
local p1 = vec(x1, y1) + dir1:perpendicular() * width
local p2 = vec(x1, y1) + dir2:perpendicular() * width
local p3 = vec(x2, y2) + dir1:perpendicular() * width
local p4 = vec(x2, y2) + dir2:perpendicular() * width
addVertex2(array, p1.x, p1.y)
addVertex2(array, p2.x, p2.y)
addVertex2(array, p3.x, p3.y)
addVertex2(array, p4.x, p4.y)
addVertex2(array, p3.x, p3.y)
addVertex2(array, p2.x, p2.y)
end
function addBorder(data, hex)
addLine(data, hex[1], hex[2], hex[3], hex[4])
addLine(data, hex[3], hex[4], hex[5], hex[6])
addLine(data, hex[5], hex[6], hex[7], hex[8])
addLine(data, hex[7], hex[8], hex[9], hex[10])
addLine(data, hex[9], hex[10], hex[11], hex[12])
addLine(data, hex[11], hex[12], hex[1], hex[2])
end
-- map - 2d array
function newHexField(startcx, startcy, map,rad, color)
--print(inspect(map))
--print(#map)
local xcount = #map
local ycount = #map[1]
assert(xcount == ycount)
local Handler = {
map = deepcopy(map),
}
Handler.__index = Handler
function Handler:get(x, y)
--print(inspect(Handler))
--print("x, y", x, y, Handler.map[x][y] or nil)
return Handler.map[x][y] or nil
end
local result = {}
setmetatable(result, Handler)
local mesh = gr.newMesh((6 * 3 + 6 * 6) * xcount * ycount, "triangles", "dynamic")
local meshData = {}
local cx, cy = startcx, startcy
local hasWH = false
local w, h
local prevHorizon = nil
local horizon = {}
for j = 1, ycount do
for i = 1, xcount do
local last = newHexPolygon(cx, cy, rad)
last.j = j
last.i = i
--table.insert(result, last)
local visible = map[j][i] ~= 0
if visible then
Handler.map[j][i] = last
last.mapIndex = {j, i}
table.insert(horizon, last)
end
--[1] left
--[2] lt
--[3] rt
--[4] right
--[5] rb
--[6] lb
last.refs = {}
for i = 1, 6 do
last.refs[#last.refs + 1] = false
end
if prevHorizon then
last.refs[2] = prevHorizon[i]
if i + 1 <= #prevHorizon then
last.refs[2] = prevHorizon[i + 1]
end
end
if i > 1 then
last.refs[1] = horizon[i - 1]
horizon[i - 1].refs[4] = last
end
if not hasWH then
w, h = getHexPolygonWidth(last), getHexPolygonHeight(last)
end
local lastIndex
if visible then
lastIndex = addHex(meshData, last, color)
addBorder(meshData, last)
last:setMesh(mesh, lastIndex)
end
cx = cx + w
end
for k, v in pairs(horizon) do
table.insert(result, v)
end
prevHorizon = horizon
horizon = {}
cy = cy + h * 3 / 4
cx = j % 2 == 1 and startcx + w / 2 or startcx
end
--updateNeighbors(result, xcount, ycount)
mesh:setVertices(meshData)
--print("result", inspect(result))
return result, mesh
end
function updateNeighbors(tbl, xcount, ycount)
for k, v in pairs(tbl) do
v.refs = {
--left lt top rt right rb bottom lb
}
if v.j > 1 then
end
end
end
return {
newHexField = newHexField,
newHexPolygon = newHexPolygon,
}
--]]