Skip to content

Commit

Permalink
Fix weapon switching in build, resolves #436
Browse files Browse the repository at this point in the history
  • Loading branch information
Fueredoriku committed Jan 7, 2025
1 parent 35bda07 commit d84bdb8
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
10 changes: 10 additions & 0 deletions Assets/Scripts/Augment/AugmentImplementations/AugmentAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 4 additions & 0 deletions Assets/Scripts/Augment/GunController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using Mirror;
using UnityEngine;
using UnityEngine.InputSystem;
Expand Down Expand Up @@ -135,6 +136,9 @@ public void UnsubscribeDelegates()
barrelAnimator.OnAnimationEnd -= FireEnd;
}

GetComponentsInChildren<AugmentAnimator>(includeInactive: true).ToList()
.ForEach(animation => animation.Desubscribe(this));

if (!Player || !Player.inputManager)
return;

Expand Down
10 changes: 8 additions & 2 deletions Assets/Scripts/Augment/GunFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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*.
Expand Down Expand Up @@ -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<GunFactory>().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<GunFactory>();
Expand Down
2 changes: 2 additions & 0 deletions Assets/Scripts/Gamestate/PlayerManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Mirror;
using System.Collections;
using System.Linq;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering.Universal;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Interactables/TrainingModeAugment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
2 changes: 0 additions & 2 deletions Assets/Scripts/Interactables/TrainingModeAugmentPlacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ public class TrainingModeAugmentPlacer : MonoBehaviour

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

private void SpawnAugments()
Expand Down

0 comments on commit d84bdb8

Please sign in to comment.