Files
JJBB/Assets/Project/Script/Player/AI/Threat.cs
2024-08-23 15:49:34 +08:00

100 lines
2.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************************************************************
* 文件名: ThreadList.cs
* 全路径: \Script\Player\AI\ThreadList.cs
* 创建人: 李嘉
* 创建时间2013-11-19
*
* 功能说明: Obj仇恨列表
*
* 修改记录:
*********************************************************************************/
using System;
using System.Collections.Generic;
using Games.LogicObj;
namespace Games.AI_Logic
{
public class ThreatInfo
{
public ThreatInfo(Obj_Character obj, int nValue)
{
ThreatObj = obj;
ThreatValue = nValue;
}
// 威胁来源
public Obj_Character ThreatObj { get; set; }
// 威胁值
public int ThreatValue { get; set; }
}
public class Threat
{
private readonly List<ThreatInfo> m_ThreatList;
public Threat()
{
if (null == m_ThreatList) m_ThreatList = new List<ThreatInfo>();
}
public int GetThreatValue(Obj_Character obj)
{
var threatInfo = FindThreadInfo(obj);
return threatInfo == null ? 0 : threatInfo.ThreatValue;
}
public ThreatInfo FindThreadInfo(Obj_Character obj)
{
ThreatInfo result = null;
if (obj != null)
result = m_ThreatList.Find(a => a.ThreatObj == obj);
return result;
}
public Obj_Character FindMaxThreatObj()
{
ThreatInfo maxThreat = null;
for (var i = 0; i < m_ThreatList.Count; ++i)
if (null == maxThreat || maxThreat.ThreatValue < m_ThreatList[i].ThreatValue)
maxThreat = m_ThreatList[i];
return maxThreat == null ? null : maxThreat.ThreatObj;
}
public void AddThreat(Obj_Character obj, int value)
{
if (null == obj)
return;
var info = FindThreadInfo(obj);
if (null == info)
{
// 负数威胁值不保存,因此不创建
if (value > 0)
{
info = new ThreatInfo(obj, value);
m_ThreatList.Add(info);
}
}
else
{
info.ThreatValue = Math.Max(0, info.ThreatValue + value);
}
}
public void ResetAllThreat()
{
m_ThreatList.Clear();
}
public void ResetThreat(Obj_Character obj)
{
if (obj != null)
{
var index = m_ThreatList.FindIndex(a => a.ThreatObj == obj);
if (index >= 0)
m_ThreatList.RemoveAt(index);
}
}
}
}