mycj_demo/mycj/Assets/Game/Scripts/Application/Object/FBMonster.cs

144 lines
3.5 KiB
C#
Raw Normal View History

2024-12-02 09:37:47 +08:00
// Felix-BangFBMonster
//   へ     /|
//  /7    ∠_/
//  / │    
//  Z _,    /`ヽ
// │     ヽ   /  〉
//  Y     `  /  /
// イ● 、 ●  ⊂⊃〈  /
// ()  へ    | \〈
//  >ー 、_  ィ  │
//  / へ   / ノ<|
//  ヽ_ノ  (_  │//
//  7       |
//  ―r ̄ ̄`ー―_
// Describe怪物
// Createtime2018/10/15
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace FBApplication
{
public class FBMonster : FBRole
{
#region
public const float CLOSEDDISTANCE = 0.1f;
#endregion
#region
public event Action<FBMonster> ReachedAction;
#endregion
#region
[SerializeField]
MonsterType f_type = MonsterType.Monster0;
float f_moveSpeed; //移动速度
Vector3[] f_path=null; //路径拐点
int f_pathIndex = -1; //下一拐点索引
bool f_isReached = false; //是否到达终点
#endregion
#region
public float MoveSpeed
{
get { return f_moveSpeed; }
set
{
f_moveSpeed = value;
}
}
#endregion
#region Unity回调
void Update ()
{
if (f_isReached)
return;
Vector3 pos = transform.position;
Vector3 dest = f_path[f_pathIndex + 1];
float dis = Vector3.Distance(pos,dest);
if (dis < CLOSEDDISTANCE)
{
MoveTo(dest);
if (HasNext())
MoveNext();
else
{
f_isReached = true;
//触发到达终点的事件
if (ReachedAction != null)
ReachedAction(this);
}
}
else
{
Vector3 direction = (dest - pos).normalized;
transform.Translate(direction * f_moveSpeed * Time.deltaTime);
}
}
#endregion
#region
public override void OnSpawn()
{
base.OnSpawn();
FBMonsterInfo info = FBGame.Instance.StaticData.GetMoster(f_type);
MaxHP = info.HP;
HP = info.HP;
MoveSpeed = info.MoveSpeed;
}
public override void OnUnspawn()
{
base.OnUnspawn();
f_moveSpeed = 0;
f_path = null;
f_pathIndex = -1;
f_isReached = false;
ReachedAction = null;
}
#endregion
#region
public void OnLoad(Vector3[] path)
{
f_path = path;
MoveNext();
}
private bool HasNext()
{
return f_pathIndex + 1 < f_path.Length - 1;
}
private void MoveNext()
{
if (!HasNext())
return;
if (f_pathIndex == -1)
{
f_pathIndex = 0;
MoveTo(f_path[f_pathIndex]);
}
else
f_pathIndex++;
}
private void MoveTo(Vector3 position)
{
transform.position = position;
}
#endregion
}
}