Files
Gnome-s-Bounty/Assets/Scripts/EnemyAI.cs
T

83 lines
2.1 KiB
C#

using UnityEngine;
public class EnemyAI : Character
{
private bool _needRespawn = false;
private int _respawnTimeout = 4;
private float _respawnElementTimer;
protected override void OnDeath()
{
_needRespawn = true;
_spriteRenderer.enabled =false;
}
protected override void SetClimbingAnimation(bool isClimbing)
{
}
protected override void SetWalkingAnimation(bool isWalking)
{
}
private void Update()
{
if(_needRespawn)
{
_respawnElementTimer -= Time.deltaTime;
if (_respawnElementTimer <= 0)
{
_respawnElementTimer = _respawnTimeout;
_needRespawn = false;
_spriteRenderer.enabled =true;
Spawn();
}
return;
}
float horizontal = 0;
float vertical = 0;
Vector2 directionToPlayer = Player.Instance.transform.position - transform.position;
directionToPlayer.Normalize();
float verticalDistance = Player.Instance.transform.position.y - transform.position.y;
if (verticalDistance > 0 && isCanClimbUp && isAllowVertical)
{
vertical = VerticalMove(verticalDistance);
}
else if (verticalDistance < 0 && isAllowVertical && isCanGoDown)
{
vertical = VerticalMove(verticalDistance);
}
else
{
if (Mathf.Abs(Player.Instance.transform.position.x - transform.position.x) < 0.1f)
{
print($"horizontal block");
horizontal = 0;
}
else if (directionToPlayer.x < 0)
{ horizontal = -1; }
else if (directionToPlayer.x > 0)
{ horizontal = 1; }
}
print($"horizontal {horizontal}");
if (Input.GetKey(KeyCode.T))
{ vertical = 1; }
if (Input.GetKey(KeyCode.G))
{ vertical = -1; }
base.MoveTo(horizontal, vertical);
}
private float VerticalMove(float verticalDistance)
{
float verticalDirection = Mathf.Sign(verticalDistance);
return verticalDirection;
}
}