【发布时间】:2021-03-27 16:06:19
【问题描述】:
我正在制作一个城市管理游戏,其中的工作人员完全由状态机控制。我有一个“问题”,我发现自己重写了很多 99% 相似的状态。通常,某些状态的唯一区别是它在完成时应该进入哪个状态。
例如,下面的状态用于传递资源,这是所有职业都会做的事情,但我为每个职业写了一个特定的,因为它们都会导致不同的状态完成。
public class BakerDeliverGrainState : BaseWorkerState, IState
{
public void Enter()
{
//Go to storage
SetDestination(storage.position);
}
public void Execute()
{
if (unit.ReachedDestination())
{
//Reached destination, state completed
storage.Store(unit.TakeResource());
unit.stateMachine.ChangeState(new BakerWorkState(unit)); //This is the line that seperates the states. For example a blacksmith will go to "BlacksmithWorkState", and so on.
}
}
public void Exit()
{
}
}
有什么方法可以在构造函数中传递要更改的状态吗?或者其他一些我可以调整我的设计的方式,不需要我重写这么多代码。
【问题讨论】:
标签: c# unity3d logic state-machine