📄 canvas1.java
字号:
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
//import com.nokia.mid.ui.FullCanvas;
public class Canvas1 extends GameCanvas implements Runnable
{
private Display disp;
private Displayable last;
Graphics g;
boolean isPlay;
int delay;
int width;
int height;
boolean isDrawing;
boolean isLoading;
//public static Vector allCollisionVC;
static int groundWidth;
static int groundHeight;
LayerManager backLayer,frontLayer,npcLayer,fixLayer;
RoleLayer roleLayer;
int windowX,windowY; //可见视窗左上角坐标
int runStatus;
int shopStatus;
int itemStatus;
int equipStatus;
int sysMenuCmdStatus, cmdListStatus;
static final int STATUS_WALK=1;
static final int STATUS_CHAT=2;
static final int STATUS_SHOWMENU=3;
static final int STATUS_SHOWSTATUS=4;
static final int STATUS_SHOWITEM=5;
static final int STATUS_SHOWEQUIP=6;
static final int STATUS_SHOWSKILL=7;
static final int STATUS_SHOWSYSTEM=8;
static final int STATUS_SHOPING = 9;
static final int STATUS_SHOWMISSION=10;
static final int ITEMSTATUS_NORMAL=0;
static final int ITEMSTATUS_COMMAND=1;
static final int EQUIPSTATUS_NORMAL=0;
static final int EQUIPSTATUS_COMMAND=1;
static final int SHOPSTATUS_CONFIRM = 0;
static final int SHOPSTATUS_NORMAL = 1;
static final int SYSTEM_CMDMENU_OPENING = 0;
static final int SYSTEM_CMDMENU_CLOSED = 1;
static final int SYSTEM_CMDLIST_OPENING = 0;
static final int SYSTEM_CMDLIST_CLOSED = 1;
static final int LEFT_PRESSED=0x0004;
static final int RIGHT_PRESSED=0x0020;
static final int UP_PRESSED=0x0002;
static final int DOWN_PRESSED=0x0040;
MsgBoard msgBoard;
Command pauseCommand,startCommand;
NPCFactory npcFactory;
EnemyFactory enemyFactory;
MySprite chatSprite;
boolean inChatArea;
boolean inBattle;
BattleCanvas battleCanvas;
GameMenu gameMenu;
Shoping shoping;
SystemMenu sysMenu;
MatCommand sysMenuCmd;
MissionTab missionTab;
public static int shopBtnCursor = 0; //0 is OK Button,1 is Cancel Button
MatCommand matCommand;
MyAlert myAlert;
Controller controller;
FaceShower faceShower;
int faceIndex;
public Canvas1(String title,Display disp,Displayable last)
{
super(false);
this.disp = disp;
this.last = last;
this.setFullScreenMode(true);
}
public void init()
{
this.g = getGraphics();
delay = 1000/60;
width = this.getWidth();
height = this.getHeight();
if(width>=176)
{
if(height<208)
{
height=208;
}
}
gameMenu = new GameMenu(this);
controller = new Controller(this);
faceShower = new FaceShower(g);
faceIndex = -1;
sysMenu = new SystemMenu(this);
loadScene(); //载入场景
System.out.println("初始化完成");
}
LoadBar loadBar;
public void loadScene()
{
isLoading=true;
//显示进度条
loadBar=new LoadBar(disp,this);
loadBar.start();
disp.setCurrent(loadBar);
Loader loader=new Loader();
loader.start();
}
private void doLoadScene()
{
GameMIDlet.midlet.stopSound();
System.out.println("载入场景");
keyStates=0;
for(int i=0;backLayer!=null && i<backLayer.getSize();i++)
{
backLayer.remove(backLayer.getLayerAt(i));
}
for(int i=0;frontLayer!=null && i<frontLayer.getSize();i++)
{
frontLayer.remove(frontLayer.getLayerAt(i));
}
for(int i=0;roleLayer!=null && i<roleLayer.getSize();i++)
{
roleLayer.remove(roleLayer.getLayerAt(i));
}
if(npcFactory!=null)
npcFactory.removeAll();
if(enemyFactory!=null)
enemyFactory.removeAll();
backLayer=null;
frontLayer=null;
roleLayer=null;
npcFactory=null;
enemyFactory=null;
System.gc();
inChatArea=false;
isDrawing=false;
groundWidth=SceneLib.scenes[SceneLib.currentSceneIndex].getGroundWidth();
groundHeight=SceneLib.scenes[SceneLib.currentSceneIndex].getGroundHeight();
windowX=0;
windowY=0;
runStatus=Canvas1.STATUS_WALK;
if(loadBar!=null)loadBar.percent=10;
if(GameMIDlet.mainRole==null)
{
Image playerImg = Tools.getImage("/role/player.png");
//System.out.println("mainRole=new MainRole()");
GameMIDlet.mainRole=new MainRole(playerImg,24,32); //创建主角对象
GameMIDlet.mainRole.setMaxLife(500);
GameMIDlet.mainRole.setLife(GameMIDlet.mainRole.getMaxLife());
GameMIDlet.mainRole.setMaxMagic(100);
GameMIDlet.mainRole.setMagic(GameMIDlet.mainRole.getMaxMagic());
GameMIDlet.mainRole.setLv(1);
GameMIDlet.mainRole.setPower(15);
GameMIDlet.mainRole.setDefence(15);
GameMIDlet.mainRole.setActSpeed(5);
GameMIDlet.mainRole.setExp(0);
GameMIDlet.mainRole.setNextExp(100);
GameMIDlet.mainRole.setMoney(500);
Skill skill;
for(int i=0;i<SkillLib.skills.length-1;i++)
{
skill = SkillLib.getSkillByIndex(i);
if(skill != null)
GameMIDlet.mainRole.addSkill(skill);
}
}
else
{
GameMIDlet.mainRole.init();
}
if(loadBar!=null)loadBar.percent=20;
//设置主角是否遇雷
GameMIDlet.mainRole.setCanTouchMine(SceneLib.scenes[SceneLib.currentSceneIndex].isHaveMine());
if(GameMIDlet.mainRole.isCanTouchMine())
{
MyRandom myRandom=new MyRandom();
myRandom.setMinInt(-30);
myRandom.setMinInt(50);
GameMIDlet.mainRole.setMineTimeCounter(SceneLib.scenes[SceneLib.currentSceneIndex].getMineTimerCounter()+myRandom.nextInt());
}
//回村自动加满血
if(SceneLib.currentSceneIndex==0)
{
GameMIDlet.mainRole.setLife(GameMIDlet.mainRole.getMaxLife());
GameMIDlet.mainRole.setMagic(GameMIDlet.mainRole.getTotalMagic());
}
System.out.println("主角创建结束");
if(loadBar!=null)loadBar.percent=30;
ChatSprites.clearAll(); //对话对象清空
System.out.println("对话对象清空结束");
SceneCreator sceneCreator=new SceneCreator();
backLayer=sceneCreator.getBackLayer(); //取得背景层
System.out.println("取得背景层");
if(loadBar!=null)loadBar.percent=40;
BlockLib.removeAll();
BlockLib.addBlockVC(sceneCreator.getCollisionVC());
System.out.println("加入障碍物");
if(loadBar!=null)loadBar.percent=50;
frontLayer=sceneCreator.getFrontLayer(); //取得前景层
System.out.println("取得前景层");
if(loadBar!=null)loadBar.percent=60;
//fixLayer=new FixLayer(this); //创建固定层
//创建角色层roleLayer(将置入主角、NPC、敌人)
roleLayer = new RoleLayer();
//加入主角
roleLayer.addSprite(GameMIDlet.mainRole);
if(loadBar!=null)loadBar.percent=70;
//加入NPC
npcFactory=new NPCFactory();
for(int i=0;npcFactory.getSpriteVC()!=null && i<npcFactory.getSpriteVC().size();i++)
{
roleLayer.addSprite((MySprite)npcFactory.getSpriteVC().elementAt(i));
}
System.out.println("加入NPC");
if(loadBar!=null)loadBar.percent=80;
//加入敌人
enemyFactory=new EnemyFactory();
if(!GameMIDlet.isOutBattle) //不是从战场回来的话生成新敌人
{
enemyFactory.init(); //生成敌人
if(GameMIDlet.midlet.enemyVC==null)
GameMIDlet.midlet.enemyVC=new Vector();
else
GameMIDlet.midlet.enemyVC.removeAllElements();
for(int i=0;enemyFactory.getSpriteVC()!=null && i<enemyFactory.getSpriteVC().size();i++)
{
roleLayer.addSprite((MySprite)enemyFactory.getSpriteVC().elementAt(i));
GameMIDlet.midlet.enemyVC.addElement((MySprite)enemyFactory.getSpriteVC().elementAt(i));
}
}
else
{
for(int i=0;GameMIDlet.midlet.enemyVC!=null && i<GameMIDlet.midlet.enemyVC.size();i++)
{
roleLayer.addSprite((MySprite)GameMIDlet.midlet.enemyVC.elementAt(i));
enemyFactory.addSprite((MySprite)GameMIDlet.midlet.enemyVC.elementAt(i));
}
}
System.out.println("加入敌人");
if(loadBar!=null)loadBar.percent=90;
//定位屏幕中心到主角
locateWindow();
System.out.println("场景载入完成");
System.gc();
if(loadBar!=null)loadBar.percent=100;
if(GameMIDlet.isOutBattle)
{
GameMIDlet.isOutBattle=false;
}
startGame();
GameMIDlet.midlet.setMusic(SceneLib.currentSceneIndex);
GameMIDlet.midlet.playSound();
enemyFactory.startAutoMove();
npcFactory.startAutoMove();
isLoading=false;
}
boolean ThreadRunning;
long t1;
public void startGame()
{
isPlay = true;
if(!ThreadRunning)
{
t1=System.currentTimeMillis();
runTimes=0;
(new Thread(this)).start();
}
}
public void stop()
{
isPlay=false;
}
public void drawFont()
{
g.setColor(0xFFFFFF);
g.drawString("w:"+width+" h:"+height,1,height-11,Graphics.LEFT | Graphics.TOP);
g.drawString("total memory:" + Runtime.getRuntime().totalMemory() + " byte",1,height-22,Graphics.LEFT | Graphics.TOP);
g.drawString("free memory:" + Runtime.getRuntime().freeMemory() + " byte",1,height-34,Graphics.LEFT | Graphics.TOP);
g.drawString("used memory:" + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) + " byte",1,height-46,Graphics.LEFT | Graphics.TOP);
//g.drawString("AlphaLevels:" + disp.numAlphaLevels(),1,height-48,Graphics.LEFT | Graphics.TOP);
//g.drawString("playerX:"+mainRole.getX()+" playerY:"+mainRole.getY(),2,15,Graphics.LEFT | Graphics.TOP);
//g.drawString("walking:"+walking,1,60,Graphics.LEFT | Graphics.TOP);
g.setColor(0x000000);
g.drawString("w:"+width+" h:"+height,0,height-0,Graphics.LEFT | Graphics.TOP);
g.drawString("total memory:" + Runtime.getRuntime().totalMemory() + " byte",0,1,Graphics.LEFT | Graphics.TOP);
g.drawString("free memory:" + Runtime.getRuntime().freeMemory() + " byte",0,13,Graphics.LEFT | Graphics.TOP);
g.drawString("used memory:" + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) + " byte",0,25,Graphics.LEFT | Graphics.TOP);
//g.drawString("AlphaLevels:" + disp.numAlphaLevels(),0,height-47,Graphics.LEFT | Graphics.TOP);
//g.drawString("playerX:"+mainRole.getX()+" playerY:"+mainRole.getY(),1,14,Graphics.LEFT | Graphics.TOP);
//g.drawString("walking:"+walking,0,59,Graphics.LEFT | Graphics.TOP);
}
public void locateWindow()
{
windowX=GameMIDlet.mainRole.getX()-width/2;
windowY=GameMIDlet.mainRole.getY()-height/2;
if(windowX<0)
windowX=0;
if(windowY<0)
windowY=0;
if(windowX>groundWidth-width)
{
windowX=groundWidth-width;
}
if(windowY>groundHeight-height)
{
windowY=groundHeight-height;
}
if(roleLayer!=null)
roleLayer.setViewWindow(windowX,windowY,width,height);
if(backLayer!=null)
backLayer.setViewWindow(windowX,windowY,width,height);
if(frontLayer!=null)
frontLayer.setViewWindow(windowX,windowY,width,height);
}
public void scrollWindow()
{
locateWindow();
/*
int centerX=windowX+width/2;
int centerY=windowY+height/2;
if(centerX<mainRole.getPlayer().getX()-3)
{
windowX=windowX+3;
}
if(centerX>mainRole.getPlayer().getX()+3)
{
windowX=windowX-3;
}
if(centerY<mainRole.getPlayer().getY()-3)
{
windowY=windowY+3;
}
if(centerY>mainRole.getPlayer().getY()+3)
{
windowY=windowY-3;
}
if(windowX<0)windowX=0;
if(windowY<0)windowY=0;
if(windowX>groundWidth-width)
{
windowX=groundWidth-width;
}
if(windowY>groundHeight-height)
{
windowY=groundHeight-height;
}
roleLayer.setViewWindow(windowX,windowY,width,height);
sceneLayer.setViewWindow(windowX,windowY,width,height);
frontLayer.setViewWindow(windowX,windowY,width,height);
npcLayer.setViewWindow(windowX,windowY,width,height);
*/
}
int runTimes;
public void run()
{
ThreadRunning=true;
while(isPlay)
{
input();
redrawAll();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -