-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRendering_Lib_MK2.lua
More file actions
343 lines (306 loc) · 9.74 KB
/
Rendering_Lib_MK2.lua
File metadata and controls
343 lines (306 loc) · 9.74 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
-- CONTAINS CLASSES FOR RENDERING DIFFERENT SHAPES
-- This rewrite moves to a different model, where we have BaseShape, Triangle and Convex N-sided Polyon.
-- This would allow easier creation of more complex shapes.
-- Honestly, I am not sure we even need BaseShape...?
ShapeTypes = {
Base = "BaseShape",
Polygon = "Polygon",
Circle = "Circle",
}
---@section BaseShape 1 _BASESHAPE_
---@class BaseShape
---@field int id
---@field position LBVec
---@field rotation number
---@field scale number
---@field color table
---@field type string
BaseShape = {
---@param self BaseShape
---@param _position LBVec
---@param _rotation number
---@param _scale number
---@return BaseShape
new = function(self, _position, _rotation, _scale)
return LifeBoatAPI.lb_copy(self, {
id = nil,
position = _position,
rotation = _rotation,
scale = _scale,
color = {255, 255, 255, 255},
type = ShapeTypes.Base,
})
end;
---@section SetPosition
---@param self BaseShape
SetPosition = function (self, vector)
self.position = vector
end;
---@endsection
---@section SetRotation
---@param self BaseShape
SetRotation = function (self, angle)
self.rotation = angle
end;
---@endsection
---@section SetScale
---@param self BaseShape
SetScale = function (self, s)
self.scale = s
end;
---@endsection
---@section SetColor
---@param self BaseShape
SetColor = function (self, r,g,b,a)
self.color = {r,g,b,a}
end;
---@endsection
---@section GetAABB
---@param self BaseShape
---@return table
GetAABB = function(self)
return {
minX = self.position.x, maxX = self.position.x,
minY = self.position.y, maxY = self.position.y,
width = 0,
height = 0,
center = self.position
}
end;
---@endsection
---@section GetWorldVertices
---@param self BaseShape
---@return table
GetWorldVertices = function(self)
return { type=self.type, vertices=nil }
end;
---@endsection
---@section Draw
---@param self BaseShape
Draw = function(self) end;
---@endsection
}
---@endsection _BASESHAPE_
---@section Polygon 1 _POLYGON_
---@class Polygon
---@field id number
---@field position LBVec
---@field rotation number
---@field scale number
---@field color table
---@field type string
---@field vertices table
---@field worldVertices table
Polygon = LifeBoatAPI.lb_copy(BaseShape, {
---@section SortVertices
---@param self Polygon
---@param vertices table
---@return table
SortVertices = function (self, vertices)
local t_x, t_y = 0, 0
for _, vertice in ipairs(vertices) do
t_x = t_x + vertice.x
t_y = t_y + vertice.y
end
local a_x, a_y = t_x / #vertices, t_y / #vertices
local angleVertices = {}
for i, vertice in ipairs(vertices) do
local angle = math.atan(vertice.y - a_y, vertice.x - a_x)
angleVertices[i] = {vertex = vertice, angle = angle}
end
table.sort(angleVertices, function(a, b)
return a.angle < b.angle
end)
local sorted = {}
for i, pair in ipairs(angleVertices) do
sorted[i] = LifeBoatAPI.LBVec:new(pair.vertex.x, pair.vertex.y)
end
local finalVertices = {}
local n = #sorted
for i = 1, n do
local prev = sorted[(i - 2) % n + 1]
local curr = sorted[i]
local next = sorted[i % n + 1]
local area = (prev.x - curr.x)*(next.y - curr.y) - (next.x - curr.x)*(prev.y - curr.y)
if math.abs(area) > 1e-8 then
finalVertices[#finalVertices + 1] = curr
end
end
return finalVertices
end;
---@endsection
---@param self Polygon
---@param _position LBVec
---@param _rotation number
---@param _scale number
---@param _vertices table
---@param _doFill boolean
---@return Polygon
new = function(self, _position, _rotation, _scale, _vertices, _doFill)
local obj = BaseShape.new(self, _position, _rotation, _scale)
obj.vertices = Polygon:SortVertices(_vertices)
obj.worldVertices = obj.vertices
obj.doFill = _doFill
obj.type = ShapeTypes.Polygon
return obj
end;
---@section Triangulate
---@param self Polygon
---@param vertices table
---@return table
Triangulate = function (self, vertices)
local tris, anchor = {}, vertices[1]
for i = 1, #vertices-2 do
tris[i] = {anchor, vertices[i+1], vertices[i+2]}
end
return tris
end;
---@endsection
---@section GetScaledVertices
---@param self Polygon
---@param vertices table
---@return table
GetScaledVertices = function (self, vertices)
local scaledVertices = {}
for i, v in ipairs(vertices) do
scaledVertices[i] = v:lbvec_scale(self.scale)
end
return scaledVertices
end;
---@endsection
---@section GetRotatedVertices
---@param self Polygon
---@param vertices table
---@return table
GetRotatedVertices = function (self, vertices)
local cosR, sinR, rotatedVertices = math.cos(self.rotation), math.sin(self.rotation), {}
for i, vertice in ipairs(vertices) do
local rotatedX = vertice.x * cosR - vertice.y * sinR
local rotatedY = vertice.x * sinR + vertice.y * cosR
rotatedVertices[i] = LifeBoatAPI.LBVec:new(rotatedX, rotatedY)
end
return rotatedVertices
end;
---@endsection
---@section GetTransformedVertices
---@param self Polygon
---@param vertices table
---@return table
GetTransformedVertices = function (self, vertices)
local transformedVerices = {}
for i, vertice in ipairs(vertices) do
transformedVerices[i] = self.position:lbvec_add(vertice)
end
return transformedVerices
end;
---@endsection
---@section GetAABB
---@param self Polygon
---@return table
GetAABB = function (self)
local verts = self.worldVertices
local minX, minY = verts[1].x, verts[1].y
local maxX, maxY = verts[1].x, verts[1].y
for i = 2, #verts do
local v = verts[i]
if v.x < minX then minX = v.x end
if v.x > maxX then maxX = v.x end
if v.y < minY then minY = v.y end
if v.y > maxY then maxY = v.y end
end
return {
minX = minX, maxX = maxX,
minY = minY, maxY = maxY,
width = maxX - minX,
height = maxY - minY,
center = LifeBoatAPI.LBVec:new((minX+maxX)/2, (minY+maxY)/2)
}
end;
---@endsection
---@section GetWorldVertices
---@param self Polygon
---@return table
GetWorldVertices = function(self)
return { type=self.type, vertices=self.worldVertices }
end;
---@endsection
---@section Draw
---@param self Polygon
Draw = function (self)
self.worldVertices = self:GetTransformedVertices(self:GetRotatedVertices(self:GetScaledVertices(self.vertices)))
screen.setColor(self.color[1], self.color[2], self.color[3], self.color[4])
if self.doFill then
local triangles = self:Triangulate(self.worldVertices)
for _, triangle in ipairs(triangles) do
screen.drawTriangleF(triangle[1].x, triangle[1].y, triangle[2].x, triangle[2].y, triangle[3].x, triangle[3].y)
end
else
for i = 1, #self.worldVertices-1 do
local v1, v2 = self.worldVertices[i], self.worldVertices[i+1]
screen.drawLine(v1.x, v1.y, v2.x, v2.y)
end
local v1, v2 = self.worldVertices[#self.worldVertices], self.worldVertices[1]
screen.drawLine(v1.x, v1.y, v2.x, v2.y)
end
end;
---@endsection
})
---@endsection _POLYGON_
---@section Circle 1 _CIRCLE_
---@class Circle
---@field int id
---@field position LBVec
---@field rotation number
---@field scale number
---@field color table
---@field type string
---@field radius number
Circle = LifeBoatAPI.lb_copy(BaseShape, {
---@param self Circle
---@param _position LBVec
---@param _rotation number
---@param _scale number
---@param _radius number
---@param _doFill boolean
---@return Circle
new = function(self, _position, _rotation, _scale, _radius, _doFill)
local obj = BaseShape.new(self, _position, _rotation, _scale)
obj.radius = _radius
obj.doFill = _doFill
obj.type = ShapeTypes.Circle
return obj
end;
---@section GetAABB
---@param self Circle
---@return table
GetAABB = function(self)
local r, p = self.radius * self.scale, self.position
return {
minX = p.x - r, maxX = p.x + r,
minY = p.y - r, maxY = p.y + r,
width = 2 * r,
height = 2 * r,
center = p
}
end;
---@endsection
---@section GetWorldVertices
---@param self Circle
---@return table
GetWorldVertices = function(self)
return { type=self.type, center=self.position, radius=self.radius }
end;
---@endsection
---@section Draw
---@param self Circle
Draw = function(self)
screen.setColor(self.color[1], self.color[2], self.color[3], self.color[4])
if self.doFill then
screen.drawCircleF(self.position.x, self.position.y, self.radius * self.scale)
else
screen.drawCircleF(self.position.x, self.position.y, self.radius * self.scale)
end
end;
---@endsection
})
---@endsection _CIRCLE_