Newer
Older
// Base Enemy class, includes some functionality common to all Enemies, such as a reference
// to the AntiPlayer and that they always face towards it.
public abstract class Enemy : Fighter
{
[SerializeField, Tooltip("How close the AntiPlayer has to be in order for this Enemy to start attacking")]
protected float playerDistanceToAttack = 1.25f;
[SerializeField, Tooltip("Whether the Enemy is facing left in the Sprite Sheet. Used to determine which way to face.")]
bool spriteIsFacingLeft = true;
protected GameObject antiPlayer;
protected override void Awake()
{
base.Awake();
antiPlayer = GameObject.FindGameObjectWithTag("AntiPlayer");
RoundController.instance.placedEnemies.Add(this);
}
protected override void Update()
{
base.Update();
{
// Always face the AntiPlayer
var scale = transform.localScale;
scale.x = transform.position.x < antiPlayer.transform.position.x ? -initalScale.x : initalScale.x;
if (!spriteIsFacingLeft)
{
scale.x = -scale.x;
}
transform.localScale = scale;
}
}
protected override bool CanAttack()
{
return GetDistanceToAntiPlayer() < playerDistanceToAttack;
}
protected override void Attack()
{
animator.SetTrigger("Attack");
}
public override void OnRoundStart()
{
base.OnRoundStart();
animator.ResetTrigger("Attack");
}
public override void OnRoundEnd(bool won)
// Destroy all dead Enemies after a successful round. Otherwise, if either
// the round was lost of the Enemy was still alive at the end of the round,
// it will be resurrected.
{
Destroy(gameObject);
return;
}
protected float GetDistanceToAntiPlayer()
{
return Vector3.Distance(transform.position, antiPlayer.transform.position);
}
protected override void OnDestroy()
{
base.OnDestroy();
RoundController.instance.placedEnemies.Remove(this);
}