Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix training mode weapon switching (WIP) #668

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Assets/PinAnimator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using CollectionExtensions;
using System.Linq.Expressions;
using UnityEngine;

public class PinAnimator : AugmentAnimator
Expand Down Expand Up @@ -31,6 +31,8 @@ public override void OnReload(GunStats stats) { }

public override void OnFire(GunStats stats)
{
if (DoWeNeedToEscape())
return;
if (playAudio)
PlayCockingSound();
transform.localPosition = Vector3.zero;
Expand Down
16 changes: 16 additions & 0 deletions Assets/Scripts/Augment/AugmentImplementations/AugmentAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,20 @@ public abstract class AugmentAnimator : MonoBehaviour
public abstract void OnInitialize(GunStats stats);
public abstract void OnReload(GunStats stats);
public abstract void OnFire(GunStats stats);

protected bool DoWeNeedToEscape()
{
try
{
if (!gameObject)
{
return true;
}
}
catch (System.NullReferenceException)
{
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public override void OnReload(GunStats stats)

public override void OnFire(GunStats stats)
{
if (DoWeNeedToEscape())
return;
animator.SetTrigger("Fire");
// TODO wait for firing animation to end
OnShotFiredAnimation?.Invoke();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public override void OnReload(GunStats stats)

public override void OnFire(GunStats stats)
{
if (DoWeNeedToEscape())
return;
animator.SetTrigger("Fire");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class DDRBodyAnimator : AugmentAnimator
private Animator animator;
public override void OnFire(GunStats stats)
{
if (DoWeNeedToEscape())
return;
animator.SetTrigger("Vibrate");
}

Expand All @@ -19,6 +21,8 @@ public override void OnInitialize(GunStats stats)

public override void OnReload(GunStats stats)
{
if (DoWeNeedToEscape())
return;
animator.SetTrigger("Vibrate");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class MowerAnimator : AugmentAnimator

public override void OnFire(GunStats stats)
{
if (DoWeNeedToEscape())
return;
animator.SetTrigger("PistonPump");
handAnimator.SetTrigger("Pull");
}
Expand All @@ -26,6 +28,8 @@ public override void OnInitialize(GunStats stats)

public override void OnReload(GunStats stats)
{
if (DoWeNeedToEscape())
return;
exhaustParticles.Play();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public override void OnReload(GunStats stats) { }

public override void OnFire(GunStats stats)
{
if (DoWeNeedToEscape())
return;
OnShotFiredAnimation?.Invoke();
OnAnimationEnd?.Invoke();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RevolverAnimator : AugmentAnimator
private GunBarrel barrel;
private GunExtension extension;

public override void OnFire(GunStats stats){}
public override void OnFire(GunStats stats) { }

public override void OnInitialize(GunStats stats)
{
Expand All @@ -27,6 +27,8 @@ public override void OnInitialize(GunStats stats)

public override void OnReload(GunStats stats)
{
if (DoWeNeedToEscape())
return;
canReload = !canReload;
if (!canReload)
return;
Expand Down
16 changes: 16 additions & 0 deletions Assets/Scripts/Augment/GunController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ private void OnDestroy()
MatchController.Singleton.onRoundEnd -= CancelZoom;
}

// TODO check if this works and possibly remove
public void RemoveListeners()
{
onFireStart = null;
onFire = null;
onFireEnd = null;
onFireNoAmmo = null;
if (!barrelAnimator)
return;

if (HasRecoil)
barrelAnimator.OnShotFiredAnimation -= PlayRecoil;
barrelAnimator.OnShotFiredAnimation -= ShotFired;
barrelAnimator.OnAnimationEnd -= FireEnd;
}

private bool ShouldFire() => !isFiring && fireRateController.shouldFire(triggerPressed, triggerHeld);

[Client]
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Augment/GunFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static GameObject InstantiateGun(Item bodyPrefab, Item barrelPrefab, Item

var firstPersonGunController = gun.GetComponent<GunFactory>().GunController;
var gunAnimations = displayGun.GetComponentsInChildren<AugmentAnimator>(includeInactive: true);
// TODO THESE ARE NEVER UNSUBSCRIBED RIGHT?
foreach (var animation in gunAnimations)
{
animation.OnInitialize(firstPersonGunController.stats);
Expand Down
9 changes: 9 additions & 0 deletions Assets/Scripts/Gamestate/PlayerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,20 @@ public void RemoveGun()
{
Destroy(gunController.transform.GetChild(i).gameObject);
}
// gunController.RemoveListeners();
var gunAnimations = GunOrigin.GetComponentsInChildren<AugmentAnimator>(includeInactive: true);
// Unsubscribe animations (very important)
foreach (var animation in gunAnimations)
{
gunController.onFireStart -= animation.OnFire;
gunController.onReload -= animation.OnReload;
}
Destroy(gunController.gameObject);
for (int i = GunOrigin.transform.childCount - 1; i >= 0; i--)
{
Destroy(GunOrigin.transform.GetChild(i).gameObject);
}
// GunOrigin.GetComponent<GunController>().RemoveListeners();
}

protected void SetLayerOnSubtree(GameObject node, int layer)
Expand Down
2 changes: 0 additions & 2 deletions Assets/Scripts/Interactables/TrainingModeAugmentPlacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public class TrainingModeAugmentPlacer : MonoBehaviour

private void Start()
{
#if UNITY_EDITOR
SpawnAugments();
#endif
}

private void SpawnAugments()
Expand Down
4 changes: 3 additions & 1 deletion Assets/SodiePopper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ public class SodiePopper : GunBody
private float lastPlayerDiff = 0f;

private AudioSource audioSource;

[SerializeField]
private AudioGroup slosh;

public override void Start()
{
gunController = transform.parent?.GetComponent<GunController>();
Expand Down Expand Up @@ -101,7 +103,7 @@ private void FixedUpdate()
lastReload = Time.fixedTime;
if (gunController && gunController.stats.Magazine != gunController.stats.Ammo)
slosh.Play(audioSource);

gunController?.Reload(reload_ammount);
}
lastPos = meassurementPoint.position + diff.normalized * offsetMagnitude;
Expand Down