From e5ef9e03c727d382f52e49832b2cc9455c3a727b Mon Sep 17 00:00:00 2001 From: in0finite Date: Mon, 8 Jul 2019 16:17:55 +0200 Subject: [PATCH] sync weapon fire event --- .../Ped/States/BaseAimMovementState.cs | 3 +++ .../Behaviours/Ped/States/BaseScriptState.cs | 10 ++++++++ .../Behaviours/Ped/States/IPedState.cs | 2 ++ Assets/Scripts/Networking/PedSync.cs | 24 +++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/Assets/Scripts/Behaviours/Ped/States/BaseAimMovementState.cs b/Assets/Scripts/Behaviours/Ped/States/BaseAimMovementState.cs index eb80c0fa..6fdc761f 100644 --- a/Assets/Scripts/Behaviours/Ped/States/BaseAimMovementState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/BaseAimMovementState.cs @@ -448,6 +448,9 @@ namespace SanAndreasUnity.Behaviours.Peds.States // play firing sound F.RunExceptionSafe (() => weapon.PlayFireSound() ); + // notify clients + Net.PedSync.OnWeaponFired(m_ped, weapon, weapon.GetFirePos()); + return true; } diff --git a/Assets/Scripts/Behaviours/Ped/States/BaseScriptState.cs b/Assets/Scripts/Behaviours/Ped/States/BaseScriptState.cs index a83a07e5..8ce6f1b8 100644 --- a/Assets/Scripts/Behaviours/Ped/States/BaseScriptState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/BaseScriptState.cs @@ -285,6 +285,16 @@ namespace SanAndreasUnity.Behaviours.Peds.States m_ped.WeaponHolder.SwitchWeapon(newSlot); } + public virtual void OnWeaponFiredFromServer(Weapon weapon, Vector3 firePos) + { + // update gun flash + if (weapon.GunFlash != null) + weapon.GunFlash.gameObject.SetActive (true); + weapon.UpdateGunFlashRotation (); + + weapon.PlayFireSound(); + } + } } diff --git a/Assets/Scripts/Behaviours/Ped/States/IPedState.cs b/Assets/Scripts/Behaviours/Ped/States/IPedState.cs index 7303b1b0..dab04868 100644 --- a/Assets/Scripts/Behaviours/Ped/States/IPedState.cs +++ b/Assets/Scripts/Behaviours/Ped/States/IPedState.cs @@ -29,6 +29,8 @@ namespace SanAndreasUnity.Behaviours.Peds.States void OnChangedWeaponByServer(int newSlot); + void OnWeaponFiredFromServer(Weapon weapon, Vector3 firePos); + } } diff --git a/Assets/Scripts/Networking/PedSync.cs b/Assets/Scripts/Networking/PedSync.cs index d0117efa..10e2c477 100644 --- a/Assets/Scripts/Networking/PedSync.cs +++ b/Assets/Scripts/Networking/PedSync.cs @@ -137,5 +137,29 @@ namespace SanAndreasUnity.Net } + public static void OnWeaponFired(Ped ped, Weapon weapon, Vector3 firePos) + { + foreach (var p in Player.AllPlayersEnumerable) + p.GetComponent().TargetOnWeaponFired(p.connectionToClient, ped.gameObject, weapon.gameObject, firePos); + } + + [TargetRpc] + void TargetOnWeaponFired(NetworkConnection conn, GameObject pedGo, GameObject weaponGo, Vector3 firePos) + { + if (NetStatus.IsServer) + return; + if (null == weaponGo || null == pedGo) + return; + + F.RunExceptionSafe( () => { + var ped = pedGo.GetComponent(); + var weapon = weaponGo.GetComponent(); + + if (ped.CurrentState != null) + ped.CurrentState.OnWeaponFiredFromServer(weapon, firePos); + }); + + } + } }