以任务流程为主线的仿真开发中,需要监听任务之间的关系,一个任务往往由多个实现条件组成,条件既可以是一个更小的任务也可以是一个判断点。
任务基类:
public abstract class TaskComponent
{
protected string mName;
public string Name { get { return mName; } }
protected List<TaskComponent> mChildren;
protected bool isdone = false;
public bool IsDone
{
get
{
if (mChildren!=null&&mChildren.Count!=0)
{
foreach (TaskComponent tc in mChildren)
{
if (tc.IsDone == false)
{
return false;
}
}
return true;
}
else
{
return isdone;
}
}
}
public List<TaskComponent> Children { get { return mChildren; } }
public abstract void AddChild(TaskComponent tc);
public abstract void RemoveChild(TaskComponent tc);
public abstract TaskComponent GetChild(int index);
public virtual void SetLocalDone(bool isDone)
{
if (mChildren!=null||mChildren.Count!=0)
{
this.isdone = isDone;
}
}
public TaskComponent(string name)
{
mName = name;
mChildren = new List<TaskComponent>();
}
}
基础任务类:
public class TaskBase : TaskComponent
{
public TaskBase(string name) : base(name) { }
public override void AddChild(TaskComponent tc)
{
mChildren.Add(tc);
}
public override TaskComponent GetChild(int index)
{
return mChildren[index];
}
public override void RemoveChild(TaskComponent tc)
{
mChildren.Remove(tc);
}
}
任务条件类:
public class TaskConditionBase : TaskComponent
{
public TaskConditionBase(string name) : base(name) { }
public override void AddChild(TaskComponent tc)
{
return;
}
public override TaskComponent GetChild(int index)
{
return null;
}
public override void RemoveChild(TaskComponent tc)
{
return;
}
}
基础任务类与任务条件类的区别在于是否存在子条件类,创建任务管理类,任务管理类负责创建任务、任务条件以及任务与任务条件之间的约束关系。最终通过GetTaskState()函数可获取任意一个任务或者任务条件的状态,此方法适用于服务端对任务的监听,也便于将任务完成流程图形化。
public class TaskManager : MonoBehaviour
{
TaskBase mieHuoTask = new TaskBase("灭火");
TaskConditionBase m_condition1 = new TaskConditionBase("关闭通风");
TaskConditionBase m_condition2 = new TaskConditionBase("打开灭火器");
TaskConditionBase m_condition3 = new TaskConditionBase("持续足够的时间");
TaskBase tongFengTask = new TaskBase("通风");
TaskConditionBase t_condition1 = new TaskConditionBase("打开通风口1");
TaskConditionBase t_condition2 = new TaskConditionBase("打开通风口2");
TaskConditionBase t_condition3 = new TaskConditionBase("打开通风口3");
TaskBase totalTask = new TaskBase("灭火任务");
// Start is called before the first frame update
void Start()
{
mieHuoTask.AddChild(m_condition1);
mieHuoTask.AddChild(m_condition2);
mieHuoTask.AddChild(m_condition3);
tongFengTask.AddChild(t_condition1);
tongFengTask.AddChild(t_condition2);
tongFengTask.AddChild(t_condition3);
totalTask.AddChild(mieHuoTask);
totalTask.AddChild(tongFengTask);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
t_condition1.SetLocalDone(true);
t_condition2.SetLocalDone(true);
t_condition3.SetLocalDone(true);
}
if (Input.GetKeyDown(KeyCode.D))
{
m_condition1.SetLocalDone(true);
m_condition2.SetLocalDone(true);
m_condition3.SetLocalDone(true);
}
if (Input.GetKeyDown(KeyCode.S))
{
t_condition3.SetLocalDone(false);
}
if (Input.GetKeyDown(KeyCode.G))
{
Debug.Log("getstate灭火状态:" + GetTaskState(totalTask)+" isdone:"+totalTask.IsDone);
}
}
public bool GetTaskState(TaskComponent task)
{
List<TaskComponent> taskChildren = task.Children;
if (taskChildren==null||taskChildren.Count==0)//已经到任务条件的层级
{
return task.IsDone;
}
else
{
foreach (TaskComponent child in taskChildren)
{
bool isCurrentDone=GetTaskState(child);
if (isCurrentDone==false)
{
Debug.Log("状态【"+child.Name+"】没有通过");
return false;
}
}
Debug.Log("状态【" + task.Name + "】没有通过");
return true;
}
}
}