diff --git a/Assets/Scripts/Augment/AugmentImplementations/AugmentAnimator.cs b/Assets/Scripts/Augment/AugmentImplementations/AugmentAnimator.cs index 6e4a8ec4..495a023e 100644 --- a/Assets/Scripts/Augment/AugmentImplementations/AugmentAnimator.cs +++ b/Assets/Scripts/Augment/AugmentImplementations/AugmentAnimator.cs @@ -13,4 +13,14 @@ public abstract class AugmentAnimator : MonoBehaviour public abstract void OnInitialize(GunStats stats); public abstract void OnReload(GunStats stats); public abstract void OnFire(GunStats stats); + public void Subscribe(GunController gun) + { + gun.onFire += OnFire; + gun.onReload += OnReload; + } + public void Desubscribe(GunController gun) + { + gun.onFire -= OnFire; + gun.onReload -= OnReload; + } } diff --git a/Assets/Scripts/Augment/GunController.cs b/Assets/Scripts/Augment/GunController.cs index 926c2a0d..9fd61ed1 100644 --- a/Assets/Scripts/Augment/GunController.cs +++ b/Assets/Scripts/Augment/GunController.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using Mirror; using UnityEngine; using UnityEngine.InputSystem; @@ -135,6 +136,9 @@ public void UnsubscribeDelegates() barrelAnimator.OnAnimationEnd -= FireEnd; } + GetComponentsInChildren(includeInactive: true).ToList() + .ForEach(animation => animation.Desubscribe(this)); + if (!Player || !Player.inputManager) return; diff --git a/Assets/Scripts/Augment/GunFactory.cs b/Assets/Scripts/Augment/GunFactory.cs index 4edb5fae..3f54e58a 100644 --- a/Assets/Scripts/Augment/GunFactory.cs +++ b/Assets/Scripts/Augment/GunFactory.cs @@ -35,8 +35,7 @@ public static GameObject InstantiateGun(Item bodyPrefab, Item barrelPrefab, Item foreach (var animation in gunAnimations) { animation.OnInitialize(firstPersonGunController.stats); - firstPersonGunController.onFireStart += animation.OnFire; - firstPersonGunController.onReload += animation.OnReload; + animation.Subscribe(firstPersonGunController); } // Animator initializers may instantiate objects, so we should set layers *afterwards*. @@ -66,6 +65,13 @@ public static GameObject InstantiateGun(Item bodyPrefab, Item barrelPrefab, Item return gun; } + public static void DesubscribeAnimators(GameObject gun) + { + var firstPersonGunController = gun.GetComponent().GunController; + firstPersonGunController.onFireStart = null; + firstPersonGunController.onReload = null; + } + public static GameObject InstantiateGunAI(Item bodyPrefab, Item barrelPrefab, Item extensionPrefab, PlayerManager owner, Transform parent) { GunFactory displayGun = owner.GunOrigin.gameObject.AddComponent(); diff --git a/Assets/Scripts/Gamestate/PlayerManager.cs b/Assets/Scripts/Gamestate/PlayerManager.cs index 2cbb5898..da86c76d 100644 --- a/Assets/Scripts/Gamestate/PlayerManager.cs +++ b/Assets/Scripts/Gamestate/PlayerManager.cs @@ -1,6 +1,7 @@ using Mirror; using System.Collections; using System.Linq; +using Unity.VisualScripting; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.Rendering.Universal; @@ -507,6 +508,7 @@ public virtual void SetGun(Transform offset) gunController.onFireEnd -= UpdateHudFire; gunController.onReload -= UpdateHudReload; gunController.projectile.OnHitboxCollision -= hudController.HitAnimation; + GunFactory.DesubscribeAnimators(gunController.gameObject); } overrideAimTarget = false; var gun = GunFactory.InstantiateGun(identity.Body, identity.Barrel, identity.Extension, this, offset); diff --git a/Assets/Scripts/Interactables/TrainingModeAugment.cs b/Assets/Scripts/Interactables/TrainingModeAugment.cs index ccf959e0..32a6d450 100644 --- a/Assets/Scripts/Interactables/TrainingModeAugment.cs +++ b/Assets/Scripts/Interactables/TrainingModeAugment.cs @@ -49,6 +49,6 @@ public void Interact(PlayerManager player) player.identity.SetLoadout(body, barrel, extension); player.RemoveGun(); - player.SetGun(player.identity.transform); + player.SetGun(player.inputManager.transform.GetChild(0).GetChild(0)); } } diff --git a/Assets/Scripts/Interactables/TrainingModeAugmentPlacer.cs b/Assets/Scripts/Interactables/TrainingModeAugmentPlacer.cs index c112c406..3848feb5 100644 --- a/Assets/Scripts/Interactables/TrainingModeAugmentPlacer.cs +++ b/Assets/Scripts/Interactables/TrainingModeAugmentPlacer.cs @@ -16,9 +16,7 @@ public class TrainingModeAugmentPlacer : MonoBehaviour private void Start() { -#if UNITY_EDITOR SpawnAugments(); -#endif } private void SpawnAugments()