zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Hotfix/Game/System/Battle/PlayerSkillAISystem.cs

374 lines
13 KiB
C#
Raw Normal View History

2021-04-08 20:09:59 +08:00
using Cal;
using Cal.DataTable;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ET
{
2021-05-01 11:27:41 +08:00
public class PlayerSkillAIAwakeSystem: AwakeSystem<SkillAI>
2021-04-08 20:09:59 +08:00
{
public override void Awake(SkillAI self)
{
}
}
2021-05-01 11:27:41 +08:00
public class SkillAIDestroySystem: DestroySystem<SkillAI>
2021-04-08 20:09:59 +08:00
{
public override void Destroy(SkillAI self)
{
self.CurrSkillNode = null;
self.AutoSkillList.Clear();
self.canSkill = false;
}
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
public static class PlayerSkillAISystem
{
public static bool CheckCD(this SkillAI self, long now)
{
2021-05-01 11:27:41 +08:00
if (self == null)
2021-04-08 20:09:59 +08:00
{
Log.Error($"skillAI = null");
return false;
}
2021-05-01 11:27:41 +08:00
2021-05-16 17:22:42 +08:00
if (!(now - self.lastSkillTime >= self.roundCD))
2021-04-08 20:09:59 +08:00
{
2021-05-16 17:22:42 +08:00
return false;
2021-04-08 20:09:59 +08:00
}
2021-05-16 17:22:42 +08:00
self.lastSkillTime = now;
return true;
2021-04-08 20:09:59 +08:00
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
public static void PlayAutoSkill(this SkillAI self, long now)
{
if (!self.canSkill) return;
self.canSkill = false;
2021-04-11 19:50:39 +08:00
LinkedList<int> skills = self.AutoSkillList;
2021-04-08 20:09:59 +08:00
if (skills.Count == 0) return;
PlaySkill(self, skills, now);
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
private static void PlaySkill(this SkillAI self, LinkedList<int> list, long now)
{
try
{
Unit unit = self.GetParent<Unit>();
int skillCount = list.Count;
for (int i = 0; i < skillCount; i++)
{
2021-04-11 19:50:39 +08:00
int skillId = self.GetCurrSkillId(list);
2021-04-08 20:09:59 +08:00
if (skillId == 0) continue;
2021-05-01 11:27:41 +08:00
string ret = self.PlaySkill(skillId, now, true);
2021-04-08 20:09:59 +08:00
if (ret == null)
{
2021-04-11 19:50:39 +08:00
SkillMgrComponent skillMgr = unit.GetComponent<SkillMgrComponent>();
2021-04-08 20:09:59 +08:00
SkillLogic skillLogic = skillMgr.GetSkill(skillId);
if (skillLogic == null)
{
Log.Error($"skillLogic == null where skillid = {skillId}");
return;
}
2021-05-01 11:27:41 +08:00
SetMainUICD(unit, skillId, skillLogic.skillConfig.CD);
2021-04-08 20:09:59 +08:00
return;
}
}
}
catch (Exception e)
{
Log.Error(e);
}
}
2021-05-01 11:27:41 +08:00
private static void SetMainUICD(Unit unit, int id, int skillCD)
2021-04-08 20:09:59 +08:00
{
UserSetting userSetting = unit.GetComponent<UserSetting>();
2021-05-01 11:27:41 +08:00
userSetting.StartCD(MainUIType.SkillSlot, id, skillCD);
2021-04-08 20:09:59 +08:00
}
2021-05-01 11:27:41 +08:00
public static string PlaySkill(this SkillAI self, int skillId, long now, bool isAuto = false)
2021-04-08 20:09:59 +08:00
{
try
{
Unit unit = self.GetParent<Unit>();
if (skillId == 0)
{
Log.Error($"skillId == 0");
return "系统错误";
}
2021-05-01 11:27:41 +08:00
2021-04-11 19:50:39 +08:00
UnitSkill skill = unit.GetComponent<UnitSkillComponent>().GetLearnedSkill(skillId);
2021-04-08 20:09:59 +08:00
if (skill == UnitSkill.Null)
{
2021-05-01 11:27:41 +08:00
Log.Error($"【{UserComponent.Instance.Get(unit.Id)?.NickName} ({unit.Id})】使用了没学会的技能,技能id={skillId}");
2021-04-08 20:09:59 +08:00
return "系统错误";
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
int level = skill.Level;
//!+暂时
2021-05-01 11:27:41 +08:00
var skillLogic = GetSkillLogic(unit, skillId, level);
2021-04-08 20:09:59 +08:00
if (skillLogic == null)
{
2021-05-01 11:27:41 +08:00
Log.Error($"{unit.Id.GetPlayerFormatName()} has not the skill where skillId = {skillId} level = {level}");
return "系统错误,没有技能";
2021-04-08 20:09:59 +08:00
}
//!判断状态
if (CheckUnActionState(unit))
{
return "禁足";
}
2021-05-01 11:27:41 +08:00
return CheckSkillConditionAndSpell(now, unit, skillLogic, isAuto);
2021-04-08 20:09:59 +08:00
}
catch (Exception e)
{
Log.Error(e);
return "系统错误";
}
2021-05-01 11:27:41 +08:00
}
private static SkillLogic GetSkillLogic(Unit unit, int skillId, int level)
{
SkillMgrComponent skillMgr = unit.GetComponent<SkillMgrComponent>();
SkillLogic skillLogic = skillMgr.GetSkill(skillId);
if (skillLogic == null)
{
Log.Error($"skillLogic == null where skillid = {skillId}");
return null;
}
2021-04-08 20:09:59 +08:00
2021-05-01 11:27:41 +08:00
skillLogic.skillLevel = level;
return skillLogic;
2021-04-08 20:09:59 +08:00
}
2021-05-01 11:27:41 +08:00
private static string CheckSkillConditionAndSpell(long now, Unit unit, SkillLogic skillLogic, bool isAuto)
2021-04-08 20:09:59 +08:00
{
SkillLogicConfig skillConfig = skillLogic.skillLogicConfig;
if (now - skillLogic.lastCDTime < skillConfig.CD)
{
return "冷却中!";
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
string canCast = null;
SkillCastType skillCastType = skillConfig.cast.skillCastType;
switch (skillCastType)
{
case SkillCastType.:
break;
case SkillCastType.:
if (!CastMp(unit, skillConfig.cast))
canCast = "精力不足!";
break;
case SkillCastType.:
if (!CastHp(unit, skillConfig.cast))
canCast = "血量不足!";
break;
default:
break;
}
2021-05-01 11:27:41 +08:00
if (isAuto)
{
if (canCast != null)
{
skillLogic = GetSkillLogic(unit, SkillHotfixHelper.GetNomalSkillId(unit), 1);
if (skillLogic == null)
{
Log.Error($"{unit.Id.GetPlayerFormatName()} has not the skill where skillId = {SkillHotfixHelper.GetNomalSkillId(unit)}");
}
skillConfig = skillLogic.skillLogicConfig;
if (now - skillLogic.lastCDTime < skillConfig.CD)
{
return "冷却中!";
}
}
}
else
{
if (canCast != null) return canCast;
}
//!选择目标
TargetableUnitComponent targetComponent = unit.GetComponent<TargetableUnitComponent>();
if (!targetComponent)
{
Log.Error($"targetComponent == null where id = {unit.Id}");
return "系统错误";
}
targetComponent.SelectTarget(skillLogic.skillLogicConfig);
2021-04-08 20:09:59 +08:00
long time = skillLogic.lastCDTime;
skillLogic.lastCDTime = now;
//!执行技能逻辑
2021-04-11 19:50:39 +08:00
bool ret = unit.GetComponent<AttackComponent>().StartSpellSkill(skillLogic);
2021-04-08 20:09:59 +08:00
if (!ret)
{
skillLogic.lastCDTime = time;
return "特别原因";
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
return null;
}
private static bool CastHp(Unit unit, CastParam cast)
{
2021-04-11 19:50:39 +08:00
NumericComponent num = unit.GetComponent<NumericComponent>();
2021-04-08 20:09:59 +08:00
float hp = num.Get(NumericType.Hp);
float value = 0;
switch (cast.castBaseType)
{
case CastBaseType.Level:
float level = num.Get(NumericType.Level);
value = level * cast.skillCast;
break;
case CastBaseType.Curr:
float currHp = num.Get(NumericType.Hp);
value = currHp * cast.skillCast;
break;
case CastBaseType.Max:
float maxHp = num.Get(NumericType.MaxHp);
value = maxHp * cast.skillCast;
break;
case CastBaseType.Other:
break;
default:
break;
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
if (hp < value)
return false;
num.ReduceSet(NumericType.Hp, value);
return true;
}
private static bool CastMp(Unit unit, CastParam cast)
{
2021-04-11 19:50:39 +08:00
NumericComponent num = unit.GetComponent<NumericComponent>();
2021-04-08 20:09:59 +08:00
float mp = num.Get(NumericType.Mp);
float value = 0;
switch (cast.castBaseType)
{
case CastBaseType.Level:
float level = num.Get(NumericType.Level);
value = level * cast.skillCast;
break;
case CastBaseType.Curr:
float currHp = num.Get(NumericType.Mp);
value = currHp * cast.skillCast;
break;
case CastBaseType.Max:
float maxHp = num.Get(NumericType.MaxMp);
value = maxHp * cast.skillCast;
break;
case CastBaseType.Other:
break;
default:
break;
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
if (mp < value)
return false;
num.ReduceSet(NumericType.Mp, value);
return true;
}
/// <summary>
/// 怪物释放逻辑
/// </summary>
/// <param name="self"></param>
public static void PlaySkill(this SkillAI self)
{
try
{
Unit unit = self.GetParent<Unit>();
if (!unit) return;
2021-04-11 19:50:39 +08:00
LinkedList<int> list = self.AutoSkillList;
int skillId = self.GetCurrSkillId(list);
2021-04-08 20:09:59 +08:00
if (skillId == 0) return;
2021-04-11 19:50:39 +08:00
UnitSkillComponent skillConponent = unit.GetComponent<UnitSkillComponent>();
UnitSkill skill = skillConponent.GetLearnedSkill(skillId);
2021-04-08 20:09:59 +08:00
if (skill == UnitSkill.Null)
{
Log.Error($"skill == null where skillid = {skillId}");
return;
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
int level = skill.Level;
2021-04-11 19:50:39 +08:00
AttackComponent attacker = unit.GetComponent<AttackComponent>();
SkillMgrComponent skillMgr = unit.GetComponent<SkillMgrComponent>();
2021-04-08 20:09:59 +08:00
SkillLogic skillLogic = skillMgr.GetSkill(skillId);
if (skillLogic == null)
{
Log.Error($"skillLogic == null where skillid = {skillId}");
return;
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
skillLogic.skillLevel = level;
//!判断状态
if (CheckUnActionState(unit))
{
return;
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
//!选择目标
2021-04-11 19:50:39 +08:00
TargetableUnitComponent targetComponent = unit.GetComponent<TargetableUnitComponent>();
2021-04-08 20:09:59 +08:00
if (!targetComponent)
Log.Error($"targetComponent == null where id = {unit.Id}");
else
targetComponent.SelectTarget(skillLogic.skillLogicConfig);
attacker.StartSpellSkill(skillLogic);
}
catch (Exception e)
{
Log.Error(e);
}
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
public static void UpdateAutoSkill(this SkillAI self)
{
2021-04-11 19:50:39 +08:00
LinkedList<int> list = self.Parent.GetComponent<UserSetting>().GetAutoSkills();
2021-04-08 20:09:59 +08:00
self.AutoSkillList.Clear();
2021-04-11 19:50:39 +08:00
foreach (int item in list)
2021-04-08 20:09:59 +08:00
{
self.AutoSkillList.AddLast(item);
}
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
private static bool CheckUnActionState(Unit unit)
{
2021-04-11 19:50:39 +08:00
ModifierContainerComponent modifierContainer = unit.GetComponent<ModifierContainerComponent>();
2021-04-08 20:09:59 +08:00
return Check(modifierContainer, ModifierStateType.) ||
2021-05-01 11:27:41 +08:00
Check(modifierContainer, ModifierStateType.) ||
Check(modifierContainer, ModifierStateType.) ||
Check(modifierContainer, ModifierStateType.);
2021-04-08 20:09:59 +08:00
static bool Check(ModifierContainerComponent modifierContainer, ModifierStateType modifierStateType) =>
2021-05-01 11:27:41 +08:00
modifierContainer.HasState(modifierStateType);
2021-04-08 20:09:59 +08:00
}
private static int GetCurrSkillId(this SkillAI self, LinkedList<int> list)
{
try
{
2021-04-11 19:50:39 +08:00
LinkedListNode<int> curr = self.CurrSkillNode ?? list.First;
2021-04-08 20:09:59 +08:00
if (curr == list.Last)
self.CurrSkillNode = null;
else
self.CurrSkillNode = curr.Next;
return curr.Value;
}
catch (Exception e)
{
Log.Error(e);
}
2021-05-01 11:27:41 +08:00
2021-04-08 20:09:59 +08:00
return 0;
}
}
2021-05-01 11:27:41 +08:00
}