-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass_Lib.lua
More file actions
245 lines (215 loc) · 6.75 KB
/
Class_Lib.lua
File metadata and controls
245 lines (215 loc) · 6.75 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
ObjectTypes = {
Base = "BaseObject",
Physics = "PhysicsObject",
}
---@section BaseObject 1 _BASEOBJECT_
---@class BaseObject
---@field position LBVec
---@field rotation number
---@field scale number
---@field name string
---@field id number
---@field shapes table
---@field layers table
---@field type string
BaseObject = {
---@param self BaseObject
---@param _position LBVec
---@param _rotation number
---@param _width number
---@param _height number
---@param _name string
---@return BaseObject
new = function(self, _position, _rotation, _scale, _name)
return LifeBoatAPI.lb_copy(self, {
position = _position or LifeBoatAPI.LBVec:new(),
rotation = _rotation or 0,
scale = _scale or 1,
name = _name or "",
id = nil, -- ID will be assigned by the engine
shapes = {},
layers = {},
type = ObjectTypes.Base,
})
end;
---@section Update
---@param self BaseObject
Update = function(self)
end;
---@endsection
---@section Draw
---@param self BaseObject
Draw = function(self)
for _, shape in ipairs(self.shapes) do
shape.position = self.position
shape.rotation = self.rotation
shape:Draw()
end
end;
---@endsection
---@section AddShape
---@param self BaseObject
---@param shape BaseShape
AddShape = function(self, shape)
shape.id = #self.shapes + 1
table.insert(self.shapes, shape)
end;
---@endsection
---@section RemoveShape
---@param self BaseObject
---@param shape BaseShape
RemoveShape = function(self, shape)
table.remove(self.shapes, shape.id)
end;
---@endsection
---@section AddLayer
---@param self BaseShape
---@param layer string
AddLayer = function(self, layer)
local id = TableContainsValue(self.layers, layer)
if id then return end
table.insert(self.layers, layer)
end;
---@endsection
---@section RemoveLayer
---@param self BaseShape
---@param layer string
RemoveLayer = function(self, layer)
RemoveFromTable(self.layers, layer)
end;
---@endsection
---@section SetLayers
---@param self BaseShape
---@param layers table
SetLayers = function(self, layers)
self.layers = layers
end;
---@endsection
---@section SetColor
---@param self BaseShape
---@param color table
---@param id number
SetColor = function(self, color, id)
id = id or nil
for _, shape in ipairs(self.shapes) do
if id ~= nil then
shape.color = (shape.id == id) and color or shape.color
else
shape.color = color
end
end
end;
---@section SetPosition
---@param self BaseShape
---@param vector LBVec
SetPosition = function(self, vector)
self.position = vector
for _, shape in ipairs(self.shapes) do
shape:SetPosition(vector)
end
end;
---@endsection
---@section SetRotation
---@param self BaseShape
---@param angle number
SetRotation = function(self, angle)
self.rotation = angle
for _, shape in ipairs(self.shapes) do
shape:SetRotation(angle)
end
end;
---@endsection
---@section SetScale
---@param self BaseShape
---@param s number
SetScale = function(self, s)
self.scale = s
for _, shape in ipairs(self.shapes) do
shape:SetScale(s)
end
end;
---@endsection
---@section GetAABB
---@param self BaseObject
---@return table
GetAABB = function(self)
local AABB = {
minX = self.position.x, maxX = self.position.x,
minY = self.position.y, maxY = self.position.y,
width = 0,
height = 0,
center = self.position
}
for _, shape in ipairs(self.shapes) do
local ShapeAABB = shape:GetAABB()
if ShapeAABB.minX < AABB.minX then AABB.minX = ShapeAABB.minX end
if ShapeAABB.maxX > AABB.maxX then AABB.maxX = ShapeAABB.maxX end
if ShapeAABB.minY < AABB.minY then AABB.minY = ShapeAABB.minY end
if ShapeAABB.maxY > AABB.maxY then AABB.maxY = ShapeAABB.maxY end
end
AABB.width = AABB.maxX - AABB.minX
AABB.height = AABB.maxY - AABB.minY
AABB.center = LifeBoatAPI.LBVec:new((AABB.minX+AABB.maxX)/2, (AABB.minY+AABB.maxY)/2)
return AABB
end;
---@endsection
---@section GetWorldVertices
---@param self BaseObject
---@return table
GetWorldVertices = function(self)
local verticeTable = {}
for _, shape in ipairs(self.shapes) do
table.insert(verticeTable, shape:GetWorldVertices())
end
return verticeTable
end;
---@endsection
---@section GetForwards
---@param self BaseObject
---@param forwardsOffset number
---@return LBVec ForwardVector Returns the normalised forwards vector based on rotation and offset
GetForwards = function(self, forwardsOffset)
forwardOffset = forwardOffset or 0
return LifeBoatAPI.LBVec:new(math.cos(self.rotation + forwardsOffset), math.sin(self.rotation + forwardsOffset))
end;
---@endsection
---@section OnCollision
---@param self BaseObject
---@param other BaseObject
---@param depth number
---@param normal LBVec
OnCollision = function(self, other, depth, normal) end;
---@endsection
}
---@endsection _BASEOBJECT_
---@section PhysicsObject 1 _PHYSICSOBJECT_
---@class PhysicsObject : BaseObject
---@field mass number
---@field velocity LBVec
---@field type string
PhysicsObject = LifeBoatAPI.lb_copy(BaseObject, {
---@param self PhysicsObject
---@param _position LBVec
---@param _rotation number
---@param _scale number
---@param _name string
---@param _mass number
---@return PhysicsObject
new = function(self, _position, _rotation, _scale, _name, _mass)
local obj = BaseObject.new(self, _position, _rotation, _scale, _name)
obj.mass = _mass or 1
obj.velocity = LifeBoatAPI.LBVec:new()
obj.type = ObjectTypes.Physics
return obj
end;
---@section ApplyForce
---@param self PhysicsObject
---@param totalForce LBVec
ApplyForce = function(self, totalForce)
local acceleration = Acceleration(totalForce, self.mass)
self.velocity = self.velocity:lbvec_add(acceleration:lbvec_scale(GameEngine.deltaTime))
self.position = self.position:lbvec_add(self.velocity:lbvec_scale(GameEngine.deltaTime))
end;
---@endsection
})
---@endsection _PHYSICSOBJECT_