Skip to content

Commit

Permalink
Fix nail-based attacks not dealing damage to remote entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Oct 9, 2023
1 parent 897821b commit d83211b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 94 deletions.
7 changes: 6 additions & 1 deletion HKMP/Animation/AnimationEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public void SetServerSettings(ServerSettings serverSettings) {
/// player taking knock back from remote players hitting shields etc.
/// </summary>
/// <param name="targetObject">The target GameObject to change.</param>
protected static void ChangeAttackTypeOfFsm(GameObject targetObject) {
/// <param name="direction">The direction in float that the damage is coming from.</param>
protected static void ChangeAttackTypeOfFsm(GameObject targetObject, float direction) {
var damageFsm = targetObject.LocateMyFSM("damages_enemy");
if (damageFsm == null) {
return;
Expand All @@ -42,5 +43,9 @@ protected static void ChangeAttackTypeOfFsm(GameObject targetObject) {
takeDamage.AttackType.Value = (int) AttackTypes.Generic;
takeDamage = damageFsm.GetFirstAction<TakeDamage>("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;
}
}
4 changes: 2 additions & 2 deletions HKMP/Animation/Effects/CycloneSlash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
49 changes: 20 additions & 29 deletions HKMP/Animation/Effects/DashSlash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);

Expand All @@ -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<PolygonCollider2D>().points =
pvpCollider.GetComponent<PolygonCollider2D>().points =
dashSlash.GetComponent<PolygonCollider2D>().points;

if (ServerSettings.AllowParries) {
AddParryFsm(dashSlashCollider);
AddParryFsm(pvpCollider);
}

if (damage != 0) {
dashSlashCollider.AddComponent<DamageHero>().damageDealt = damage;
pvpCollider.AddComponent<DamageHero>().damageDealt = damage;
}
}

// Get the animator, figure out the duration of the animation and destroy the object accordingly afterwards
var dashSlashAnimator = dashSlash.GetComponent<tk2dSpriteAnimator>();
var dashSlashAnimationDuration = dashSlashAnimator.DefaultClip.frames.Length / dashSlashAnimator.ClipFps;
var defaultClip = dashSlashAnimator.DefaultClip;
var dashSlashAnimationDuration = defaultClip.frames.Length / defaultClip.fps;

Object.Destroy(dashSlash, dashSlashAnimationDuration);
}
Expand Down
26 changes: 14 additions & 12 deletions HKMP/Animation/Effects/GreatSlash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<PolygonCollider2D>().points =
greatSlash.GetComponent<PolygonCollider2D>().points;

if (ServerSettings.AllowParries) {
AddParryFsm(greatSlash);
AddParryFsm(pvpCollider);
}

if (damage != 0) {
greatSlash.AddComponent<DamageHero>().damageDealt = damage;
pvpCollider.AddComponent<DamageHero>().damageDealt = damage;
}
}

Expand Down
74 changes: 24 additions & 50 deletions HKMP/Animation/Effects/SlashBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -147,69 +160,30 @@ protected void Play(GameObject playerObject, bool[] effectInfo, GameObject prefa
slash.GetComponent<MeshRenderer>().enabled = true;

var polygonCollider = slash.GetComponent<PolygonCollider2D>();

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<PolygonCollider2D>();
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<PolygonCollider2D>().points = polygonCollider.points;

if (ServerSettings.AllowParries) {
AddParryFsm(slash);
AddParryFsm(pvpCollider);
}

if (damage != 0) {
slash.AddComponent<DamageHero>().damageDealt = damage;
pvpCollider.AddComponent<DamageHero>().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
Expand Down
1 change: 1 addition & 0 deletions HKMP/Ui/Component/ChatInputComponent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using Hkmp.Networking.Packet.Data;
using Hkmp.Ui.Resources;
using Hkmp.Util;
Expand Down

0 comments on commit d83211b

Please sign in to comment.