NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Fight/Protect/BeProtectUnit.cs

150 lines
4.7 KiB
C#
Raw Normal View History

2024-06-04 14:12:03 +08:00
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Gameplay.Area;
2024-06-04 14:57:24 +08:00
using Gameplay.Buff;
using Gameplay.Character;
using Gameplay.Fight.Protect.Anim;
using Gameplay.Fight.Protect.Data;
2024-06-03 20:00:42 +08:00
using Gameplay.Fight.Protect.State;
using Gameplay.Performance;
2024-06-03 20:00:42 +08:00
using Gameplay.Skill;
using Gameplay.Unit;
using Gameplay.Unit.Data;
2024-06-03 20:00:42 +08:00
using LTGame;
using PhxhSDK;
using UnityEngine;
namespace Gameplay.Fight.Protect
{
public class BeProtectUnit : GameUnit
{
public BeProtectDataConfig configData;
public BeProtectUnitAnim anim;
2024-06-03 20:00:42 +08:00
private NStateMachine<BaseBeProtectState> _fsm;
2024-06-04 14:12:03 +08:00
public List<int> pathCellIndexs;
public List<Vector3> displayPath;
public int currentMoveIndex;
2024-06-12 18:14:56 +08:00
private BeProtectTargetPoint _targetPoint;
2024-06-04 14:12:03 +08:00
public BeProtectUnit(uint id,
int troopId, BeProtectDataConfig configData) : base(id, troopId, EGameUnitType.BeProtect)
{
this.configData = configData;
if (this.configData == null)
{
DebugUtil.LogError("BeProtectUnit配置为空");
return;
}
debugShowInfo = new DebugShowInfo();
debugShowInfo.name = configData.rawConfig.Name;
debugShowInfo.configId = "ProtectUnitConfig@" + configData.rawConfig.ID;
debugShowInfo.iconPath = "";
commonData.canControl = false;
commonData.standLevel = configData.rawConfig.StandCrossLevel;
commonData.crossLevel = configData.rawConfig.MoveCrossLevel;
2024-06-04 14:12:03 +08:00
pathCellIndexs = new List<int>();
displayPath = new List<Vector3>();
}
2024-06-12 18:14:56 +08:00
public void CreateTargetPoint(int targetIndex)
{
controlData.targetCellIndex = targetIndex;
_targetPoint = new BeProtectTargetPoint(targetIndex);
}
2024-06-03 20:00:42 +08:00
private void _InitFsm()
{
_fsm = new NStateMachine<BaseBeProtectState>();
_fsm.Add(new BeProtectStateIdle(this));
_fsm.Add(new BeProtectStateMove(this));
_fsm.Add(new BeProtectStateDead(this));
2024-06-04 14:12:03 +08:00
_fsm.Add(new BeProtectStateFindPath(this));
_fsm.Add(new BeProtectStateFinish(this));
2024-06-03 20:00:42 +08:00
}
public async UniTask CreateAsync()
{
GameUnitManager.instance.RecordLoadPath(configData.modelPath);
var sampleObj = await AssetManager.Instance.LoadAssetAsync<GameObject>(configData.modelPath);
if (sampleObj == null)
{
DebugUtil.LogError("加载BeProtectUnit失败: {0}", configData.modelPath);
return;
}
if (Node == null) return;
var parent = Node.GameObject.transform;
var go = Object.Instantiate(sampleObj, parent, true);
go.transform.ResetLocals();
Node.GameObject.SetLayerEx(Node.GameObject.layer);
anim = new BeProtectUnitAnim(go);
// 设置后处理
var obj = Node.GameObject;
var isEnemy = commonData.troopId != ETroopsId.Self;
OutlineManager.instance.AutoAddFromObj(obj, isEnemy);
PerformanceManager.instance.HandleCreateUnit(obj);
}
2024-06-03 20:00:42 +08:00
protected override async UniTask _OnPrepare()
{
_InitFsm();
_InitProps();
2024-06-04 14:57:24 +08:00
BuffManager.instance.InitBeProtectUnit(this);
2024-06-03 20:00:42 +08:00
}
2024-06-04 14:12:03 +08:00
protected override void _OnEnterBattle()
{
base._OnEnterBattle();
2024-06-04 14:57:24 +08:00
isReady = true;
2024-06-04 14:12:03 +08:00
_fsm.ChangeState(EBeProtectState.FindPath);
AreaManager.instance.RegistBeProtectUnit(this);
}
2024-06-03 20:00:42 +08:00
private void _InitProps()
{
aliveData.maxHp = configData.rawConfig.HP;
aliveData.maxMorale = 0;
aliveData.maxShield = configData.rawConfig.Shield;
aliveData.Init(true);
fightData.moveSpeed = configData.rawConfig.MoveSpeed;
fightData.turnSpeed = configData.rawConfig.TurnSpeed;
}
protected override void _OnLogicUpdate(float dt)
{
if (SkillManager.instance.needSkillPaused && SkillManager.instance.inSkillingUnit != this)
{
return;
}
_fsm.LogicUpdate(dt);
_ViewUpdate(dt);
}
2024-06-03 20:00:42 +08:00
private void _ViewUpdate(float dt)
{
if (Node?.GameObject == null) return;
// 更新trans
if (transData.isTransChanged)
{
Node.GameObject.transform.SetPositionAndRotation(transData.pos, transData.rot);
}
transData.Reset();
anim.ViewUpdate(dt);
}
}
}