Skip to content
Snippets Groups Projects
Verified Commit 91056c63 authored by Adrian Paschkowski's avatar Adrian Paschkowski :thinking:
Browse files

AntiHero: Add delay before retargeting

parent 0a15d68e
No related branches found
No related tags found
No related merge requests found
......@@ -116,6 +116,7 @@ MonoBehaviour:
currentHealth: 100
maxDistanceToEnemy: 1
movementSpeed: 4
delayUntilNextTarget: 0.3
--- !u!95 &2655505060201611074
Animator:
serializedVersion: 3
......
......@@ -8,6 +8,8 @@ public class AntiPlayer : Fighter, IFighterCallback
[SerializeField, Tooltip("How close the AntiPlayer has to be to start attacking, or how far away it has to be to start running towards the target")]
float maxDistanceToEnemy = 1f;
[SerializeField] float movementSpeed = 4f;
[SerializeField, Tooltip("After killing an Enemy, how long to stay idle before targeting the next enemy")]
float delayUntilNextTarget = 0.3f;
List<Fighter> enemies = new List<Fighter>();
Fighter currentTargetEnemy;
......@@ -81,6 +83,9 @@ public class AntiPlayer : Fighter, IFighterCallback
public void OnFighterDeath(Fighter fighter)
{
enemies.Remove(fighter);
CalculateClosestEnemy();
// Wait a short time before targeting next enemy. This is to avoid interrupting the current
// attack animation, which would happen if we immediately ran to the next target.
currentTargetEnemy = null;
Invoke(nameof(CalculateClosestEnemy), delayUntilNextTarget);
}
}
......@@ -22,7 +22,7 @@ public abstract class Fighter : MonoBehaviour
[SerializeField] protected int currentHealth = 100;
protected Animator animator;
protected new Rigidbody2D rigidbody;
protected SpriteRenderer spriteRenderer;
protected BoxCollider2D collider;
protected bool alive { get => currentHealth > 0; }
protected FighterTypes fighterType;
protected string opponentTag;
......@@ -39,7 +39,7 @@ public abstract class Fighter : MonoBehaviour
{
animator = GetComponent<Animator>();
rigidbody = GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
collider = GetComponent<BoxCollider2D>();
fighterType = FighterTypes.ENEMY;
initalPosition = transform.position;
initalScale = transform.localScale;
......@@ -83,6 +83,10 @@ public abstract class Fighter : MonoBehaviour
{
animator.SetTrigger("Death");
callbacks.ForEach(c => c.OnFighterDeath(this));
// Disable own collider so that other Fighters can run over our dead body
collider.enabled = false;
// Let's not fall through the world, without any active colliders
rigidbody.bodyType = RigidbodyType2D.Static;
}
else
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment