Files
ClickRPG/Assets/Scripts/Enemy.cs

118 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : Character
{
public enum State
{
Idle,
Chase,
Attack
}
private State curState = State.Idle;
[Header("Attack")]
private float lastAttackTime;
//[SerializeField] private GameObject attackPrefab;
private float targetDistance;
void Start ()
{
target = Player.Current;
}
void Update ()
{
if (target == null)
return;
// Calculate distance to target.
targetDistance = Vector3.Distance(transform.position, target.transform.position);
// Run the respective update function based on our current state.
switch(curState)
{
case State.Idle:
IdleUpdate();
break;
case State.Attack:
AttackUpdate();
break;
case State.Chase:
ChaseUpdate();
break;
}
}
// Called when we want to change our state.
void SetState (State newState)
{
curState = newState;
// On enter state.
switch(curState)
{
case State.Idle:
Controller.StopMovement();
break;
case State.Chase:
Controller.MoveToTarget(target.transform);
break;
case State.Attack:
Controller.StopMovement();
break;
}
}
// Called every frame while in the IDLE state.
void IdleUpdate()
{
if (targetDistance < ChaseRange && targetDistance > AttackRange)
SetState(State.Chase);
else if (targetDistance < AttackRange)
SetState(State.Attack);
}
// Called every frame while in the CHASE state.
void ChaseUpdate ()
{
if (targetDistance > ChaseRange)
SetState(State.Idle);
else if (targetDistance < AttackRange)
SetState(State.Attack);
}
// Called every frame while in the ATTACK state.
void AttackUpdate ()
{
if (targetDistance > AttackRange)
SetState(State.Chase);
Controller.LookTowards(target.transform.position - transform.position);
if(Time.time - lastAttackTime > AttackRate)
{
lastAttackTime = Time.time;
AttackTarget();
}
}
// Create a projectile and shoot it at our target.
void AttackTarget ()
{
Attack(target);
/*if (attackType == AttackType.Melee)
{
target.TakeDamage(Damage);
return;
}
else
{
GameObject proj = Instantiate(attackPrefab, transform.position + Vector3.up, Quaternion.LookRotation(target.transform.position - transform.position));
proj.GetComponent<Projectile>().Setup(this);
}*/
}
}