📄 gamemanage.java
字号:
/**
* <p>Title: Transpanzer</p>
* <p>Description:
* You cannot remove this copyright and notice.
* You cannot use this file any part without the express permission of the author.
* All Rights Reserved</p>
* @author Jjyo
* @email jjyo@163.com
* @version 1.0.0
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
public class GameManage {
public Menu menu; //游戏菜单
public KeyMask keyMask; //按键
public LoadGameResource lgr; //资源
public boolean isGameStart=false; //游戏开始
public static boolean isVolOn;
public static int level=1; //关卡数
public static int mapIndex=12; //地图索引
public static byte gameState; //游戏状态
public static final byte GAMESTATE_MENU=1;
public static final byte GAMESTATE_GAMELOOP=2;
public static final byte GAMESTATE_PAUSE=3;
public static final byte GMAESTATE_ABOUT=4;
public static final byte GMAESTATE_CONTROL=5;
public static final byte GAMESTATE_CONTINUE=6;
//需要使用lgr来构造和游戏中的敌人,所有游戏中用的Sprite的构造
public GameManage(){
System.out.println("GameMange Successful....");
menu=new Menu(this);
lgr=new LoadGameResource(this);
lgr.level(mapIndex);
}
/**
* TODO 游戏中所有带有 action 成员函数的处理
*/
public void action() {
lgr.poit.action();
/* mainSprite Tank */
lgr.tankSprite.action(); //坦克
lgr.staticTankSprite.action();
/* ObjectOffset */
lgr.objectOffset();
//TANK BOMB
if(!lgr.tankBomb.isHidden){
lgr.tankBomb.action();
}
if(!TankSprite.isChangeScreen){
TankSprite.haveData=false;
LoadGameResource.data=0x00;
/* EnemySprite16 */
for(int i=lgr.enemySprite16.length-1;i>=0;i--){
if(!lgr.enemySprite16[i].isHidden){
lgr.enemySprite16[i].action();
}
}
/* EnemySprite32 */
for(int i=lgr.enemySprite32.length-1;i>=0;i--){
if(!lgr.enemySprite32[i].isHidden)
lgr.enemySprite32[i].action();
}
/* Bomb */
for(int i=lgr.bomb.length-1;i>=0;i--){
if(!lgr.bomb[i].isHidden){
lgr.bomb[i].action();
}
}
/* TankBullet */
for(int i=lgr.tankBullet.length-1;i>=0;i--){
if(!lgr.tankBullet[i].isHidden){
lgr.tankBullet[i].action();
}
}
/* EnemyBullet */
for(int i=lgr.tankBullet.length-1;i>=0;i--){
if(!lgr.enemyBullet[i].isHidden){
lgr.enemyBullet[i].action();
}
}
/* Goods */
for(int i=lgr.goods.length-1;i>=0;i--){
if(!lgr.goods[i].isHidden){
lgr.goods[i].action();
}
}
/* 静态物品 */
for(int i=lgr.staticGoods.length-1;i>=0;i--){
if(!lgr.staticGoods[i].isHidden){
lgr.staticGoods[i].action();
}
}
/* Corps */
for(int i=lgr.corps.length-1;i>=0;i--){
if(!lgr.corps[i].isHidden){
lgr.corps[i].action();
}
}
/* Tank Beam */
if(!lgr.beam.isHidden){
lgr.beam.action();
}
}else{
if(LoadGameResource.joinData){
lgr.initData();
}
}
}
/* 选择类 action成员函数的处理 */
public void menuAction(){
// TODO Auto-generated method stub
lgr.choose.action();
}
/* 继续游戏 */
public void contineAction() {
// TODO Auto-generated method stub
isGameStart=false;
lgr.choose.action();
menu.continueAction();
}
/* 关于菜单 */
public void menuAbout() {
// TODO Auto-generated method stub
gameState=GMAESTATE_ABOUT;
}
/* 菜单声音开关 */
public void menuAudio() {
// TODO Auto-generated method stub
if(isGameStart){
if(isVolOn){
if(lgr.bgAudio==null){
lgr.audioRes(level);
}
lgr.bgAudio.setLoop();
lgr.bgAudio.play();
}else{
if(lgr.bgAudio!=null){
lgr.bgAudio.stop();
lgr.bgAudio.close();
lgr.bgAudio=null;
System.gc();
}
}
}else{
if(isVolOn){
if(lgr.menuAudio==null){
lgr.audioMenu();
}
lgr.menuAudio.setLoop();
lgr.menuAudio.play();
}else{
if(lgr.menuAudio!=null){
lgr.menuAudio.stop();
lgr.menuAudio.close();
lgr.menuAudio=null;
System.gc();
}
}
}
}
/* 从菜单开始新游戏 */
public void menuBeginNewGame() {
// TODO Auto-generated method stub
gameState=GAMESTATE_GAMELOOP;
TankSprite.isChangeScreen=true;
TankGameCanvas.index=3;
isGameStart=true;
if(isVolOn){
if(lgr.menuAudio!=null){
lgr.menuAudio.close();
lgr.menuAudio=null;
System.gc();
}
lgr.audioRes(level);
lgr.bgAudio.setLoop();
lgr.bgAudio.play();
}
}
/* 继续游戏 */
public void continueGame(){
isGameStart=true;
lgr.tankSprite.reInit();
TankSprite.spriteState=TankSprite.TANK;
GameManage.gameState=GameManage.GAMESTATE_GAMELOOP;
switch(level){
case 1:
level(12);
Maps.x=0;
Maps.y=300;
lgr.tankSprite.x=80;
lgr.tankSprite.y=100;
TankSprite.isChangeScreen=true;
TankGameCanvas.index=3;
break;
case 2:
break;
}
}
/* 菜单退出游戏 */
public void menuExitGame() {
// TODO Auto-generated method stub
isGameStart=false;
TankMIDlet.gameCanvas.midlet.exitMIDlet();
}
/* 创建物品 */
public void creatGoods(int x,int y,int goodsType){
for(int i=lgr.goods.length-1;i>=0;--i){
if(lgr.goods[i].isHidden&&goodsType!=0){
lgr.goods[i].init(x, y,goodsType);
goodsType=0;
return;
}
}
}
/* 创建主角子弹 */
public void creatBullet(int x,int y,int xSpeed,int ySpeed,int bulletType){
for(int i=lgr.tankBullet.length-1;i>=0;--i)
{
if(lgr.tankBullet[i].isHidden)
{
lgr.tankBullet[i].initBullet( x, y,xSpeed,ySpeed,bulletType);
return;
}
}
}
/* 敌人炮弹 */
public void creatEnemyBullet(int x,int y,int xSpeed,int ySpeed,int bulletType){
for(int i=lgr.enemyBullet.length-1;i>=0;--i)
{
if(lgr.enemyBullet[i].isHidden)
{
lgr.enemyBullet[i].initBullet( x, y,xSpeed,ySpeed,bulletType);
return;
}
}
}
/* 创建爆炸效果 */
public void creatBombEffect(int x,int y,int bombType){
for(int i=lgr.bomb.length-1;i>0;--i)
{
if(lgr.bomb[i].isHidden){
lgr.bomb[i].initBomb(x, y, bombType);
return;
}
}
}
/* 创建 */
public void creatBeam(int x,int y,byte beamType){
if(lgr.beam.isHidden){
lgr.beam.init(x*16-Maps.x, y*16-Maps.y, beamType);
return ;
}
}
/* 创建Tank死亡时的爆炸效果 */
public void creatTankBomb(int x,int y,char tankState){
if(lgr.tankBomb.isHidden){
lgr.tankBomb.initBomb(x, y, tankState);
return;
}
}
/* 关卡 */
public void level(int mapIndex){
lgr.level(mapIndex);
if(lgr.menuAudio!=null){
lgr.menuAudio.stop();
lgr.menuAudio.close();
lgr.menuAudio=null;
System.gc();
}
if(isVolOn){
if(lgr.bgAudio==null){
lgr.audioRes(level);
lgr.bgAudio.setLoop();
lgr.bgAudio.play();
}else{
lgr.bgAudio.replay();
}
}
}
/* 按键按下调用 */
public void judgeKeyCode(int keyCode, int gameKeyCode)
{
menu.judgeKeyCode(keyCode,gameKeyCode);
if(gameState==GAMESTATE_GAMELOOP&&!TankSprite.isChangeScreen&&!TankSprite.isDead)
{
lgr.tankSprite.judgeKeyCode(keyCode,gameKeyCode);
}
}
/* 按键释放调用 */
public void freeKey(int keyCode, int gameKeyCode)
{
menu.freeKey(keyCode,gameKeyCode);
if(gameState==GAMESTATE_GAMELOOP)
{
lgr.tankSprite.freeKey(keyCode,gameKeyCode);
}
}
/* 记录名称 */
private static final String rmsName = "rms";
/* 读档 */
public void menuLoadGame() {
// TODO Auto-generated method stub
isGameStart=true;
int i;
RecordStore rs = null;
byte[] data = null;
try {
rs = RecordStore.openRecordStore(rmsName,true);
if(rs.getNumRecords() > 0)
data = rs.getRecord(1);
} catch (RecordStoreFullException e) {
System.out.println(e.toString());
} catch (RecordStoreNotFoundException e) {
System.out.println(e.toString());
} catch (RecordStoreException e) {
System.out.println(e.toString());
} finally{
try {
rs.closeRecordStore();
} catch (RecordStoreNotOpenException e) {
} catch (RecordStoreException e) {
} finally{
rs = null;
}
}
if(data == null){
System.out.println("data is empty");
return;
}
ByteArrayInputStream bais=new ByteArrayInputStream(data);
DataInputStream dis=new DataInputStream(bais);
try {
/**
* 数据读取
*------------Start---------------
*/
//游戏开始
isGameStart=true;
//游戏状态中
gameState=GAMESTATE_GAMELOOP;
//读取上一次声音设置
isVolOn=dis.readBoolean();
//读取关卡
level = dis.readInt();
//读取关卡内的地图
mapIndex = dis.readInt();
//装载地图
level(mapIndex);
//EnemySprite32
i=dis.readInt();
for(i=i-1;i>=0;i--){
lgr.enemySprite32[i].readData(dis);
}
//EnemySprite16
i=dis.readInt();
for(i=i-1;i>=0;i--){
lgr.enemySprite16[i].readData(dis);
}
//StaticGoods
i=dis.readInt();
for(i=i-1;i>=0;i--){
lgr.staticGoods[i].readData(dis);
}
//Tank
lgr.tankSprite.readData(dis);
//Static Tank
lgr.staticTankSprite.readData(dis);
TankGameCanvas.index=2;
TankSprite.isChangeScreen=true;
System.out.println("load successful");
/**------------End-------------*/
} catch (IOException e1) {
} finally {
try {
dis.close();
} catch (IOException e2) {
} finally{
dis = null;
System.gc();
}
}
}
/* 存档 */
public void menuSaveGame(){
int i;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(baos);
try {
/**
* 数据写入
*------------Start-------------
*/
//写入当前声音设置
dos.writeBoolean(isVolOn);
//写入当前关卡
dos.writeInt(level);
//写入当前关卡地图
dos.writeInt(mapIndex);
//EnemySprite32
dos.writeInt(lgr.enemySprite32.length);
for(i=lgr.enemySprite32.length-1;i>=0;i--){
lgr.enemySprite32[i].writeData(dos);
}
//EnemySprite16
dos.writeInt(lgr.enemySprite16.length);
for(i=lgr.enemySprite16.length-1;i>=0;i--){
lgr.enemySprite16[i].writeData(dos);
}
//staticGood
dos.writeInt(lgr.staticGoods.length);
for(i=lgr.staticGoods.length-1;i>=0;i--){
lgr.staticGoods[i].writeData(dos);
}
//Tank
lgr.tankSprite.writeData(dos);
//Static Tank
lgr.staticTankSprite.writeData(dos);
gameState=GAMESTATE_GAMELOOP;
/**-------------End-------------*/
} catch (IOException e1) {
} finally{
try {
dos.close();
} catch (IOException e2) {
} finally{
dos = null;
}
}
byte[] data = baos.toByteArray();
if(data == null){
return;
}
RecordStore rs = null;
try {
rs = RecordStore.openRecordStore(rmsName,true);
if(rs.getNumRecords() == 0)
rs.addRecord(data,0,data.length);
else
rs.setRecord(1,data,0,data.length);
System.out.println("Save successfunl");
} catch (RecordStoreFullException e) {
System.out.println(e.toString());
} catch (RecordStoreNotFoundException e) {
System.out.println(e.toString());
} catch (RecordStoreException e) {
System.out.println(e.toString());
} finally {
try {
rs.closeRecordStore();
} catch (RecordStoreNotOpenException e2) {
} catch (RecordStoreException e2) {
} finally {
rs = null;
System.gc();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -