You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.7 KiB
64 lines
1.7 KiB
using System;
|
|
using UnityEngine;
|
|
|
|
[Serializable]
|
|
public class EnemyChasePlayer : State<Character>
|
|
{
|
|
StatData moveSpeedPerSecStat;
|
|
float moveSpeedPerSec => moveSpeedPerSecStat != null ? moveSpeedPerSecStat.Value : 0;
|
|
|
|
Entity target;
|
|
float sqrtTargetDist;
|
|
|
|
protected override void OnInitialize()
|
|
{
|
|
Owner.Excutor.Data.TryGetStat(StatID.MoveSpeedPerSec, out moveSpeedPerSecStat);
|
|
}
|
|
|
|
public override void Enter()
|
|
{
|
|
if(target is null)
|
|
{
|
|
Owner.ChangeState<EnemyIdle>();
|
|
return;
|
|
}
|
|
|
|
Debug.Assert(Owner.Excutor.SkeletonAnimation.TryGetAnimation("move", out Spine.Animation _), "move animation not exist");
|
|
Owner.Excutor.SkeletonAnimation.AnimationState.SetAnimation(0, "move", true);
|
|
}
|
|
|
|
public override void Excute()
|
|
{
|
|
if(target is null)
|
|
{
|
|
Owner.ChangeState<EnemyIdle>();
|
|
}
|
|
else
|
|
{
|
|
Owner.Excutor.ChangeLookDirection(target.transform);
|
|
Vector2 to = target.Position - Owner.Excutor.Position;
|
|
|
|
if (to.sqrMagnitude <= sqrtTargetDist)
|
|
{
|
|
Owner.ChangeState<EnemyIdle>();
|
|
}
|
|
else
|
|
{
|
|
Vector3 newPosition = Owner.transform.position + (Vector3)(to.normalized * moveSpeedPerSec * Time.fixedDeltaTime);
|
|
Owner.transform.position = newPosition;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void Exit()
|
|
{
|
|
target = null;
|
|
sqrtTargetDist = 0;
|
|
}
|
|
|
|
public void SetTarget(Entity target, float targetDist)
|
|
{
|
|
this.target = target;
|
|
sqrtTargetDist = targetDist * targetDist;
|
|
}
|
|
}
|