Skip to content

Commit b383dbe

Browse files
Optimize gun
1 parent e9a0590 commit b383dbe

File tree

1 file changed

+82
-53
lines changed

1 file changed

+82
-53
lines changed

Menu/Main.cs

Lines changed: 82 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,25 @@ public static void Prefix()
12321232
Visuals.ClearLinePool();
12331233
Visuals.ClearNameTagPool();
12341234

1235+
if (GunPointer != null)
1236+
{
1237+
if (!GunPointer.activeSelf)
1238+
Destroy(GunPointer);
1239+
1240+
GunPointer.SetActive(false);
1241+
}
1242+
1243+
if (GunLine != null)
1244+
{
1245+
if (!GunLine.gameObject.activeSelf)
1246+
{
1247+
Destroy(GunLine.gameObject);
1248+
GunLine = null;
1249+
}
1250+
1251+
GunLine.gameObject.SetActive(false);
1252+
}
1253+
12351254
List<(Vector3, float)> toRemoveAura = new List<(Vector3, float)> { };
12361255
foreach (KeyValuePair<(Vector3, float), GameObject> key in auraPool)
12371256
{
@@ -3350,6 +3369,10 @@ public static string GetHttp(string url)
33503369

33513370
private static List<float> volumeArchive = new List<float> { };
33523371
private static Vector3 GunPositionSmoothed = Vector3.zero;
3372+
3373+
private static GameObject GunPointer;
3374+
private static LineRenderer GunLine;
3375+
33533376
public static (RaycastHit Ray, GameObject NewPointer) RenderGun(int overrideLayerMask = -1)
33543377
{
33553378
GunSpawned = true;
@@ -3415,11 +3438,14 @@ public static (RaycastHit Ray, GameObject NewPointer) RenderGun(int overrideLaye
34153438
EndPosition = GunPositionSmoothed;
34163439
}
34173440

3418-
GameObject NewPointer = GameObject.CreatePrimitive(PrimitiveType.Sphere);
3419-
NewPointer.transform.localScale = (smallGunPointer ? new Vector3(0.1f, 0.1f, 0.1f) : new Vector3(0.2f, 0.2f, 0.2f)) * (scaleWithPlayer ? GTPlayer.Instance.scale : 1f);
3420-
NewPointer.transform.position = EndPosition;
3441+
if (GunPointer == null)
3442+
GunPointer = GameObject.CreatePrimitive(PrimitiveType.Sphere);
34213443

3422-
Renderer PointerRenderer = NewPointer.GetComponent<Renderer>();
3444+
GunPointer.SetActive(true);
3445+
GunPointer.transform.localScale = (smallGunPointer ? new Vector3(0.1f, 0.1f, 0.1f) : new Vector3(0.2f, 0.2f, 0.2f)) * (scaleWithPlayer ? GTPlayer.Instance.scale : 1f);
3446+
GunPointer.transform.position = EndPosition;
3447+
3448+
Renderer PointerRenderer = GunPointer.GetComponent<Renderer>();
34233449
PointerRenderer.material.shader = Shader.Find("GUI/Text Shader");
34243450
PointerRenderer.material.color = (gunLocked || GetGunInput(true)) ? GetBDColor(0f) : GetBRColor(0f);
34253451

@@ -3436,126 +3462,129 @@ public static (RaycastHit Ray, GameObject NewPointer) RenderGun(int overrideLaye
34363462
Destroy(Particle.GetComponent<Collider>());
34373463
}
34383464

3439-
Destroy(NewPointer.GetComponent<Collider>());
3440-
Destroy(NewPointer, Time.deltaTime);
3465+
Destroy(GunPointer.GetComponent<Collider>());
34413466

34423467
if (!disableGunLine)
34433468
{
3444-
GameObject line = new GameObject("iiMenu_GunLine");
3445-
LineRenderer lineRenderer = line.AddComponent<LineRenderer>();
3446-
lineRenderer.material.shader = Shader.Find("GUI/Text Shader");
3447-
lineRenderer.startColor = GetBGColor(0f);
3448-
lineRenderer.endColor = GetBGColor(0.5f);
3449-
lineRenderer.startWidth = 0.025f * (scaleWithPlayer ? GTPlayer.Instance.scale : 1f);
3450-
lineRenderer.endWidth = 0.025f * (scaleWithPlayer ? GTPlayer.Instance.scale : 1f);
3451-
lineRenderer.positionCount = 2;
3452-
lineRenderer.useWorldSpace = true;
3469+
if (GunLine == null)
3470+
{
3471+
GameObject line = new GameObject("iiMenu_GunLine");
3472+
GunLine = line.AddComponent<LineRenderer>();
3473+
}
3474+
3475+
GunLine.gameObject.SetActive(true);
3476+
GunLine.material.shader = Shader.Find("GUI/Text Shader");
3477+
GunLine.startColor = GetBGColor(0f);
3478+
GunLine.endColor = GetBGColor(0.5f);
3479+
GunLine.startWidth = 0.025f * (scaleWithPlayer ? GTPlayer.Instance.scale : 1f);
3480+
GunLine.endWidth = 0.025f * (scaleWithPlayer ? GTPlayer.Instance.scale : 1f);
3481+
GunLine.positionCount = 2;
3482+
GunLine.useWorldSpace = true;
34533483
if (smoothLines)
34543484
{
3455-
lineRenderer.numCapVertices = 10;
3456-
lineRenderer.numCornerVertices = 5;
3485+
GunLine.numCapVertices = 10;
3486+
GunLine.numCornerVertices = 5;
34573487
}
3458-
lineRenderer.SetPosition(0, StartPosition);
3459-
lineRenderer.SetPosition(1, EndPosition);
3460-
Destroy(line, Time.deltaTime);
3488+
GunLine.SetPosition(0, StartPosition);
3489+
GunLine.SetPosition(1, EndPosition);
34613490

34623491
int Step = GunLineQuality;
34633492
switch (gunVariation)
34643493
{
34653494
case 1: // Lightning
34663495
if (GetGunInput(true) || gunLocked)
34673496
{
3468-
lineRenderer.positionCount = Step;
3469-
lineRenderer.SetPosition(0, StartPosition);
3497+
GunLine.positionCount = Step;
3498+
GunLine.SetPosition(0, StartPosition);
34703499

34713500
for (int i = 1; i < (Step - 1); i++)
34723501
{
34733502
Vector3 Position = Vector3.Lerp(StartPosition, EndPosition, i / (Step - 1f));
3474-
lineRenderer.SetPosition(i, Position + (UnityEngine.Random.Range(0f, 1f) > 0.75f ? new Vector3(UnityEngine.Random.Range(-0.1f, 0.1f), UnityEngine.Random.Range(-0.1f, 0.1f), UnityEngine.Random.Range(-0.1f, 0.1f)) : Vector3.zero));
3503+
GunLine.SetPosition(i, Position + (UnityEngine.Random.Range(0f, 1f) > 0.75f ? new Vector3(UnityEngine.Random.Range(-0.1f, 0.1f), UnityEngine.Random.Range(-0.1f, 0.1f), UnityEngine.Random.Range(-0.1f, 0.1f)) : Vector3.zero));
34753504
}
34763505

3477-
lineRenderer.SetPosition(Step - 1, EndPosition);
3506+
GunLine.SetPosition(Step - 1, EndPosition);
34783507
}
34793508
break;
34803509
case 2: // Wavy
34813510
if (GetGunInput(true) || gunLocked)
34823511
{
3483-
lineRenderer.positionCount = Step;
3484-
lineRenderer.SetPosition(0, StartPosition);
3512+
GunLine.positionCount = Step;
3513+
GunLine.SetPosition(0, StartPosition);
34853514

34863515
for (int i = 1; i < (Step - 1); i++)
34873516
{
34883517
float value = ((float)i / (float)Step) * 50f;
34893518

34903519
Vector3 Position = Vector3.Lerp(StartPosition, EndPosition, i / (Step - 1f));
3491-
lineRenderer.SetPosition(i, Position + (Up * Mathf.Sin((Time.time * -10f) + value) * 0.1f));
3520+
GunLine.SetPosition(i, Position + (Up * Mathf.Sin((Time.time * -10f) + value) * 0.1f));
34923521
}
34933522

3494-
lineRenderer.SetPosition(Step - 1, EndPosition);
3523+
GunLine.SetPosition(Step - 1, EndPosition);
34953524
}
34963525
break;
34973526
case 3: // Blocky
34983527
if (GetGunInput(true) || gunLocked)
34993528
{
3500-
lineRenderer.positionCount = Step;
3501-
lineRenderer.SetPosition(0, StartPosition);
3529+
GunLine.positionCount = Step;
3530+
GunLine.SetPosition(0, StartPosition);
35023531

35033532
for (int i = 1; i < (Step - 1); i++)
35043533
{
35053534
Vector3 Position = Vector3.Lerp(StartPosition, EndPosition, i / (Step - 1f));
3506-
lineRenderer.SetPosition(i, new Vector3(Mathf.Round(Position.x * 25f) / 25f, Mathf.Round(Position.y * 25f) / 25f, Mathf.Round(Position.z * 25f) / 25f));
3535+
GunLine.SetPosition(i, new Vector3(Mathf.Round(Position.x * 25f) / 25f, Mathf.Round(Position.y * 25f) / 25f, Mathf.Round(Position.z * 25f) / 25f));
35073536
}
35083537

3509-
lineRenderer.SetPosition(Step - 1, EndPosition);
3538+
GunLine.SetPosition(Step - 1, EndPosition);
35103539
}
35113540
break;
35123541
case 4: // Sinewave
35133542
Step = GunLineQuality / 2;
35143543

35153544
if (GetGunInput(true) || gunLocked)
35163545
{
3517-
lineRenderer.positionCount = Step;
3518-
lineRenderer.SetPosition(0, StartPosition);
3546+
GunLine.positionCount = Step;
3547+
GunLine.SetPosition(0, StartPosition);
35193548

35203549
for (int i = 1; i < (Step - 1); i++)
35213550
{
35223551
Vector3 Position = Vector3.Lerp(StartPosition, EndPosition, i / (Step - 1f));
3523-
lineRenderer.SetPosition(i, Position + (Up * Mathf.Sin(Time.time * 10f) * (i % 2 == 0 ? 0.1f : -0.1f)));
3552+
GunLine.SetPosition(i, Position + (Up * Mathf.Sin(Time.time * 10f) * (i % 2 == 0 ? 0.1f : -0.1f)));
35243553
}
35253554

3526-
lineRenderer.SetPosition(Step - 1, EndPosition);
3555+
GunLine.SetPosition(Step - 1, EndPosition);
35273556
}
35283557
break;
35293558
case 5: // Spring
35303559
if (GetGunInput(true) || gunLocked)
35313560
{
3532-
lineRenderer.positionCount = Step;
3533-
lineRenderer.SetPosition(0, StartPosition);
3561+
GunLine.positionCount = Step;
3562+
GunLine.SetPosition(0, StartPosition);
35343563

35353564
for (int i = 1; i < (Step - 1); i++)
35363565
{
35373566
float value = ((float)i / (float)Step) * 50f;
35383567

35393568
Vector3 Position = Vector3.Lerp(StartPosition, EndPosition, i / (Step - 1f));
3540-
lineRenderer.SetPosition(i, Position + (Right * Mathf.Cos((Time.time * -10f) + value) * 0.1f) + (Up * Mathf.Sin((Time.time * -10f) + value) * 0.1f));
3569+
GunLine.SetPosition(i, Position + (Right * Mathf.Cos((Time.time * -10f) + value) * 0.1f) + (Up * Mathf.Sin((Time.time * -10f) + value) * 0.1f));
35413570
}
35423571

3543-
lineRenderer.SetPosition(Step - 1, EndPosition);
3572+
GunLine.SetPosition(Step - 1, EndPosition);
35443573
}
35453574
break;
35463575
case 6: // Bouncy
35473576
if (GetGunInput(true) || gunLocked)
35483577
{
3549-
lineRenderer.positionCount = Step;
3550-
lineRenderer.SetPosition(0, StartPosition);
3578+
GunLine.positionCount = Step;
3579+
GunLine.SetPosition(0, StartPosition);
35513580

35523581
for (int i = 1; i < (Step - 1); i++)
35533582
{
35543583
float value = ((float)i / (float)Step) * 15f;
3555-
lineRenderer.SetPosition(i, Vector3.Lerp(StartPosition, EndPosition, i / (Step - 1f)) + (Up * Mathf.Abs(Mathf.Sin((Time.time * -10f) + value)) * 0.3f));
3584+
GunLine.SetPosition(i, Vector3.Lerp(StartPosition, EndPosition, i / (Step - 1f)) + (Up * Mathf.Abs(Mathf.Sin((Time.time * -10f) + value)) * 0.3f));
35563585
}
35573586

3558-
lineRenderer.SetPosition(Step - 1, EndPosition);
3587+
GunLine.SetPosition(Step - 1, EndPosition);
35593588
}
35603589
break;
35613590
case 7: // Audio
@@ -3579,16 +3608,16 @@ public static (RaycastHit Ray, GameObject NewPointer) RenderGun(int overrideLaye
35793608
if (volumeArchive.Count > Step)
35803609
volumeArchive.Remove(Step);
35813610

3582-
lineRenderer.positionCount = Step;
3583-
lineRenderer.SetPosition(0, StartPosition);
3611+
GunLine.positionCount = Step;
3612+
GunLine.SetPosition(0, StartPosition);
35843613

35853614
for (int i = 1; i < (Step - 1); i++)
35863615
{
35873616
Vector3 Position = Vector3.Lerp(StartPosition, EndPosition, i / (Step - 1f));
3588-
lineRenderer.SetPosition(i, Position + (Up * (i >= volumeArchive.Count ? 0 : volumeArchive[i]) * (i % 2 == 0 ? 1f : -1f)));
3617+
GunLine.SetPosition(i, Position + (Up * (i >= volumeArchive.Count ? 0 : volumeArchive[i]) * (i % 2 == 0 ? 1f : -1f)));
35893618
}
35903619

3591-
lineRenderer.SetPosition(Step - 1, EndPosition);
3620+
GunLine.SetPosition(Step - 1, EndPosition);
35923621
}
35933622
break;
35943623
case 8: // Bezier, credits to Crisp / Kman / Steal / Untitled One of those 4 I don't really know who
@@ -3605,8 +3634,8 @@ public static (RaycastHit Ray, GameObject NewPointer) RenderGun(int overrideLaye
36053634
MidVelocity *= Mathf.Exp(-6f * Time.deltaTime);
36063635
MidPosition += MidVelocity * Time.deltaTime;
36073636

3608-
lineRenderer.positionCount = Step;
3609-
lineRenderer.SetPosition(0, StartPosition);
3637+
GunLine.positionCount = Step;
3638+
GunLine.SetPosition(0, StartPosition);
36103639

36113640
Vector3[] points = new Vector3[Step];
36123641
for (int i = 0; i < Step; i++)
@@ -3617,13 +3646,13 @@ public static (RaycastHit Ray, GameObject NewPointer) RenderGun(int overrideLaye
36173646
Mathf.Pow(t, 2) * EndPosition;
36183647
}
36193648

3620-
lineRenderer.positionCount = Step;
3621-
lineRenderer.SetPositions(points);
3649+
GunLine.positionCount = Step;
3650+
GunLine.SetPositions(points);
36223651
break;
36233652
}
36243653
}
36253654

3626-
return (Ray, NewPointer);
3655+
return (Ray, GunPointer);
36273656
}
36283657

36293658
public static bool GetGunInput(bool isShooting)

0 commit comments

Comments
 (0)