📄 gameloadprocesscanvas.java
字号:
package cn.org.matrix.gmatrix.practice.demo01;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import cn.org.matrix.gmatrix.gameLab.GameObjectQueue;
import cn.org.matrix.gmatrix.gameLab.engine.XmlScriptParser;
import cn.org.matrix.gmatrix.gameLab.control.*;
/**
* 处理游戏load的画布:显示游戏装载进程,这里没有继承GameUI类,以后要实现
* @author cleverpig
*
*/
public class GameLoadProcessCanvas extends Canvas implements Runnable{
//配置文件资源URL
private String configureResURL=null;
//当前处理进度
private int processing=0;
//进度条的起始x坐标
private int drawX=0;
//进度条的起始y坐标
private int drawY=0;
//进度条的宽度
private int drawWidth=0;
//进度条的高度
private int drawHeight=0;
//是否完成装载
private boolean loadFinish=false;
//背景颜色
private final int bgColor=0xffffff;
//进度条颜色
private final int processBarColor=0xff0000;
//进度条边框颜色
private final int processBarBorderColor=0x000000;
//字体颜色
private final int fontColor=0x000000;
//处理进度最大值
public final int PROCESS_MAX=15;
/**
* 构造方法
* @param configureResURL 配置文件资源URL
* @param processMax 处理进度最大值
*/
public GameLoadProcessCanvas(String configureResURL){
this.configureResURL=configureResURL;
drawX=0;
drawY=this.getHeight()-this.getHeight()/5;
drawHeight=this.getHeight()/10;
drawWidth=0;
loadFinish=false;
}
/**
* 初始化
*/
public void init(){
drawX=0;
drawY=this.getHeight()-this.getHeight()/5;
drawHeight=this.getHeight()/10;
drawWidth=0;
loadFinish=false;
processing=0;
}
/**
* 绘制图形
* @param g Graphics对象
*/
public void paint(Graphics g){
//使用背景色填充
g.setColor(bgColor);
g.fillRect(0,0,this.getWidth(),this.getHeight());
//计算进度
int processPercent=100*processing/PROCESS_MAX;
drawWidth=this.getWidth()*processing/PROCESS_MAX;
//填充进度条
g.setColor(processBarColor);
g.fillRect(drawX,drawY,drawWidth,drawHeight);
//勾画进度条边线
g.setColor(processBarBorderColor);
g.drawRect(drawX,drawY,this.getWidth(),drawHeight);
//绘制文字
g.setColor(fontColor);
Font font=g.getFont();
String str="装载进度:"+processPercent+"%";
g.drawString(str,this.getWidth()/2-font.stringWidth(str)/2,drawY,Graphics.TOP|Graphics.LEFT);
if (processPercent!=100){
str="正在装载游戏请等待...";
}
else{
str="装载游戏完成,按\"Fire\"键进入游戏!";
loadFinish=true;
}
g.drawString(str,this.getWidth()/2-font.stringWidth(str)/2,this.getHeight()/2,Graphics.TOP|Graphics.LEFT);
}
//线程执行方法
public void run(){
long startTime=System.currentTimeMillis();
repaint();
XmlScriptParser x=new XmlScriptParser();
//打开xml配置文件
x.openConfigure(configureResURL);
boolean firstInit=(MainMIDlet.gameLoader.getGlobalActorTable()==null);
if (firstInit){
System.out.println("第一次装载数据");
//1.装载消息对象
GameObjectQueue msgTable=x.readMsgConfigure(true);
System.out.println("msg数量="+msgTable.size());
processing++;
repaint();
//2.装载消息对列,并与消息对象作关联
GameObjectQueue mqTable=x.readMsgQueueConfigure(new GameObjectQueue[]{msgTable},true);
System.out.println("msg Queue数量="+mqTable.size());
MainMIDlet.gameLoader.setMsgTable(mqTable);
processing++;
repaint();
//3.装载事件对象配置
GameObjectQueue eventTable=x.readEventConfigure(true);
System.out.println("event数量="+eventTable.size());
processing++;
repaint();
//4.装载事件列表,并与事件对象作关联
GameObjectQueue eqTable=x.readEventQueueConfigure(new GameObjectQueue[]{eventTable},true);
System.out.println("event Queue数量="+eqTable.size());
MainMIDlet.gameLoader.setEventTable(eqTable);
processing++;
repaint();
//5.装载道具对象
GameObjectQueue propTable=x.readPropertyConfigure(true);
System.out.println("property数量="+propTable.size());
processing++;
repaint();
//6.装载道具箱,并与道具对象作关联
GameObjectQueue propBoxTable=x.readPropertyBoxConfigure(new GameObjectQueue[]{propTable},true);
System.out.println("propertyBox数量="+propBoxTable.size());
MainMIDlet.gameLoader.setPropertyTable(propBoxTable);
processing++;
repaint();
//7.装载NPC对象,关联道具箱对象、事件对象
GameObjectQueue npcTable=x.readNpcConfigure(new GameObjectQueue[]{propBoxTable,eventTable},true);
System.out.println("npc数量="+npcTable.size());
MainMIDlet.gameLoader.setNpcTable(npcTable);
processing++;
repaint();
//8.装载主角,关联道具箱对象
GameObjectQueue actorTable=x.readActorConfigure(new GameObjectQueue[]{propBoxTable},true);
System.out.println("actor数量="+actorTable.size());
processing++;
repaint();
//9.装载图层对象
GameObjectQueue layerTable=x.readLayerConfigure(true);
System.out.println("layer数量="+layerTable.size());
MainMIDlet.gameLoader.setLayerTable(layerTable);
processing++;
repaint();
//10.装载地图转换器对象
GameObjectQueue transformerTable=x.readTransformerConfigure(true);
System.out.println("transformer数量="+transformerTable.size());
MainMIDlet.gameLoader.setTransformerTable(transformerTable);
processing++;
repaint();
//11.装载地图对象,关联NPC、图层、地图转换器
GameObjectQueue mapTable=x.readMapConfigure(new GameObjectQueue[]{layerTable,npcTable,transformerTable},true);
System.out.println("map数量="+mapTable.size());
MainMIDlet.gameLoader.setMapTable(mapTable);
processing++;
repaint();
//12.装载关卡对象,并关联地图对象
GameObjectQueue levelTable=x.readLevelConfigure(new GameObjectQueue[]{mapTable},true);
System.out.println("level数量="+levelTable.size());
MainMIDlet.gameLoader.setLevelTable(levelTable);
processing++;
repaint();
//13.装载摄像机对象
GameObjectQueue cameraTable=x.readCameraConfigure(true);
System.out.println("camera数量="+cameraTable.size());
processing++;
repaint();
//14.装载音乐对象
GameObjectQueue musicTable=x.readMusicConfigure(true);
System.out.println("music数量="+musicTable.size());
processing++;
repaint();
//15.装载游乐场运行间隔
MainMIDlet.gameLoader.setCarnieRunInterval(x.readCarnieRunInterval(true));
System.out.println("游乐场运行间隔="+MainMIDlet.gameLoader.getCarnieRunInterval());
processing++;
repaint();
//与gameLoader作关联
MainMIDlet.gameLoader.setGlobalCameraTable(cameraTable);
MainMIDlet.gameLoader.setGlobalMusicTable(musicTable);
MainMIDlet.gameLoader.setGlobalLevelTable(levelTable);
MainMIDlet.gameLoader.setGlobalActorTable(actorTable);
}
else{
System.out.println("非首次装载数据,使用cache数据");
processing+=4;
repaint();
//5.装载道具对象
GameObjectQueue propTable=x.readPropertyConfigure(true);
System.out.println("property数量="+propTable.size());
processing++;
repaint();
if (propTable.size()==0){
System.out.println("装载道具对象为空");
}
//6.装载道具箱,并与道具对象作关联
GameObjectQueue propBoxTable=x.readPropertyBoxConfigure(new GameObjectQueue[]{propTable},true);
System.out.println("propertyBox数量="+propBoxTable.size());
MainMIDlet.gameLoader.setPropertyTable(propBoxTable);
processing++;
repaint();
//7.装载NPC对象,关联道具箱对象、事件对象
GameObjectQueue npcTable=x.readNpcConfigure(new GameObjectQueue[]{propBoxTable,
MainMIDlet.gameLoader.getEventTable()},true);
System.out.println("npc数量="+npcTable.size());
MainMIDlet.gameLoader.setNpcTable(npcTable);
processing++;
repaint();
if (npcTable.size()==0){
System.out.println("装载NPC对象为空");
}
//8.装载主角,关联道具箱对象
GameObjectQueue actorTable=x.readActorConfigure(new GameObjectQueue[]{propBoxTable},true);
System.out.println("actor数量="+actorTable.size());
processing++;
repaint();
if (actorTable.size()==0){
System.out.println("装载主角为空");
}
processing+=2;
repaint();
//11.装载地图对象,关联NPC、图层、地图转换器
GameObjectQueue mapTable=x.readMapConfigure(new GameObjectQueue[]{MainMIDlet.gameLoader.getLayerTable(),
npcTable,MainMIDlet.gameLoader.getTransformerTable()},false);
System.out.println("map数量="+mapTable.size());
MainMIDlet.gameLoader.setMapTable(mapTable);
processing++;
repaint();
if (mapTable.size()==0){
System.out.println("装载地图对象为空");
}
//12.装载关卡对象,并关联地图对象
GameObjectQueue levelTable=x.readLevelConfigure(new GameObjectQueue[]{mapTable},true);
System.out.println("level数量="+levelTable.size());
MainMIDlet.gameLoader.setLevelTable(levelTable);
processing++;
repaint();
if (levelTable.size()==0){
System.out.println("装载关卡对象为空");
}
processing+=3;
repaint();
//与gameLoader作关联
MainMIDlet.gameLoader.setGlobalLevelTable(levelTable);
MainMIDlet.gameLoader.setGlobalActorTable(actorTable);
}
System.out.println("装载工作花费时间:"+(System.currentTimeMillis()-startTime)+" ms");
}
/**
* 处理键盘事件:玩家按Fire键进入游戏
* @param keyCode 键盘代码
*/
public void keyPressed(int keyCode){
System.out.println("键盘值为:"+getGameAction(keyCode));
if ((KeyManager.getKeyValue(getGameAction(keyCode))==KeyManager.FIRE_KEYPRESS)&&(loadFinish)){
System.out.println("玩家按下Fire键进入游戏");
MainMIDlet.gameNavigator.switchSomething(GameNavigator.CARNIE_CANVAS);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -