From d83211b4550f1bb1f305d5aa4f83aadc584419f6 Mon Sep 17 00:00:00 2001 From: Extremelyd1 Date: Mon, 9 Oct 2023 18:48:19 +0200 Subject: [PATCH] Fix nail-based attacks not dealing damage to remote entities --- HKMP/Animation/AnimationEffect.cs | 7 ++- HKMP/Animation/Effects/CycloneSlash.cs | 4 +- HKMP/Animation/Effects/DashSlash.cs | 49 +++++++--------- HKMP/Animation/Effects/GreatSlash.cs | 26 +++++---- HKMP/Animation/Effects/SlashBase.cs | 74 ++++++++----------------- HKMP/Ui/Component/ChatInputComponent.cs | 1 + 6 files changed, 67 insertions(+), 94 deletions(-) diff --git a/HKMP/Animation/AnimationEffect.cs b/HKMP/Animation/AnimationEffect.cs index c430c28..af434d0 100644 --- a/HKMP/Animation/AnimationEffect.cs +++ b/HKMP/Animation/AnimationEffect.cs @@ -30,7 +30,8 @@ public void SetServerSettings(ServerSettings serverSettings) { /// player taking knock back from remote players hitting shields etc. /// /// The target GameObject to change. - protected static void ChangeAttackTypeOfFsm(GameObject targetObject) { + /// The direction in float that the damage is coming from. + protected static void ChangeAttackTypeOfFsm(GameObject targetObject, float direction) { var damageFsm = targetObject.LocateMyFSM("damages_enemy"); if (damageFsm == null) { return; @@ -42,5 +43,9 @@ protected static void ChangeAttackTypeOfFsm(GameObject targetObject) { takeDamage.AttackType.Value = (int) AttackTypes.Generic; takeDamage = damageFsm.GetFirstAction("Grandparent"); takeDamage.AttackType.Value = (int) AttackTypes.Generic; + + // Find the variable that controls the slash direction for damaging enemies + var directionVar = damageFsm.FsmVariables.GetFsmFloat("direction"); + directionVar.Value = direction; } } diff --git a/HKMP/Animation/Effects/CycloneSlash.cs b/HKMP/Animation/Effects/CycloneSlash.cs index c6c356a..f17a5d5 100644 --- a/HKMP/Animation/Effects/CycloneSlash.cs +++ b/HKMP/Animation/Effects/CycloneSlash.cs @@ -50,10 +50,10 @@ public override void Play(GameObject playerObject, bool[] effectInfo) { cycloneSlash.layer = 17; var hitLComponent = cycloneSlash.FindGameObjectInChildren("Hit L"); - ChangeAttackTypeOfFsm(hitLComponent); + ChangeAttackTypeOfFsm(hitLComponent, 0f); var hitRComponent = cycloneSlash.FindGameObjectInChildren("Hit R"); - ChangeAttackTypeOfFsm(hitRComponent); + ChangeAttackTypeOfFsm(hitRComponent, 180f); cycloneSlash.SetActive(true); diff --git a/HKMP/Animation/Effects/DashSlash.cs b/HKMP/Animation/Effects/DashSlash.cs index 34eb384..e05b89d 100644 --- a/HKMP/Animation/Effects/DashSlash.cs +++ b/HKMP/Animation/Effects/DashSlash.cs @@ -35,11 +35,13 @@ public override void Play(GameObject playerObject, bool[] effectInfo) { dashSlashObject, playerObject.transform.parent ); + dashSlash.layer = 17; // Since we anchor the dash slash on the player container instead of the player object // (to prevent it from flipping when the knight turns around) we need to adjust the scale based // on which direction the knight is facing - var playerScaleX = playerObject.transform.localScale.x; + var localScale = playerObject.transform.localScale; + var playerScaleX = localScale.x; var dashSlashTransform = dashSlash.transform; var dashSlashScale = dashSlashTransform.localScale; dashSlashTransform.localScale = new Vector3( @@ -48,18 +50,9 @@ public override void Play(GameObject playerObject, bool[] effectInfo) { dashSlashScale.z ); - dashSlash.layer = 22; - - ChangeAttackTypeOfFsm(dashSlash); - - // // Get the "damages_enemy" FSM from the dash slash object - // var slashFsm = dashSlash.LocateMyFSM("damages_enemy"); - // // Find the variable that controls the slash direction for damaging enemies - // var directionVar = slashFsm.FsmVariables.GetFsmFloat("direction"); - // - // // Set it based on the direction the knight is facing - // var facingRight = playerScaleX > 0; - // directionVar.Value = facingRight ? 180f : 0f; + // Check which direction the knight is facing for the damages_enemy FSM + var facingRight = localScale.x > 0; + ChangeAttackTypeOfFsm(dashSlash, facingRight ? 180f : 0f); dashSlash.SetActive(true); @@ -68,38 +61,36 @@ public override void Play(GameObject playerObject, bool[] effectInfo) { // Set the newly instantiate collider to state Init, to reset it // in case the local player was already performing it - dashSlash.LocateMyFSM("Control Collider").SetState("Init"); + var controlColliderFsm = dashSlash.LocateMyFSM("Control Collider"); + if (controlColliderFsm.ActiveStateName != "Init") { + controlColliderFsm.SetState("Init"); + } var damage = ServerSettings.DashSlashDamage; if (ServerSettings.IsPvpEnabled && ShouldDoDamage) { - // Somehow adding a DamageHero component or the parry FSM simply to the dash slash object doesn't work, - // so we create a separate object for it - var dashSlashCollider = Object.Instantiate( - new GameObject( - "DashSlashCollider", - typeof(PolygonCollider2D) - ), - dashSlash.transform - ); - dashSlashCollider.SetActive(true); - dashSlashCollider.layer = 22; + // Since the dash slash should deal damage to other players, we create a separate object for that purpose + var pvpCollider = new GameObject("PvP Collider", typeof(PolygonCollider2D)); + pvpCollider.transform.SetParent(dashSlash.transform); + pvpCollider.SetActive(true); + pvpCollider.layer = 22; // Copy over the polygon collider points - dashSlashCollider.GetComponent().points = + pvpCollider.GetComponent().points = dashSlash.GetComponent().points; if (ServerSettings.AllowParries) { - AddParryFsm(dashSlashCollider); + AddParryFsm(pvpCollider); } if (damage != 0) { - dashSlashCollider.AddComponent().damageDealt = damage; + pvpCollider.AddComponent().damageDealt = damage; } } // Get the animator, figure out the duration of the animation and destroy the object accordingly afterwards var dashSlashAnimator = dashSlash.GetComponent(); - var dashSlashAnimationDuration = dashSlashAnimator.DefaultClip.frames.Length / dashSlashAnimator.ClipFps; + var defaultClip = dashSlashAnimator.DefaultClip; + var dashSlashAnimationDuration = defaultClip.frames.Length / defaultClip.fps; Object.Destroy(dashSlash, dashSlashAnimationDuration); } diff --git a/HKMP/Animation/Effects/GreatSlash.cs b/HKMP/Animation/Effects/GreatSlash.cs index 4b5735d..15f382c 100644 --- a/HKMP/Animation/Effects/GreatSlash.cs +++ b/HKMP/Animation/Effects/GreatSlash.cs @@ -37,18 +37,11 @@ public override void Play(GameObject playerObject, bool[] effectInfo) { playerAttacks.transform ); greatSlash.layer = 17; - - ChangeAttackTypeOfFsm(greatSlash); - // Get the "damages_enemy" FSM from the great slash object - var slashFsm = greatSlash.LocateMyFSM("damages_enemy"); - // Find the variable that controls the slash direction for damaging enemies - var directionVar = slashFsm.FsmVariables.GetFsmFloat("direction"); - - // Set it based on the direction the knight is facing + // Check which direction the knight is facing for the damages_enemy FSM var facingRight = playerObject.transform.localScale.x > 0; - directionVar.Value = facingRight ? 180f : 0f; - + ChangeAttackTypeOfFsm(greatSlash, facingRight ? 180f : 0f); + greatSlash.SetActive(true); // Set the newly instantiate collider to state Init, to reset it @@ -57,12 +50,21 @@ public override void Play(GameObject playerObject, bool[] effectInfo) { var damage = ServerSettings.GreatSlashDamage; if (ServerSettings.IsPvpEnabled && ShouldDoDamage) { + // Since the great slash should deal damage to other players, we create a separate object for that purpose + var pvpCollider = new GameObject("PvP Collider", typeof(PolygonCollider2D)); + pvpCollider.transform.SetParent(greatSlash.transform); + pvpCollider.SetActive(true); + pvpCollider.layer = 22; + + pvpCollider.GetComponent().points = + greatSlash.GetComponent().points; + if (ServerSettings.AllowParries) { - AddParryFsm(greatSlash); + AddParryFsm(pvpCollider); } if (damage != 0) { - greatSlash.AddComponent().damageDealt = damage; + pvpCollider.AddComponent().damageDealt = damage; } } diff --git a/HKMP/Animation/Effects/SlashBase.cs b/HKMP/Animation/Effects/SlashBase.cs index 516d778..5461c24 100644 --- a/HKMP/Animation/Effects/SlashBase.cs +++ b/HKMP/Animation/Effects/SlashBase.cs @@ -60,8 +60,21 @@ protected void Play(GameObject playerObject, bool[] effectInfo, GameObject prefa // Instantiate the slash gameObject from the given prefab // and use the attack gameObject as transform reference var slash = Object.Instantiate(prefab, playerAttacks.transform); - slash.layer = 22; + slash.layer = 17; + float direction; + if (type is SlashType.Wall or SlashType.Normal or SlashType.Alt) { + // For wall, normal and alt slash, we need to check the direction the knight is facing + var facingRight = playerObject.transform.localScale.x > 0; + direction = facingRight ? 180f : 0f; + } else if (type is SlashType.Up) { + direction = 90f; + } else { + direction = 270f; + } + + ChangeAttackTypeOfFsm(slash, direction); + // Set the base scale of the slash based on the slash type, this prevents remote nail slashes to occur // larger than they should be if they are based on the prefab from Long Nail/Mark of Pride/both slash var baseScale = _baseScales[type]; @@ -147,69 +160,30 @@ protected void Play(GameObject playerObject, bool[] effectInfo, GameObject prefa slash.GetComponent().enabled = true; var polygonCollider = slash.GetComponent(); - polygonCollider.enabled = true; - // Instantiate additional game object that can interact with enemies so remote enemies can be hit - GameObject enemySlash; - { - enemySlash = Object.Instantiate(prefab, playerAttacks.transform); - enemySlash.layer = 17; - enemySlash.name = "Enemy Slash"; - enemySlash.transform.localScale = slash.transform.localScale; - - var typesToRemove = new[] { - typeof(MeshFilter), typeof(MeshRenderer), typeof(tk2dSprite), typeof(tk2dSpriteAnimator), - typeof(NailSlash), - typeof(AudioSource) - }; - foreach (var typeToRemove in typesToRemove) { - Object.Destroy(enemySlash.GetComponent(typeToRemove)); - } - - for (var i = 0; i < enemySlash.transform.childCount; i++) { - Object.Destroy(enemySlash.transform.GetChild(i)); - } - - polygonCollider = enemySlash.GetComponent(); - polygonCollider.enabled = true; - - var damagesEnemyFsm = slash.LocateMyFSM("damages_enemy"); - Object.Destroy(damagesEnemyFsm); - - ChangeAttackTypeOfFsm(enemySlash); - - // Get the "damages_enemy" FSM from the slash object - var slashFsm = enemySlash.LocateMyFSM("damages_enemy"); - // Find the variable that controls the slash direction for damaging enemies - var directionVar = slashFsm.FsmVariables.GetFsmFloat("direction"); - - if (type is SlashType.Wall or SlashType.Normal or SlashType.Alt) { - // For wall, normal and alt slash, we need to check the direction the knight is facing - var facingRight = playerObject.transform.localScale.x > 0; - directionVar.Value = facingRight ? 180f : 0f; - } else if (type is SlashType.Up) { - directionVar.Value = 90f; - } else { - directionVar.Value = 270f; - } - } - var damage = ServerSettings.NailDamage; if (ServerSettings.IsPvpEnabled && ShouldDoDamage) { + // Since the slash should deal damage to other players, we create a separate object for that purpose + var pvpCollider = new GameObject("PvP Collider", typeof(PolygonCollider2D)); + pvpCollider.transform.SetParent(slash.transform); + pvpCollider.SetActive(true); + pvpCollider.layer = 22; + + pvpCollider.GetComponent().points = polygonCollider.points; + if (ServerSettings.AllowParries) { - AddParryFsm(slash); + AddParryFsm(pvpCollider); } if (damage != 0) { - slash.AddComponent().damageDealt = damage; + pvpCollider.AddComponent().damageDealt = damage; } } // After the animation is finished, we can destroy the slash object var animationDuration = slashAnimator.CurrentClip.Duration; Object.Destroy(slash, animationDuration); - Object.Destroy(enemySlash, animationDuration); if (!hasGrubberflyElegyCharm || isOnOneHealth && !hasFuryCharm diff --git a/HKMP/Ui/Component/ChatInputComponent.cs b/HKMP/Ui/Component/ChatInputComponent.cs index 1c2b98c..026d24a 100644 --- a/HKMP/Ui/Component/ChatInputComponent.cs +++ b/HKMP/Ui/Component/ChatInputComponent.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Hkmp.Networking.Packet.Data; using Hkmp.Ui.Resources; using Hkmp.Util;