73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
using System.Collections.Generic;
|
|
using Framework;
|
|
using PhxhSDK;
|
|
using Gameplay.AI;
|
|
using Gameplay.BT.Variables;
|
|
using Gameplay.Unit;
|
|
using Gameplay.Character;
|
|
using Gameplay.Level;
|
|
|
|
/// <summary>
|
|
/// 自动战斗
|
|
/// </summary>
|
|
public class PVEManager : Singlenton<PVEManager>
|
|
{
|
|
private const string SharedVariableName = "battleInfo";
|
|
|
|
// 临时AI行为树
|
|
private const string AIBehavior = "AIbaseEnemy";
|
|
|
|
public bool AutoFight = false;
|
|
|
|
/// <summary>
|
|
/// 己方添加AI
|
|
/// </summary>
|
|
public async void LoadAIToSelf(GameUnit unit)
|
|
{
|
|
if (unit is not Character { troopId: ETroopsId.Self } character) return;
|
|
|
|
//创建角色 LevelData 数据
|
|
character.unitLevelData = new UnitLevelData()
|
|
{
|
|
PatrolInfos = new List<LevelData.PatrolInfo>(),
|
|
GroupInfos = new List<int>(),
|
|
};
|
|
|
|
character.statusData.isAttackActive = true;
|
|
var bt = await AIManager.Instance.RegisterAI(character.Node.GameObject, AIBehavior, true);
|
|
bt.SetVariable(SharedVariableName, new SharedBattleInfo()
|
|
{
|
|
Value = new BattleInfo()
|
|
{
|
|
ownerID = character.GetID(),
|
|
patrolInfos = character.unitLevelData.PatrolInfos,
|
|
groupInfo = character.unitLevelData.GroupInfos,
|
|
AutoFight = false,
|
|
cannotReach = new List<int>(),
|
|
}
|
|
});
|
|
character.statusData.isAIControll = false;
|
|
DebugUtil.Log("角色:{0} 绑定AI", character.GetID());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开启或关闭自动战斗
|
|
/// </summary>
|
|
public void EnableSelfAutoFight(bool enable)
|
|
{
|
|
if (enable == AutoFight) return;
|
|
AutoFight = enable;
|
|
foreach (var unit in GameUnitManager.instance.canControlUnits.unitList)
|
|
{
|
|
unit.statusData.isAIControll = enable;
|
|
}
|
|
AIManager.Instance.EnableAllSelfAI(enable);
|
|
}
|
|
|
|
public void StopAI(GameUnit unit)
|
|
{
|
|
if (!AutoFight) return;
|
|
AIManager.Instance.EnableSingleAI(unit.Node.GameObject, false);
|
|
}
|
|
|
|
} |