1
+ local function InsertParticle (var , particle )
2
+ var .addedParticleCount = var .addedParticleCount + 1 ;
3
+ var .addedParticles [var .addedParticleCount ] = particle ;
4
+ end
5
+
6
+ local function emitSmoke (self , particleCount )
7
+ local var = self .var ;
8
+ local MuzzlePos = self .MuzzlePos ;
9
+ for i = 1 , particleCount do
10
+ local smoke = CreateMOSParticle (" Tiny Smoke Ball 1" .. (math.random () < 0.5 and " Glow Blue" or " " ), " Base.rte" );
11
+ smoke .Pos = MuzzlePos ;
12
+ smoke .Lifetime = smoke .Lifetime * RangeRand (0.5 , 1.0 );
13
+ smoke .Vel = var .Vel * 0.5 + Vector (RangeRand (0 , i ), 0 ):RadRotate (RangeRand (- math.pi , math.pi ));
14
+ InsertParticle (var , smoke );
15
+ end
16
+ self :RequestSyncedUpdate ();
17
+ end
18
+
1
19
function Create (self )
2
- self .range = math.sqrt (FrameMan .PlayerScreenWidth ^ 2 + FrameMan .PlayerScreenHeight ^ 2 )/ 2 ;
3
- self .penetrationStrength = 170 ;
4
- self .strengthVariation = 5 ;
20
+ -- Create local table to store variables for performance
21
+ local var = {};
22
+
23
+ var .range = math.sqrt (FrameMan .PlayerScreenWidth ^ 2 + FrameMan .PlayerScreenHeight ^ 2 )/ 2 ;
24
+ var .penetrationStrength = 170 ;
25
+ var .strengthVariation = 5 ;
5
26
-- This value tracks the shots and varies the penetration strength to create a "resistance" effect on tougher materials
6
- self .shotCounter = 0 ; -- TODO: Rename/describe this variable better
7
- self .activity = ActivityMan :GetActivity ();
27
+ var .shotCounter = 0 ; -- TODO: Rename/describe this variable better
28
+ var .activity = ActivityMan :GetActivity ();
8
29
9
- self .cooldown = Timer ();
10
- self .cooldownSpeed = 0.5 ;
30
+ var .cooldown = Timer ();
31
+ var .cooldownSpeed = 0.5 ;
11
32
12
- self .addedParticles = {};
33
+ var .addedParticles = {};
34
+ var .addedParticleCount = 0 ;
13
35
14
- self .addedWound = nil ;
15
- self .addedWoundOffset = nil ;
16
- self .addedWoundToUniqueID = nil ;
36
+ var .addedWound = nil ;
37
+ var .addedWoundOffset = nil ;
38
+ var .addedWoundToUniqueID = nil ;
39
+ var .Vel = self .Vel ;
17
40
18
- function self .emitSmoke (particleCount )
19
- for i = 1 , particleCount do
20
- local smoke = CreateMOSParticle (" Tiny Smoke Ball 1" .. (math.random () < 0.5 and " Glow Blue" or " " ), " Base.rte" );
21
- smoke .Pos = self .MuzzlePos ;
22
- smoke .Lifetime = smoke .Lifetime * RangeRand (0.5 , 1.0 );
23
- smoke .Vel = self .Vel * 0.5 + Vector (RangeRand (0 , i ), 0 ):RadRotate (RangeRand (- math.pi , math.pi ));
24
- table.insert (self .addedParticles , smoke );
25
- end
26
- self :RequestSyncedUpdate ();
27
- end
41
+ self .var = var ;
28
42
end
29
43
30
44
function ThreadedUpdate (self )
45
+ local var = self .var ;
31
46
if self .FiredFrame then
32
47
local actor = self :GetRootParent ();
33
- local range = self .range + math.random (8 );
48
+ local range = var .range + math.random (8 );
34
49
if IsActor (actor ) then
35
50
actor = ToActor (actor );
36
51
range = range + actor .AimDistance ;
@@ -52,12 +67,12 @@ function ThreadedUpdate(self)
52
67
skipPx = 1 ;
53
68
local shortRay = SceneMan :CastObstacleRay (gapPos , Vector (trace .X , trace .Y ):SetMagnitude (range - rayLength + skipPx ), hitPos , gapPos , actor .ID , self .Team , rte .airID , skipPx );
54
69
gapPos = gapPos - Vector (trace .X , trace .Y ):SetMagnitude (skipPx );
55
- local strengthFactor = math.max (1 - rayLength / self .range , math.random ()) * (self .shotCounter + 1 )/ self .strengthVariation ;
70
+ local strengthFactor = math.max (1 - rayLength / var .range , math.random ()) * (var .shotCounter + 1 )/ var .strengthVariation ;
56
71
57
- self . addedWoundToMOID = SceneMan :GetMOIDPixel (hitPos .X , hitPos .Y );
58
- if self . addedWoundToMOID ~= rte .NoMOID and self . addedWoundToMOID ~= self .ID then
59
- local mo = ToMOSRotating (MovableMan :GetMOFromID (self . addedWoundToMOID ));
60
- if self .penetrationStrength * strengthFactor >= mo .Material .StructuralIntegrity then
72
+ local addedWoundToMOID = SceneMan :GetMOIDPixel (hitPos .X , hitPos .Y );
73
+ if addedWoundToMOID ~= rte .NoMOID and addedWoundToMOID ~= self .ID then
74
+ local mo = ToMOSRotating (MovableMan :GetMOFromID (addedWoundToMOID ));
75
+ if var .penetrationStrength * strengthFactor >= mo .Material .StructuralIntegrity then
61
76
local moAngle = - mo .RotAngle * mo .FlipFactor ;
62
77
63
78
local woundName = mo :GetEntryWoundPresetName ();
@@ -68,9 +83,9 @@ function ThreadedUpdate(self)
68
83
local woundOffset = Vector (dist .X * mo .FlipFactor , dist .Y ):RadRotate (moAngle ):SetMagnitude (dist .Magnitude - (wound .Radius - 1 ) * wound .Scale );
69
84
wound .InheritedRotAngleOffset = woundOffset .AbsRadAngle ;
70
85
woundOffset = woundOffset :RadRotate (- mo .RotAngle );
71
- self .addedWound = wound ;
72
- self .addedWoundOffset = woundOffset ;
73
- self .addedWoundToUniqueID = mo .UniqueID ;
86
+ var .addedWound = wound ;
87
+ var .addedWoundOffset = woundOffset ;
88
+ var .addedWoundToUniqueID = mo .UniqueID ;
74
89
self :RequestSyncedUpdate ();
75
90
end
76
91
end
@@ -80,19 +95,19 @@ function ThreadedUpdate(self)
80
95
smoke .Pos = gapPos ;
81
96
smoke .Vel = Vector (- trace .X , - trace .Y ):SetMagnitude (math.random (3 , 6 )):RadRotate (RangeRand (- 1.5 , 1.5 ));
82
97
smoke .Lifetime = smoke .Lifetime * strengthFactor ;
83
- table.insert ( self . addedParticles , smoke );
98
+ InsertParticle ( var , smoke );
84
99
85
100
local pix = CreateMOPixel (" Laser Rifle Glow " .. math.floor (strengthFactor * 4 + 0.5 ), " Techion.rte" );
86
101
pix .Pos = gapPos ;
87
- pix .Sharpness = self .penetrationStrength / 6 ;
102
+ pix .Sharpness = var .penetrationStrength / 6 ;
88
103
pix .Vel = Vector (trace .X , trace .Y ):SetMagnitude (6 );
89
- table.insert ( self . addedParticles , pix );
104
+ InsertParticle ( var , pix );
90
105
end
91
106
if rayLength ~= 0 then
92
107
trace = SceneMan :ShortestDistance (startPos , gapPos , SceneMan .SceneWrapsX );
93
108
for player = Activity .PLAYER_1 , Activity .MAXPLAYERCOUNT - 1 do
94
- local team = self .activity :GetTeamOfPlayer (player );
95
- local screen = self .activity :ScreenOfPlayer (player );
109
+ local team = var .activity :GetTeamOfPlayer (player );
110
+ local screen = var .activity :ScreenOfPlayer (player );
96
111
if screen ~= - 1 and not (SceneMan :IsUnseen (startPos .X , startPos .Y , team ) or SceneMan :IsUnseen (hitPos .X , hitPos .Y , team )) then
97
112
PrimitiveMan :DrawLinePrimitive (screen , startPos , startPos + trace , 254 );
98
113
end
@@ -101,48 +116,55 @@ function ThreadedUpdate(self)
101
116
for i = 0 , particleCount do
102
117
local pix = CreateMOPixel (" Laser Rifle Glow 0" , " Techion.rte" );
103
118
pix .Pos = startPos + trace * i / particleCount ;
104
- pix .Vel = self .Vel ;
105
- table.insert ( self . addedParticles , pix );
119
+ pix .Vel = var .Vel ;
120
+ InsertParticle ( var , pix );
106
121
end
107
122
end
108
- self .shotCounter = (self .shotCounter + 1 ) % self .strengthVariation ;
109
- self .cooldown :Reset ();
123
+ var .shotCounter = (var .shotCounter + 1 ) % var .strengthVariation ;
124
+ var .cooldown :Reset ();
110
125
end
111
126
112
127
if self .Magazine and self .Magazine .RoundCount > 0 then
113
128
local ammoRatio = 1 - self .Magazine .RoundCount / self .Magazine .Capacity ;
114
- self . emitSmoke (math.floor (ammoRatio * RangeRand (0.5 , 2.0 ) + RangeRand (0.25 , 0.50 )));
115
- self .cooldown :SetSimTimeLimitMS (self .ReloadTime * ammoRatio );
129
+ emitSmoke (self , math.floor (ammoRatio * RangeRand (0.5 , 2.0 ) + RangeRand (0.25 , 0.50 )));
130
+ var .cooldown :SetSimTimeLimitMS (self .ReloadTime * ammoRatio );
116
131
117
- local cooldownRate = math.floor (self .cooldown .ElapsedSimTimeMS / (60000 / (self .RateOfFire * self .cooldownSpeed )));
132
+ local cooldownRate = math.floor (var .cooldown .ElapsedSimTimeMS / (60000 / (self .RateOfFire * var .cooldownSpeed )));
118
133
if ammoRatio ~= 0 and cooldownRate >= 1 then
119
134
self .Magazine .RoundCount = math.min (self .Magazine .RoundCount + cooldownRate , self .Magazine .Capacity );
120
- self .cooldown :Reset ();
135
+ var .cooldown :Reset ();
121
136
end
122
137
self .FireSound .Pitch = (1.0 - ammoRatio * 0.1 )^ 2 ;
123
138
elseif self :IsReloading () then
124
- self . emitSmoke (math.floor ((self .cooldown :LeftTillSimTimeLimitMS ()/ self .ReloadTime ) * RangeRand (0.5 , 2.0 ) + RangeRand (0.50 , 0.75 )));
139
+ emitSmoke (self , math.floor ((var .cooldown :LeftTillSimTimeLimitMS ()/ self .ReloadTime ) * RangeRand (0.5 , 2.0 ) + RangeRand (0.50 , 0.75 )));
125
140
elseif self .RoundInMagCount >= 0 then
126
141
self :Reload ();
127
142
end
128
143
end
129
144
145
+ -- Localize function to improve performance
146
+ local AddParticle = MovableMan .AddParticle ;
147
+
130
148
function SyncedUpdate (self )
131
- if self .addedWound then
132
- local mo = MovableMan :FindObjectByUniqueID (self .addedWoundToUniqueID );
149
+ local var = self .var ;
150
+ if var .addedWound then
151
+ local mo = MovableMan :FindObjectByUniqueID (var .addedWoundToUniqueID );
133
152
if mo then
134
153
mo = ToMOSRotating (mo );
135
- mo :AddWound (self .addedWound , self .addedWoundOffset , true );
154
+ mo :AddWound (var .addedWound , var .addedWoundOffset , true );
136
155
end
137
156
138
- self .addedWound = nil ;
139
- self .addedWoundOffset = nil ;
140
- self .addedWoundToMOID = nil ;
141
- self .addedWoundToUniqueID = nil ;
157
+ var .addedWound = nil ;
158
+ var .addedWoundOffset = nil ;
159
+ var .addedWoundToUniqueID = nil ;
142
160
end
143
161
144
- for i = 1 , # self .addedParticles do
145
- MovableMan :AddParticle (self .addedParticles [i ]);
162
+ if (var .addedParticleCount > 0 ) then
163
+ for i = 1 , var .addedParticleCount do
164
+ -- Remember to add function caller as first argument to localized function
165
+ AddParticle (MovableMan , var .addedParticles [i ]);
166
+ end
167
+ var .addedParticles = {};
168
+ var .addedParticleCount = 0 ;
146
169
end
147
- self .addedParticles = {};
148
170
end
0 commit comments