我需要帮助将伪代码转换为Java(状态机示例)

时间:2015-02-04 21:33:31

标签: java state

您好我写这篇文章是因为我试图将下面的伪代码转换为Java格式冷杉2天左右。我试图转换的代码来自这篇惊人的文章:

http://gamedevelopment.tutsplus.com/articles/how-to-build-a-jrpg-a-primer-for-game-developers--gamedev-6676

代码是

class StateMachine

Map<String, IState> mStates = new Map<String, IState>();
IState mCurrentState = EmptyState;

public void Update(float elapsedTime)
{
    mCurrentState.Update(elapsedTime);
}

public void Render()
{
    mCurrentState.Render();
}

public void Change(String stateName, optional var params)
{
    mCurrentState.OnExit();
    mCurrentState = mStates[stateName];
    mCurrentState.OnEnter(params);
}

public void Add(String name, IState state)
{
    mStates[name] = state;
}

和此代码

public class StateStack

Map<String, IState> mStates = new Map<String, IState>();
List<IState> mStack = List<IState>();

public void Update(float elapsedTime)
{
    IState top = mStack.Top()
    top.Update(elapsedTime)
}

public void Render()
{
    IState top = mStack.Top()
    top.Render()
}

public void Push(String name)
{
    IState state = mStates[name];
    mStack.Push(state);
}

public IState Pop()
{
    return mStack.Pop();
}

如果有人给我最终结果或者人们指导我正确的方向,我不介意。我想解决这个XD

额外位

  StateMachine gGameMode = new StateMachine();

// A state for each game mode
gGameMode.Add("mainmenu",   new MainMenuState(gGameMode));
gGameMode.Add("localmap",   new LocalMapState(gGameMode));
gGameMode.Add("worldmap",   new WorldMapState(gGameMode));
gGameMode.Add("battle",     new BattleState(gGameMode));
gGameMode.Add("ingamemenu", new InGameMenuState(gGameMode));

gGameMode.Change("mainmenu");

// Main Game Update Loop
public void Update()
{
    float elapsedTime = GetElapsedFrameTime();
    gGameMode.Update(elapsedTime);
    gGameMode.Render();
}

额外位的结尾

如果你可以指导我,这对我也有帮助:P

- 感谢你

1 个答案:

答案 0 :(得分:0)

这基本上是Java,你只需要将代码放在正确的.java文件中并编写构造函数。

等。

public class StateMachine(){
  public StateMachine(){
        
  }
}

问题是你没有任何MainMenuState ......类的代码