NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Building/UnitBuild.cs

141 lines
4.2 KiB
C#
Raw Normal View History

2024-09-09 19:22:22 +08:00
using Cysharp.Threading.Tasks;
using Gameplay.Area;
using Gameplay.Buff;
using Gameplay.Building.Data;
2024-09-10 12:42:32 +08:00
using Gameplay.Building.Impl.Common;
2024-09-09 19:22:22 +08:00
using Gameplay.Character;
using Gameplay.Performance;
2024-09-09 16:10:24 +08:00
using Gameplay.Unit;
2024-09-09 15:30:51 +08:00
using Gameplay.Unit.Data;
2024-09-10 12:32:44 +08:00
using LTGame;
2024-09-09 19:22:22 +08:00
using PhxhSDK;
using UnityEngine;
2024-09-10 12:32:44 +08:00
2024-09-09 16:10:24 +08:00
namespace Gameplay.Building
2024-09-09 15:30:51 +08:00
{
2024-09-09 16:10:24 +08:00
public abstract class UnitBuild : GameUnit
2024-09-09 15:30:51 +08:00
{
2024-09-09 19:22:22 +08:00
public EFightBuildType buildType { get; private set; } = EFightBuildType.None;
2024-09-09 16:10:24 +08:00
2024-09-09 19:22:22 +08:00
public BuildConfigData configData { get; private set; }
2024-09-10 12:32:44 +08:00
protected NStateMachine<NBaseState> _fsm;
2024-09-09 19:22:22 +08:00
2024-09-09 15:30:51 +08:00
public UnitBuild(uint id,
int troopId) : base(id, troopId, EGameUnitType.Building)
{
2024-09-10 12:32:44 +08:00
_fsm = new NStateMachine<NBaseState>();
2024-09-09 15:30:51 +08:00
}
2024-09-09 19:22:22 +08:00
public void InitConfig(BuildConfigData setConfig)
{
configData = setConfig;
buildType = configData.buildType;
2024-09-10 12:32:44 +08:00
debugShowInfo = new DebugShowInfo();
debugShowInfo.name = configData.rawConfig.Name;
debugShowInfo.configId = configData.rawConfig.Id + "";
debugShowInfo.cacheConfigId = configData.rawConfig.Id;
2024-09-09 19:22:22 +08:00
statusData.canBeAttack = configData.rawConfig.Hp > 0;
if (statusData.canBeAttack)
{
aliveData.maxHp = configData.rawConfig.Hp;
aliveData.currentHp = aliveData.maxHp;
}
commonData.standLevel = configData.rawConfig.StandCrossLevel;
2024-09-10 12:32:44 +08:00
commonData.level = configData.rawConfig.FightLevel;
fightData.baseAttack = configData.rawConfig.FightAttack;
fightData.attack = Mathf.FloorToInt(fightData.baseAttack);
fightData.baseDefense = configData.rawConfig.FightDefense;
fightData.defense = Mathf.FloorToInt(fightData.baseDefense);
_AfterInitConfig();
}
protected virtual void _AfterInitConfig()
{
2024-09-09 19:22:22 +08:00
}
public async UniTask CreateAsync()
{
var modelPath = configData.rawConfig.ModelPath;
GameUnitManager.instance.RecordLoadPath(modelPath);
var sampleObj = await AssetManager.Instance.LoadAssetAsync<GameObject>(modelPath);
if (sampleObj == null)
{
DebugUtil.LogError("加载建筑失败: {0}", 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);
// 设置后处理
var obj = Node.GameObject;
var isEnemy = commonData.troopId != ETroopsId.Self;
OutlineManager.instance.AutoAddFromObj(obj, isEnemy);
2024-09-10 12:32:44 +08:00
2024-09-09 19:22:22 +08:00
PerformanceManager.instance.HandleCreateUnit(obj);
2024-09-10 12:32:44 +08:00
_AfterCreateView();
_InitFsm();
2024-09-10 12:42:32 +08:00
_fsm.ChangeState(EBuildState.Idle);
2024-09-10 12:32:44 +08:00
}
protected virtual void _AfterCreateView()
{
}
protected virtual void _InitFsm()
{
2024-09-09 19:22:22 +08:00
}
protected override void _OnEnterBattle()
{
base._OnEnterBattle();
isReady = true;
BuffManager.instance.InitBuild(this);
buffManager.TriggerEnterBattle();
if (configData.rawConfig.BlockAttack)
{
var blockData = AreaManager.instance.attackAreaManager.CreateBlockAttackCell(transData.cellIndex);
blockData.canBeAttack = statusData.canBeAttack;
}
}
protected override void _OnExitBattle()
{
base._OnExitBattle();
isReady = false;
buffManager?.TriggerExitBattle();
if (configData.rawConfig.BlockAttack)
{
AreaManager.instance.attackAreaManager.RemoveBlockAttackCell(transData.cellIndex);
}
}
2024-09-10 12:32:44 +08:00
protected override void _OnLogicUpdate(float dt)
{
base._OnLogicUpdate(dt);
_fsm.LogicUpdate(dt);
}
2024-09-09 15:30:51 +08:00
}
}