战斗系统设计之(条件触发篇)
1.战斗里面的数值需要发生改变必须需要满足条件才会触发。这里就涉及到条件触发系统。
a.通用数值条件判断
using System;
public enum RangeType
{
Less = 0,//小于
LessOrEqual = 1,//小于等于
Equal = 2,//等于
Range = 3,//在范围内
Abs = 4, //绝对值
GreaterOrEqual = 5,//大于等于
Greater = 6,//大于
}
public class CommonRanger
{
public float Value;
public float Min;
public float Max;
public RangeType RangeType;
public CommonRanger(RangeType pRangeType, float pValue, float pMinValue = 0, float pMaxValue = 0)
{
RangeType = pRangeType;
Value = pValue;
Min = pMinValue;
Max = pMaxValue;
}
public bool InRange(float pTarget)
{
if (RangeType == RangeType.Less)
{
return pTarget < Value;
}
else if (RangeType == RangeType.LessOrEqual)
{
return pTarget <= Value;
}
else if (RangeType == RangeType.Equal)
{
return pTarget == Value;
}
else if (RangeType == RangeType.Range)
{
return pTarget >= Min && pTarget <= Max;
}
else if (RangeType == RangeType.GreaterOrEqual)
{
return pTarget >= Value;
}
else if (RangeType == RangeType.Abs)
{
var abs = Math.Abs(pTarget - Value);
return abs <= Max && abs >= Min;
}
else if (RangeType == RangeType.Greater)
{
return pTarget > Value;
}
return false;
}
}
b.基础条件类:
public class VConditioner
{
protected bool isPass = false;
public ConditionerType type = ConditionerType.None;
public int BodyId;
public System.Action<float> passCB;
protected CommonRanger ranger;
protected SVType vType;
private bool _isLoad = false;
protected SVBase selfVBase_;
public VConditioner(){ }
public VConditioner(int pBodyId)
{
BodyId = pBodyId;
}
public void SetRanger(CommonRanger pRanger)
{
ranger = pRanger;
}
public void SetPassCB(System.Action<float> pPassCB)
{
passCB = pPassCB;
}
public void Load(){
if (_isLoad)
return;
_isLoad = true;
OnLoad();
}
public void UnLoad(){OnUnload();}
protected virtual void OnLoad(){
selfVBase_ = Word.Instance.GetValue(BodyId, vType);
}
public virtual void OnCheckPass(float pTime)
{
if (selfVBase_ != null)
{
var _value = selfVBase_.GetValue(pTime);
isPass = ranger.InRange(_value);
}
}
protected virtual void OnUnload(){ }
protected void OnPass(float pTime){
if (isPass)
{
passCB.Invoke(pTime);
}
}
public bool IsPass() { return isPass; }
}
c. 举例扩展cd 条件
public class VCDConditioner : VConditioner
{
private float cd_;
private float nextRTime;
public VCDConditioner(int pBodyId,float pCD = 1)
{
BodyId = pBodyId;
cd_ = pCD;
}
public override void OnCheckPass(float pTime)
{
if (pTime >= nextRTime)
{
isPass = true;
nextRTime = pTime + cd_;
}
else
{
isPass = false;
}
}
}
d.举例扩展死亡条件
public class VDeadConditioner : VConditioner
{
public VDeadConditioner(int pBodyId)
{
BodyId = pBodyId;
SetRanger(new CommonRanger(RangeType.Less, 1));
vType = SVType.Hp;
}
}
2.触发条件
a.基础触发类:
using System.Collections.Generic;
public class VTrigger
{
private Dictionary<ConditionerType, List<VConditioner>> map = new Dictionary<ConditionerType, List<VConditioner>>();
private List<VConditioner> list = new List<VConditioner>();
private bool isTrigger = false;
public bool Enable = false;
private System.Action<float> triggerAc;
public int BodyId;
public VTrigger() { }
public VTrigger(int pBodyId)
{
BodyId = pBodyId;
}
public void SetTriggerAction(System.Action<float> pAc)
{
triggerAc = pAc;
}
public void Load()
{
OnLoad();
foreach (var item in list)
{
item.SetPassCB(OnOnePass);
}
foreach (var item in list)
{
item.Load();
}
}
protected void AddVconditioner(VConditioner pVConditioner)
{
if (map.ContainsKey(pVConditioner.type))
{
map[pVConditioner.type].Add(pVConditioner);
}
else
{
map.Add(pVConditioner.type ,new List<VConditioner>() { pVConditioner });
}
list.Add(pVConditioner);
}
protected virtual void OnLoad(){}
private void OnOnePass(float pTime)
{
CheckAllPass(pTime);
}
private void CheckAllPass(float pTime)
{
if (IsAllPass(pTime))
{
isTrigger = true;
if (triggerAc != null)
{
triggerAc.Invoke(pTime);
}
}
}
public bool IsAllPass(float pTime)
{
foreach (var temp in list)
{
temp.OnCheckPass(pTime);
var isPass = temp.IsPass();
if (!isPass)
return false;
}
return true;
}
}
b.举例扩展死亡触发器
public class VDeadTrigger : VTrigger
{
public VDeadTrigger(int pBodyId)
{
BodyId = pBodyId;
}
protected override void OnLoad()
{
VDeadConditioner dead = new VDeadConditioner(BodyId);
AddVconditioner(dead);
}
}