80 lines
2.6 KiB
C#
80 lines
2.6 KiB
C#
using System.Collections.Generic;
|
|
using PhxhSDK;
|
|
using Gameplay.AI;
|
|
using Gameplay.BT.Variables;
|
|
using UnityEngine;
|
|
using Gameplay.Unit;
|
|
using Gameplay.Character;
|
|
using Gameplay.Level;
|
|
|
|
/// <summary>
|
|
/// 自动战斗
|
|
/// </summary>
|
|
public class PVEManager : Singlenton<PVEManager>
|
|
{
|
|
private const string SharedVariableName = "battleInfo";
|
|
private const string AIBehavior = "AIBehavior";
|
|
|
|
public bool AutoFight = false;
|
|
|
|
/// <summary>
|
|
/// 己方添加AI
|
|
/// </summary>
|
|
public async void LoadAIToSelf(GameUnit unit, Vector2Int position)
|
|
{
|
|
if (unit is not Character { troopId: ETroopsId.Self } character) return;
|
|
|
|
//创建角色 LevelData 数据
|
|
character.unitLevelData = new UnitLevelData()
|
|
{
|
|
AttachAIName = LevelData.AttachAIType.None,
|
|
AIName = LevelData.AIType.BasicAI,
|
|
BornPosition = position,
|
|
PatrolInfos = new List<LevelData.PatrolInfo>(),
|
|
GroupInfos = new List<int>(),
|
|
};
|
|
|
|
character.statusData.isAttackActive = true;
|
|
var bt = await AIManager.Instance.RegisterAI(character.Node.GameObject, AIBehavior, false);
|
|
bt.SetVariable(SharedVariableName, new SharedBattleInfo()
|
|
{
|
|
Value = new BattleInfo()
|
|
{
|
|
ownerId = character.GetID(),
|
|
AIName = character.unitLevelData.AIName,
|
|
AttachAIName = character.unitLevelData.AttachAIName,
|
|
PatrolInfos = character.unitLevelData.PatrolInfos,
|
|
groupInfo = character.unitLevelData.GroupInfos,
|
|
AutoFight = false,
|
|
}
|
|
});
|
|
character.statusData.isAIControll = false;
|
|
DebugUtil.Log("角色:{0} 绑定了AI", character.GetID());
|
|
}
|
|
|
|
public void UnLoadAIToSelf(GameUnit unit)
|
|
{
|
|
if (unit is not Character { troopId: ETroopsId.Self } character) return;
|
|
|
|
character.statusData.isAttackActive = true;
|
|
AIManager.Instance.UnRegisterAI(character.Node.GameObject);
|
|
DebugUtil.Log("角色:{0} 解绑了AI", character.GetID());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 己方是否自动战斗
|
|
/// </summary>
|
|
public void EnableSelfAutoFight(bool enable)
|
|
{
|
|
if (enable == AutoFight) return;
|
|
AutoFight = enable;
|
|
var units = GameUnitManager.instance.infightUnits.unitList;
|
|
foreach (var unit in units)
|
|
{
|
|
if (unit is Character { troopId: ETroopsId.Self } unitCharacter)
|
|
{
|
|
AIManager.Instance.EnableAI(unitCharacter.Node.GameObject, enable);
|
|
}
|
|
}
|
|
}
|
|
} |