游戲設計中的狀態轉換是一個既是非常重要又十分復雜的部分。我前幾天做移植的時候看到一個public void keyPressed(int keyCode)中間竟然有著近2000千行的代碼,這樣的話冗長的代碼不僅不符合OO的思更加嚴重的是會打擾我們的程序思路。這兩天運用State模式做了一個J2ME游戲中的狀態轉換由於只是一個實驗的例子所以只做了開始菜單和幫助的轉換,在這個框架下再加入其他的狀態實際就是十分的簡單的了。
現在DEMO中只有兩個狀態,兩種狀態下keyPressed和paint的功能是不同的,我們用狀態模式來實現各種狀態下的keyPressed和paint的功能功能。
首先定義抽象狀態State接口,這個接口指定了keyPressed和paint的行為
public interface State {
public void onKeyPressed(int keyCode,StateManage _current);
public void paint(Graphics g);
}
State接口keyPressed和paint方法,我們實現兩個子類HelpMenuState和MainMenuState,分別表示主菜單狀態和HELP菜單的狀態,並實現具體的onKeyPressed和paint方法:
public class MainMenuState implements State {
/**省略變量的聲明和初使化部分*/
public void onKeyPressed(int keyCode, StateManage _current) {
switch (whichMenu) {
case 0:
mainMenuKeyPressed(keyCode,_current);
break;
case 1:
break;
}
}
private void mainMenuKeyPressed(int keyCode,StateManage _current) {
switch (keyCode) {
case 1: //KEY_UP = 1;
menuIndex = menuIndex <= 0 ? menuMain.length - 1 : --menuIndex;
break;
case 6://KEY_DOWN = 6;
menuIndex = menuIndex >= menuMain.length - 1 ? 0 : ++menuIndex;
break;
case 8://KEY_MIDDLE = 8;
if (menuIndex == 2)
_current.setState(new HelpMenuState());
break;
}
}
public void paint(Graphics g) {
switch (whichMenu) {
case 0:
drawMainMenu(g);
break;
}
}
private void drawMainMenu(Graphics g) {
g.drawImage(imgMenu, 0, 0, 0);
g.setColor(0, 0, 0);
g.setFont(GameInfo.FONT_LARGE);
for (int i = 0; i < menuMain.length; i++)
g.drawString(menuMain[i], 120, 70 + i * 20, Graphics.TOP
| Graphics.HCENTER);
g.drawImage(imgIndex, 60, 65 + 20 * menuIndex, 0); //畫index圖
g.setColor(255, 0, 0);
g.drawString(menuMain[menuIndex], 120, 70 + 20 * menuIndex,
Graphics.TOP | Graphics.HCENTER);
}
}
public class HelpMenuState implements State {
/**省略變量的聲明和初使化部分*/
public void onKeyPressed(int keyCode, StateManage _current) {
switch (whichMenu) {
case 0:
helpMenuKeyPressed(keyCode, _current);
break;
case 1:
break;
}
}
public void helpMenuKeyPressed(int keyCode, StateManage _current) {
switch (keyCode) {
case 1: //KEY_UP = 1;
menuIndex = menuIndex <= 0 ? helpMenuString.length - 1
: --menuIndex;
break;
case 6://KEY_DOWN = 6;
menuIndex = menuIndex >= helpMenuString.length - 1 ? 0
: ++menuIndex;
break;
case 8://KEY_MIDDLE = 8;
if (menuIndex == 4) {
_current.setState(new MainMenuState());
}
break;
}
}
public void paint(Graphics g) {
drawHelp(g);
}
private void drawHelp(Graphics g) {
for (int i = 0; i <= 15; i++) {
for (int j = 0; j <= 10; j++) {
g.drawImage(img, 0 + 24 * i, 0 + 24 * j, 0);
}
}
g.setColor(251, 220, 48);
g.setFont(GameInfo.FONT_LARGE);
g.drawString("<< HELP >>", 120, 40, Graphics.TOP | Graphics.HCENTER);
for (int i = 0; i < helpMenuString.length; i++)
g.drawString(helpMenuString[i], 120, 80 + i * 25, Graphics.TOP
| Graphics.HCENTER);
g.setColor(255, 0, 0);
g.drawString(helpMenuString[menuIndex], 120, 80 + 25 * menuIndex,
Graphics.TOP | Graphics.HCENTER);
}
}
最後,定義StateManage,StateManage的實例作為狀態轉換的控制器:
public class StateManage {
State _current;
public StateManage(){
_current = new MainMenuState(); //以主菜單開始
}