我正在制作一款基于当前Task
的工人执行动作的游戏。每个工人将被分配一个任务列表,在一个首选的顺序(这是由玩家的决定影响)。
当一个任务完成时(例如从X到Y),工作者需要通过检查他们的可能任务列表来启动一个新任务,看看每个任务是否可以执行,如果可以,将他们的当前任务设置为该任务并启动它(最后一个任务-"徘徊"总是可用的)。
我目前使用一个大的开关语句和枚举来工作,但现在想要概括这段代码来创建一个Task
类,并给工人一个首选任务列表,一个GetNextTask()
函数,并在工人的Update()
方法中,调用currentTask.update()
(这将使工人在当前任务下做任何他需要做的事情,当任务完成时调用worker.GetNextTask()
)。
我不清楚的是在工作中存储任务的最佳方式。我应该使用:
1)反射。将可能的任务存储为类型列表,然后使用反射来a)调用静态方法public static virtual bool CanPerformThisTask()
,该方法在每个子类中被覆盖,b)为工人创建该任务的实例?(下面的示例代码尝试-但无法测试)
2)实例化所有的任务,每当一个工人需要获得一个新的任务(可能使用Activator),并检查(Task)task.CanPerformThisTask()
为每一个-如果为真,做那个任务。实例化它们似乎效率低下?
3)泛型。这可以用泛型实现吗?如果有,怎么做?
下面是我的类的一个片段,可以让你知道我要做什么:
工人阶级:
protected List<Point> waypoints = new List<Point>();
public bool reachedDestination { get { return waypoints.Count == 0; } }
protected Task task;
public List<Type> possibleTasks;
public Worker(Task initialTask, List<Type> initialPossibleTasks ...)
: base(...)
{
task = initialTask;
possibleTasks = initialPossibleTasks;
}
public override void Update()
{
base.Update();
if (!reachedDestination) Move();
task.Update();
}
public void GetNextTask()
{
foreach (Type t in possibleTasks)
{
//reflection code here - will this work and can we do this with generics instead?
Bool canDoT = (bool)t.GetMethod("CanPerformThisTask", BindingFlags.Static | BindingFlags.Public).Invoke(null, null);
if (canDoT)
{
task = Activator.CreateInstance(t);
return;
}
}
}
下面是我的基本任务类的一些不完整的代码(不应该被实例化):
public class Task
{
public Worker worker;
public virtual static bool CanPerformThisTask()
{
//never call this from here - always from subclasses
return false;
}
public Task()
{
//set up code here
}
public virtual void Update()
{
//make worker do relevant activities here
//call finish task when done
}
public void FinishTask()
{
worker.GetNextTask();
}
}
,下面是一个任务的例子,工作器将在其可能的任务列表中包含:
public class T_WorkerWander : Task
{
public static override bool CanPerformThisTask()
{
//can always wander (other Tasks will have conditions here)
return true;
}
public T_WorkerWander()
: base()
{
}
override public void Update()
{
//make the worker wander here
if (worker.reachedDestination) FinishTask();
}
}
更新:这是我现在已经工作的代码
任务类:
public abstract class Task
{
//the entity holding this task
public TaskableEntity taskEntity;
public List<TaskStage> taskStages;
public TaskStage currentTaskStage { get { return taskStages[0]; } }
public Task(TaskableEntity t) { taskEntity = t; }
/// <summary>
/// the conditions for the Task to be started
/// </summary>
public virtual bool CanStart()
{
return true;
}
public void Start()
{
taskStages = new List<TaskStage>();
InitialiseTaskStages();
taskStages[0].Start();
}
public abstract void InitialiseTaskStages();
public void Update()
{
currentTaskStage.Update();
if (currentTaskStage.IsComplete()) TaskStageComplete();
}
public void TaskStageComplete()
{
taskStages.RemoveAt(0);
if (taskStages.Count == 0) taskEntity.TaskComplete();
else currentTaskStage.Start();
}
public void SetTaskStages(params TaskStage[] t)
{
taskStages = t.ToList();
}
public void Interrupt()
{
currentTaskStage.Interrupt();
}
}
TaskStage类:
public sealed class TaskStage
{
private Task task;
private List<Point> pointsToMoveTo;
public void SetPointsToMoveTo(Point p) { pointsToMoveTo = new List<Point>() { p }; }
public void SetPointsToMoveTo(params Point[] p) { pointsToMoveTo = p.ToList(); }
public void SetPointsToMoveTo(List<Point> p) { pointsToMoveTo = p; }
public Action actionToApply;
private float timeToWait;
public void SetWait(float wait) { timeToWait = wait; }
private IReservable[] itemsToReserve;
public void SetItemsToReserve(params IReservable[] items) { itemsToReserve = items; }
private IReservable[] itemsToUnreserve;
public void SetItemsToUnreserve(params IReservable[] items) { itemsToUnreserve = items; }
private Emotion emotionToSet;
public void SetEmotionToSet(Emotion e) { emotionToSet = e; }
private TaskStage _interrupt;
public void SetInterruptAction(TaskStage t) { _interrupt = t; }
public void Interrupt() { _interrupt.Start(); }
public TaskStage(Task t)
{
task = t;
}
public void Start()
{
if (actionToApply != null) actionToApply();
if (itemsToUnreserve != null) UnreserveItems();
if (itemsToReserve != null) ReserveItems();
if (pointsToMoveTo != null)
{
//this will need changing after pathfinding sorted out...
if (pointsToMoveTo.Count == 1) task.taskEntity.SetWaypoints(pointsToMoveTo[0]);
else task.taskEntity.waypoints = pointsToMoveTo;
}
if (emotionToSet != null) emotionToSet.StartEmotion();
}
public void Update()
{
if (timeToWait > 0) timeToWait -= GV.elapsedTime;
}
public bool IsComplete()
{
if (pointsToMoveTo != null && !task.taskEntity.reachedDestination) return false;
if (timeToWait > 0) return false;
return true;
}
public void ReserveItems()
{
foreach (IReservable i in itemsToReserve)
{
i.reserved = true;
}
}
public void UnreserveItems()
{
foreach (IReservable i in itemsToUnreserve)
{
i.reserved = false;
}
}
}
示例任务:
public class T_WorkerGoToBed : Task
{
public FactoryWorker worker { get { return taskEntity as FactoryWorker; } }
public T_WorkerGoToBed(TaskableEntity t)
: base(t) { }
public override bool CanStart()
{
return Room.Available<Bed>(GV.Bedrooms);
}
public override void InitialiseTaskStages()
{
Bed bedToSleepIn = Room.NearestAvailableFurniture<Bed>(GV.Bedrooms, taskEntity.X, taskEntity.Y);
//stage 1 - reserve bed and move there
TaskStage ts1 = new TaskStage(this);
ts1.SetItemsToReserve(bedToSleepIn);
ts1.SetPointsToMoveTo(bedToSleepIn.XY);
//stage 2 - sleep in bed
TaskStage ts2 = new TaskStage(this);
ts2.SetWait((worker.maxEnergy - worker.energy) / worker.energyRegeneratedPerSecondWhenSleeping);
ts2.SetEmotionToSet(new E_Sleeping(worker, false));
//stage 3 - unreserve bed
TaskStage ts3 = new TaskStage(this);
ts3.SetItemsToUnreserve(bedToSleepIn);
ts3.SetEmotionToSet(new E_Happy(worker, false));
SetTaskStages(ts1, ts2, ts3);
}
}
听起来你需要在任务和工人之间颠倒责任。不要问是否可以完成任务,而要问工人是否可以完成给定的任务:
class Worker
{
bool CanPerformTask<T>() where T : Task
{
var type = typeof(T);
// code to determine whether worker can perform the task T
}
// alternative with instance parameter
bool CanPerformTask<T>( T task ) where T : Task
{
// code to determine whether worker can perform the task passed in
}
}
此解决方案避免了"实例化所有任务或调用静态方法"的问题。
另外,考虑使用内置集合类。诸如队列和堆栈之类的东西可以极大地简化调度执行所需的代码。
我认为你滥用了静态类的意义。"Task"类应该是标准的(不是静态的)。您的"Worker"类不是静态的,因此意味着有多个"Worker"实例。在此范例下,这些工人可能会被分配相同的任务。
你的"Worker"类需要修改以下属性:
public List;
public List;
您可能也不应该对该属性具有公共访问权限。您可以根据需要修改"CanPerformThisTask"