forked from wiremod/wire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmod_wire_vectorthruster.lua
More file actions
295 lines (241 loc) · 6.98 KB
/
gmod_wire_vectorthruster.lua
File metadata and controls
295 lines (241 loc) · 6.98 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
AddCSLuaFile()
DEFINE_BASECLASS( "base_wire_entity" )
ENT.PrintName = "Wire Vector Thruster"
ENT.WantsTranslucency = true
ENT.WireDebugName = "Vector Thruster"
function ENT:SetEffect( name )
self:SetNWString( "Effect", name )
self.neteffect = WireLib.ThrusterNetEffects[ name ]
end
function ENT:GetEffect()
return self:GetNWString( "Effect" )
end
function ENT:SetOn( boolon )
if (self:IsOn() ~= boolon) then
if (boolon) then
if self.soundname then
self:StopSound( self.soundname )
self:EmitSound( self.soundname )
end
else
if self.soundname then
self:StopSound( self.soundname )
end
end
self:SetNWBool( "vecon", boolon, true )
end
end
function ENT:IsOn()
return self:GetNWBool( "vecon" )
end
function ENT:SetMode( v )
self:SetNWInt( "vecmode", v, true )
end
function ENT:GetMode()
return self:GetNWInt( "vecmode" )
end
function ENT:SetOffset( v )
self:SetNWVector( "Offset", v, true )
end
function ENT:GetOffset( name )
return self:GetNWVector( "Offset" )
end
function ENT:SetNormal( v )
self:SetNWVector( "vec", v )
end
function ENT:GetNormal()
return self:GetNWVector( "vec" )
end
if CLIENT then
function ENT:Initialize()
self.ShouldDraw = 1
self.EffectAvg = 0
local mx, mn = self:GetRenderBounds()
self:SetRenderBounds(mn + Vector(0,0,128), mx, 0)
end
function ENT:DrawTranslucent()
if self.ShouldDraw == 0 or not self:IsOn() then return end
local EffectDraw = WireLib.ThrusterEffectDraw[self:GetEffect()]
if EffectDraw then EffectDraw(self) end
end
function ENT:Think()
BaseClass.Think(self)
self.ShouldDraw = GetConVarNumber("cl_drawthrusterseffects")
if self.ShouldDraw == 0 or not self:IsOn() then return end
local EffectThink = WireLib.ThrusterEffectThink[self:GetEffect()]
if EffectThink then EffectThink(self) end
end
function ENT:CalcNormal()
return self:GetNormal()
end
return -- No more client
end
-- Server
function ENT:Initialize()
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:DrawShadow( false )
self.X = 0
self.Y = 0
self.Z = 0
self.mode = 0
self.yaw = 0
self.pitch = 0
self.mul = 0
self.force = 0
self.ForceLinear = vector_origin
self.ForceAngular = vector_origin
local max = self:OBBMaxs()
self.ThrustOffset = Vector( 0, 0, max.z )
local phys = self:GetPhysicsObject()
if (phys:IsValid()) then
local massCenter = phys:GetMassCenter()
self.ThrustOffset.x = massCenter.x
self.ThrustOffset.y = massCenter.y
phys:Wake()
end
self.oweffect = "fire"
self.uweffect = "same"
self:SetOffset(self.ThrustOffset)
self:SetNormal(Vector())
self:StartMotionController()
self.Inputs = Wire_CreateInputs(self, { "Mul" })
self.soundname = "PhysicsCannister.ThrusterLoop"
end
function ENT:OnRemove()
BaseClass.OnRemove(self)
if self.soundname then
self:StopSound(self.soundname)
end
end
function ENT:CalcForce(phys)
if self.angleinputs then
self.X = math.cos(self.pitch) * math.cos(self.yaw)
self.Y = math.sin(self.pitch)
self.Z = math.cos(self.pitch) * math.sin(self.yaw)
end
local ThrusterWorldForce = Vector( -self.X, -self.Y, -self.Z )
if (self.mode == 0) then
ThrusterWorldForce = phys:LocalToWorldVector( ThrusterWorldForce )
elseif (self.mode == 2) then
ThrusterWorldForce = phys:LocalToWorldVector( ThrusterWorldForce )
ThrusterWorldForce.z = -self.Z
end
local ThrustLen = ThrusterWorldForce:Length()
if self.lengthismul then
self.mul = ThrustLen
end
local LinearForceLength
if ThrustLen>0 then
local ThrustNormal = ThrusterWorldForce/ThrustLen
self:SetNormal( -ThrustNormal )
self.ForceLinear, self.ForceAngular = phys:CalculateVelocityOffset( ThrustNormal * ( math.min( self.force * self.mul, self.force_max ) * 50 ), phys:LocalToWorld( self.ThrustOffset ) )
self.ForceLinear = WireLib.clampForce(self.ForceLinear)
self.ForceAngular = WireLib.clampForce(self.ForceAngular)
LinearForceLength = self.ForceLinear:Length()
else
self:SetNormal( vector_origin )
self.ForceLinear, self.ForceAngular = vector_origin, vector_origin
LinearForceLength = 0
end
if self.neteffect then
self:SetNWFloat("Thrust", LinearForceLength)
end
end
function ENT:Setup(force, force_min, force_max, oweffect, uweffect, owater, uwater, bidir, soundname, mode, angleinputs, lengthismul)
self.mul = 0
self.force = force
self.oweffect = oweffect
self.uweffect = uweffect
self.force_min = force_min
self.force_max = force_max
self.bidir = bidir
self.owater = owater
self.uwater = uwater
self.angleinputs = angleinputs
self.lengthismul = lengthismul
if soundname and soundname ~= "" then
self.soundname = WireLib.SoundExists(soundname)
if self.soundname then
self:StopSound(self.soundname)
end
else
self:StopSound( self.soundname or "" )
self.soundname = nil
end
self.mode = mode or 0
self:SetMode( self.mode )
self:ShowOutput()
if (angleinputs) then
WireLib.AdjustInputs(self, {"Mul", "Pitch", "Yaw"})
else
WireLib.AdjustInputs(self, {"Mul", "X", "Y", "Z", "Vector [VECTOR]"})
end
end
function ENT:TriggerInput(iname, value)
if (iname == "Mul") then
self.mul = value
elseif (iname == "X") then
self.X = value
elseif (iname == "Y") then
self.Y = value
elseif (iname == "Z") then
self.Z = value
elseif (iname == "Vector") then
self.X = value.x
self.Y = value.y
self.Z = value.z
elseif (iname == "Yaw") then
self.yaw = math.rad( value )
elseif (iname == "Pitch") then
self.pitch = math.rad( value )
end
local phys = self:GetPhysicsObject()
if phys:IsValid() then
if phys:IsMotionEnabled() then
phys:Wake()
else
self:PhysicsSimulate(phys)
end
end
if self.lengthismul then
self:SetOn(true)
else
self:SetOn(self.mul ~= 0 and ( self.bidir and (math.abs(self.mul) > 0.01) and (math.abs(self.mul) > self.force_min) ) or ( (self.mul > 0.01) and (self.mul > self.force_min) ))
end
self:ShowOutput()
end
function ENT:PhysicsSimulate( phys, deltatime )
if (not self:IsOn()) then return SIM_NOTHING end
if (self:IsPlayerHolding()) then return SIM_NOTHING end
if (self:WaterLevel() > 0) then
if (not self.uwater) then
self:SetEffect("none")
return SIM_NOTHING
end
if (self.uweffect == "same") then
self:SetEffect(self.oweffect)
else
self:SetEffect(self.uweffect)
end
else
if (not self.owater) then
self:SetEffect("none")
return SIM_NOTHING
end
self:SetEffect(self.oweffect)
end
self:CalcForce(phys)
return self.ForceAngular, self.ForceLinear, SIM_GLOBAL_ACCELERATION
end
function ENT:ShowOutput()
local mode = self:GetMode()
self:SetOverlayText(string.format("Force Mul: %.2f\nInput: %.2f\nForce Applied: %.2f\nMode: %s",
self.force,
self.mul,
self:IsOn() and math.min( self.force * self.mul, self.force_max ) or 0,
(mode == 0 and "XYZ Local") or (mode == 1 and "XYZ World") or (mode == 2 and "XY Local, Z World")
))
end
duplicator.RegisterEntityClass("gmod_wire_vectorthruster", WireLib.MakeWireEnt, "Data", "force", "force_min", "force_max", "oweffect", "uweffect", "owater", "uwater", "bidir", "soundname", "mode", "angleinputs", "lengthismul")