一款运行于手机的Java游戏神灯传奇源代码
源代码在线查看: control.java
package com.thinkenjoy.control;
//Download by http://www.codefans.net
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotFoundException;
import com.thinkenjoy.feitian.screen.AboutScreen;
import com.thinkenjoy.feitian.screen.HelpScreen;
import com.thinkenjoy.feitian.screen.LoadResourceScreen;
import com.thinkenjoy.feitian.screen.LogoScreen;
import com.thinkenjoy.feitian.screen.MenuScreen;
import com.thinkenjoy.feitian.screen.TitleScreen;
import com.thinkenjoy.feitian.screen.PlayScreen;
import com.thinkenjoy.feitian.screen.PersonScreen;
import com.thinkenjoy.feitian.screen.PassScreen;
import java.util.*;
/**
*
* 控制类,所有显示界面的中枢类
*/
public class Control {
/**
* 当前有效的显示对象
*/
private static ActorCanvas focusLayer = null;
/**
* 进入一些临时界面,如弹出式菜单,帮助,关于等时记录上一个有效显示对象
*/
public static ActorCanvas oldFocusLayer = null;
public static final byte LOGO_STAGE = 0;
public static final byte LOAD_RESOURCE_STAGE = 1;
public static final byte TITLE_STAGE = 2;
public static final byte ABOUT_STAGE = 6;
public static final byte HELP_STAGE = 7;
public static final byte MENU_STAGE = 8;
public static final byte PLAY_STAGE = 9;
public static final byte PLAYER_SELECT = 10 ;
public static final byte GAME_START = 11;
public static final byte PASS_STAGE = 12;
/**
* MIDLET
*/
public static StoryMIDlet te = null;
/**
* 当前有效显示界面
*/
public static byte form = -1;
/**
* 上一个有效显示界面
*/
public static byte oldform = -1;
public static boolean music = true;
public static int musicVolumn = 30;
/**
* 判定是否是第一次启动程序时调用showNotify
*/
public static boolean isStartShowNotify = false;
/**
* currentPlayer
* 当前选中进行游戏的人物
* oldGunLevel
* 当前选中进行游戏的人物子弹等级
* oldScore
* 当前分数
*/
public static int currentPlayer,oldGunLevel,oldScore;
/**
* 记录当前的关卡数
*/
public static int stage = 1;
private static boolean victory;
/**
* 确定显示的界面
* @param whichForm为控制界面的整型参数
*/
public static void show(byte whichForm) {
if (whichForm != form) {
ActorCanvas tempFocusLayer; // 初始化类时使用,避免类还没有初始化完就被主线程使用
if (focusLayer != null && whichForm != MENU_STAGE &&
whichForm != ABOUT_STAGE && whichForm != HELP_STAGE) {
focusLayer.reCycle();
focusLayer = null;
if (oldFocusLayer != null) {
oldFocusLayer.reCycle();
oldFocusLayer = null;
}
System.gc();
}
switch (whichForm) {
case PLAY_STAGE:
focusLayer = null;
tempFocusLayer = PlayScreen.getInstance();
focusLayer = tempFocusLayer;
break;
case LOGO_STAGE:
focusLayer = null;
tempFocusLayer = new LogoScreen();
focusLayer = tempFocusLayer;
break;
case LOAD_RESOURCE_STAGE:
tempFocusLayer = new LoadResourceScreen();
focusLayer = tempFocusLayer;
break;
case TITLE_STAGE:
focusLayer = null;
stage = 1;
tempFocusLayer = new TitleScreen();
focusLayer = tempFocusLayer;
break;
case ABOUT_STAGE:
oldFocusLayer = focusLayer;
focusLayer = null;
tempFocusLayer = new AboutScreen();
focusLayer = tempFocusLayer;
break;
case HELP_STAGE:
if (form != MENU_STAGE) {
oldFocusLayer = focusLayer;
} else {
focusLayer.reCycle();
focusLayer = null;
form = oldform;
}
focusLayer = null;
tempFocusLayer = new HelpScreen();
focusLayer = tempFocusLayer;
break;
case MENU_STAGE:
if (form != HELP_STAGE && form != ABOUT_STAGE) {
oldFocusLayer = focusLayer;
} else {
focusLayer.reCycle();
focusLayer = null;
form = oldform;
}
tempFocusLayer = new MenuScreen();
focusLayer = tempFocusLayer;
break;
case PLAYER_SELECT:
focusLayer = null;
tempFocusLayer = new PersonScreen();
focusLayer = tempFocusLayer;
break;
case GAME_START:
//oldFocusLayer = focusLayer;
focusLayer = null;
tempFocusLayer = PlayScreen.getInstance();
PlayScreen.initialNewCase();
focusLayer = tempFocusLayer;
break;
case PASS_STAGE:
oldFocusLayer = focusLayer;
stage = stage+1;
focusLayer = null;
tempFocusLayer = new PassScreen();
focusLayer = tempFocusLayer;
break;
default:
break;
}
setForm(whichForm);
}
}
/**
* return the curent Display Canvas
*
* @return
*/
public static ActorCanvas getDisplay() {
return focusLayer;
}
/**
* 返回上一个有效状态界面
*
*/
public static void returnOldFocus() {
focusLayer.reCycle();
focusLayer = null;
focusLayer = oldFocusLayer;
setForm(oldform);
oldFocusLayer = null;
}
public static void setForm(byte whichForm) {
oldform = form;
form = whichForm;
}
//播放声音调用过程
public static void playSound(int ikey, int count) {
if (music) {
MusicPlayer.getinstance().play(ikey, count);
}
}
public static void resumeMusic() {
switch (oldform) {
//TODO:确定在哪个界面上需要恢复声音
}
}
public static void stopMusic() {
MusicPlayer.getinstance().stop();
}
public static void upMusic() {
if (music) {
int volumn = MusicPlayer.getinstance().getVolume() + 5;
if (volumn > 100) {
volumn = 100;
}
MusicPlayer.getinstance().setVolume(volumn);
}
}
public static void downMusic() {
if (music) {
int volumn = MusicPlayer.getinstance().getVolume() - 5;
if (volumn < 0) {
volumn = 0;
}
MusicPlayer.getinstance().setVolume(volumn);
}
}
/**
* 得到当前的关卡
*
* @return
*/
public static int getStage() {
return stage;
}
public static void setStage(int newStage) {
stage = newStage;
}
public static boolean isVictory() {
return victory;
}
public static void setVictory(boolean victoryArg) {
victory = victoryArg;
}
/**
* 读取音乐设置
*
* @return
*/
public static void loadMusicSetting() {
String recordinfo = null;
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore("music", false);
if (rs == null) {
setMusic();
} else {
ByteArrayInputStream bis = new ByteArrayInputStream(rs.getRecord(1));
DataInputStream dis = new DataInputStream(bis);
music = dis.readBoolean();
musicVolumn = dis.readInt();
dis.close();
bis.close();
rs.closeRecordStore();
rs = null;
}
} catch (RecordStoreNotFoundException rex) {
try {
rs.closeRecordStore();
rs = null;
} catch (Exception S) {
}
} catch (Exception e) {
try {
rs.closeRecordStore();
rs = null;
} catch (Exception S) {
}
}
}
/**
* 设置音乐
*
*/
public static void setMusic() {
RecordStore rs = null;
try {
RecordStore.deleteRecordStore("music");
} catch (Exception ex) {
}
try {
rs = RecordStore.openRecordStore("music", true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeBoolean(music);
dos.writeInt(musicVolumn);
rs.addRecord(bos.toByteArray(), 0, (bos.toByteArray()).length);
dos.close();
bos.close();
rs.closeRecordStore();
rs = null;
} catch (Exception e) {
try {
rs.closeRecordStore();
rs = null;
} catch (Exception S) {
System.out.println("get Player error: " + S);
}
}
}
}