www.pudn.com > TXWW.rar > MyGameCanvas.java
package org.gamecollege.j2me.rpg;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.*;
import java.util.*;
/**
* @author Jagie
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class MyGameCanvas extends GameCanvas implements CommandListener,
Runnable {
/**
* 游戏MIDlet
*/
MIDlet midlet;
/**
* 游戏运行开关
*/
boolean isRunning;
/**
* 消息队列
*/
Vector messageQueue;
public static final int GAME_LOOP_INTERVAL = 100;
MyRPGGameMIDlet myRPGGameMIDlet;
public static final int STATUS_SHOWFLASH = 0;//显示flash
public static final int STATUS_POPMENU_CONFIRM = 1;//确认买卖
public static final int STATUS_BUYING = 11;//购买道具
public static final int STATUS_WALKING = 2;//主角行走
public static final int STATUS_FIGHTING = 3;////战斗
public static final int STATUS_MAIN_MENU = 4;//主菜单
public static final int STATUS_HELP = 5;//帮助
public static final int STATUS_ABOUT = 6;//关于
public static final int STATUS_PLAYER_INFO = 8;//主角属性
public static final int STATUS_MISSION_INFO = 9;//任务列表
public static final int STATUS_GAME_OVER = 10;//游戏结束
public static final int STATUS_LOADING_LEVEL = 12;//装载关卡
public static final int STATUS_SALING = 13;//卖东西
public static final int STATUS_FIGHT_OVER = 14;//战斗结束
public static final int STATUS_MISSION_OVER = 15;//任务结束
private int gameStatus = STATUS_SHOWFLASH;
//屏幕宽度,高度
int width, height;
//主角
Player myPlayer;
LayerManager lm;
//当前关卡号
int curLevelNo;
//1:买卖武器护具,2:药品买卖
int saleType;
//当前菜单项索引
int curMenuIndex;
//当前关卡
Level curLevel;
//可出售的武器集合
Vector weaponVec = new Vector();
//可出售的护具集合
Vector jacketVec = new Vector();
//可出售的药品集合
Vector medicVec = new Vector();
//购买武器护具时,区别到底是武器还是护具
boolean isArm;
LayerManager fightLayerManager;
GameInnerThread engine;
String curMessage;
Thread gameThread;
long lastPopMsgTime;
int beforeFightHp;
int beforeFightExp;
int beforeFightMp;
int beforeFightMoney;
int beforeFightHp_npc;
//是否在战斗时查看技能
boolean popSkill;
long lastFightOverTime;
long lastGameOverTime;
//在战斗中随机出现的敌人
NPC ranEnemy;
//战斗结束后获得的技能
Skill ranSkill;
Sprite fightBackGround;
int curMenuIndex2;
int killTigerCount;
GameStore gs;
boolean hasStored;
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#drawMessage(javax.microedition.lcdui.Graphics)
*/
public void commandAction(Command com, Displayable disp) {
if (com == List.SELECT_COMMAND) {
gs.deleteRecord();
myPlayer = new Player(ResourceLoader.StringResource[0]);
List list = (List) disp;
curLevelNo = list.getSelectedIndex();
curLevel = new Level();
LevelLoader.instance.loadLevel(curLevel);
Display.getDisplay(myRPGGameMIDlet).setCurrent(this);
} else if (com.getCommandType() == Command.BACK) {
this.setGameStatus(STATUS_MAIN_MENU);
Display.getDisplay(myRPGGameMIDlet).setCurrent(this);
}
}
public void showLevels() {
List list = new List(ResourceLoader.StringResource[25], Choice.IMPLICIT);
list.append(ResourceLoader.StringResource[24], null);
list.addCommand(new Command(ResourceLoader.StringResource[34],
Command.BACK, 1));
list.setCommandListener(this);
Display.getDisplay(myRPGGameMIDlet).setCurrent(list);
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#stopGame()
*/
public void stopGame() {
System.gc();
// TODO Auto-generated method stub
engine.stop();
//this.isRunning=false;
}
protected void drawMessage() {
Graphics g = this.getGraphics();
if (curMessage != null) {
g.setColor(255, 255, 128);
g.drawImage(ResourceLoader.menu[17], width / 2, height - 20,
Graphics.HCENTER | Graphics.TOP);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
g.drawString(curMessage, width / 2, height - 18, Graphics.HCENTER
| Graphics.TOP);
}
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#input()
*/
protected void input() {
int keyStates = getKeyStates();
switch (getGameStatus()) {
//主菜单状态
case STATUS_MAIN_MENU:
if (gs.canLoad()) {
handleMenuKeyBrowse(keyStates, 4);
} else {
handleMenuKeyBrowse(keyStates, 3);
}
if ((keyStates & FIRE_PRESSED) != 0) {
if (hasStored) {
switch (curMenuIndex) {
case 0:
showLevels();
break;
case 1:
loadGame();
break;
case 2:
setGameStatus(STATUS_ABOUT);
break;
case 3:
setGameStatus(STATUS_HELP);
break;
case 4:
MyRPGGameMIDlet.midlet.notifyDestroyed();
break;
}
} else {
switch (curMenuIndex) {
case 0:
showLevels();
break;
case 1:
setGameStatus(STATUS_ABOUT);
break;
case 2:
setGameStatus(STATUS_HELP);
break;
case 3:
MyRPGGameMIDlet.midlet.notifyDestroyed();
break;
}
}
}
break;
//帮助界面
case STATUS_HELP:
//关于界面
case STATUS_ABOUT:
if (keyStates != 0) {
setGameStatus(STATUS_MAIN_MENU);
}
break;
//行走界面
case STATUS_WALKING:
//按着左键
if ((keyStates & LEFT_PRESSED) != 0) {
myPlayer.move(Player.LEFT);
}
//按着右键
else if ((keyStates & RIGHT_PRESSED) != 0) {
myPlayer.move(Player.RIGHT);
}
//按着上键
else if ((keyStates & UP_PRESSED) != 0) {
myPlayer.move(Player.UP);
}
//按着下键
else if ((keyStates & DOWN_PRESSED) != 0) {
myPlayer.move(Player.DOWN);
} else {
//未按方向键
myPlayer.move(Player.STILL);
}
break;
//确认界面
case STATUS_POPMENU_CONFIRM:
handleMenuKeyBrowse(keyStates, 2);
if ((keyStates & FIRE_PRESSED) != 0) {
switch (curMenuIndex) {
case 0:
setGameStatus(STATUS_BUYING);
break;
case 1:
setGameStatus(STATUS_SALING);
break;
case 2:
setGameStatus(STATUS_WALKING);
break;
}
}
break;
//购买物品界面
case STATUS_BUYING:
int maxMenuIndex = 0;
Property pro = null;
switch (this.saleType) {
//武器护具
case 1:
//注意多了一个[退出]菜单
if (this.isArm) {
maxMenuIndex = this.weaponVec.size();
if (curMenuIndex < weaponVec.size()) {
pro = (Property) weaponVec.elementAt(curMenuIndex);
}
} else {
maxMenuIndex = this.jacketVec.size();
if (curMenuIndex < jacketVec.size()) {
pro = (Property) jacketVec.elementAt(curMenuIndex);
}
}
break;
//药品
case 2:
if (curMenuIndex < medicVec.size()) {
pro = (Property) medicVec.elementAt(curMenuIndex);
}
maxMenuIndex = this.medicVec.size();
break;
}
handleMenuKeyBrowse(keyStates, maxMenuIndex);
if ((keyStates & LEFT_PRESSED) != 0
|| (keyStates & RIGHT_PRESSED) != 0) {
isArm = !isArm;
} else if ((keyStates & FIRE_PRESSED) != 0) {
if (pro != null) {
if (pro.price <= myPlayer.money) {
myPlayer.addSth(pro);
myPlayer.money -= pro.price;
}
} else {
setGameStatus(STATUS_POPMENU_CONFIRM);
}
}
break;
//出售物品界面
case STATUS_SALING:
Vector ps = null;
if (this.saleType == 1) {
if (isArm) {
ps = myPlayer.weaponVec;
} else {
ps = myPlayer.jacketVec;
}
} else {
ps = myPlayer.medicVec;
}
maxMenuIndex = ps.size();
handleMenuKeyBrowse(keyStates, maxMenuIndex);
if ((keyStates & LEFT_PRESSED) != 0
|| (keyStates & RIGHT_PRESSED) != 0) {
isArm = !isArm;
} else if ((keyStates & FIRE_PRESSED) != 0) {
if (curMenuIndex < ps.size()) {
pro = (Property) ps.elementAt(curMenuIndex);
myPlayer.removeSth(pro);
//卖东西,只能得到80%的收益
myPlayer.money += pro.price * 8 / 10;
} else {
setGameStatus(STATUS_POPMENU_CONFIRM);
}
}
break;
//战斗界面
case STATUS_FIGHTING:
if ((keyStates & LEFT_PRESSED) != 0) {
popSkill = false;
curMenuIndex2 = 0;
curMenuIndex = curMenuIndex - 1 >= 0 ? curMenuIndex - 1 : 2;
} else if ((keyStates & RIGHT_PRESSED) != 0) {
popSkill = false;
curMenuIndex2 = 0;
curMenuIndex = curMenuIndex + 1 > 2 ? 0 : curMenuIndex + 1;
} else if ((keyStates & FIRE_PRESSED) != 0) {
if (!popSkill) {
switch (curMenuIndex) {
case 0:
if (myPlayer.attackStatus == -1) {
myPlayer.attackStatus = 0;
}
break;
case 1:
//显示技能列表
this.popSkill = true;
break;
case 2:
//退出战斗
synchronized (engine) {
this.setGameStatus(STATUS_WALKING);
this.ranEnemy = null;
myPlayer.attackStatus = -1;
}
break;
}
} else {
popSkill = false;
if (myPlayer.skillVec.size() > 0) {
fireSkill(curMenuIndex2);
}
}
} else {
this.handleMenuKeyBrowse2(keyStates,
myPlayer.skillVec.size() - 1);
}
break;
//主角属性界面
case STATUS_PLAYER_INFO:
Vector v = this.weaponVec;
switch (curMenuIndex) {
case 1:
v = myPlayer.weaponVec;
break;
case 2:
v = myPlayer.medicVec;
break;
case 3:
v = myPlayer.jacketVec;
break;
case 4:
v = myPlayer.skillVec;
break;
}
if ((keyStates & LEFT_PRESSED) != 0) {
curMenuIndex = curMenuIndex - 1 >= 0 ? curMenuIndex - 1 : 4;
} else if ((keyStates & RIGHT_PRESSED) != 0) {
curMenuIndex = curMenuIndex + 1 > 4 ? 0 : curMenuIndex + 1;
} else if ((keyStates & FIRE_PRESSED) != 0) {
if (curMenuIndex == 0) {
this.setGameStatus(STATUS_WALKING);
} else {
if (curMenuIndex2 < v.size() && curMenuIndex != 4) {
RPGObject ro = (RPGObject) v.elementAt(curMenuIndex2);
myPlayer.useObject(ro);
} else {
this.setGameStatus(STATUS_WALKING);
}
}
} else {
handleMenuKeyBrowse2(keyStates, v.size());
}
break;
}
}
private void fireSkill(int index) {
Skill sk = (Skill) myPlayer.skillVec.elementAt(index);
if (sk.requiredLevel <= myPlayer.rank && sk.hpCost <= myPlayer.hp
&& sk.mpCost <= myPlayer.mp) {
myPlayer.fireSkill = sk;
myPlayer.setAttackStatusSprite(1979);
}
}
public void handleGameOver() {
ranSkill = null;
this.ranEnemy = null;
myPlayer.attackStatus = -1;
myPlayer.hp = 10;
saveGame();
engine.stop();
lastGameOverTime = System.currentTimeMillis();
this.setGameStatus(STATUS_GAME_OVER);
}
/**
* 菜单上下遍历的通用方法
* @param keyStates 按键状态
* @param maxMenuIndex 最大的菜单索引
*/
private void handleMenuKeyBrowse(int keyStates, int maxMenuIndex) {
if ((keyStates & DOWN_PRESSED) != 0) {
curMenuIndex = curMenuIndex + 1 > maxMenuIndex ? 0
: curMenuIndex + 1;
}
if ((keyStates & UP_PRESSED) != 0) {
curMenuIndex = curMenuIndex - 1 < 0 ? maxMenuIndex
: curMenuIndex - 1;
}
}
private void handleMenuKeyBrowse2(int keyStates, int maxMenuIndex) {
if ((keyStates & DOWN_PRESSED) != 0) {
curMenuIndex2 = curMenuIndex2 + 1 > maxMenuIndex ? 0
: curMenuIndex2 + 1;
}
if ((keyStates & UP_PRESSED) != 0) {
curMenuIndex2 = curMenuIndex2 - 1 < 0 ? maxMenuIndex
: curMenuIndex2 - 1;
}
}
public void startGame() {
this.isRunning = true;
Thread t = new Thread(this);
this.gameThread = t;
t.start();
}
public void handleRanEnemy() {
if (this.ranEnemy != null) {
this.constructsFightMap();
this.setGameStatus(STATUS_FIGHTING);
//System.out.println("handleRanEnemy over");
//engine.ranEnemy = null;
}
}
public void handleFightOver() {
if (ranEnemy.ID == 3) {
this.killTigerCount++;
}
gameStatus = STATUS_FIGHT_OVER;
lastFightOverTime = System.currentTimeMillis();
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#render(javax.microedition.lcdui.Graphics)
*/
protected void render() {
Graphics g = this.getGraphics();
g.setColor(255, 255, 255);
g.fillRect(0, 0, width, height);
g.setColor(0, 0, 0);
switch (getGameStatus()) {
case STATUS_WALKING:
lm.paint(g, 0, 0);
drawMessage();
break;
case STATUS_LOADING_LEVEL:
drawLoadingLevel(g);
break;
case STATUS_POPMENU_CONFIRM:
lm.paint(g, 0, 0);
drawConfirmMenu(g);
break;
case STATUS_BUYING:
drawBuying(g);
break;
case STATUS_SALING:
drawSaling(g);
break;
case STATUS_FIGHTING:
drawFightScene(g);
break;
case STATUS_MAIN_MENU:
drawMainMenu(g);
break;
case STATUS_HELP:
drawHelp(g);
break;
case STATUS_ABOUT:
drawAbout(g);
break;
case STATUS_PLAYER_INFO:
drawPlayerInfo(g);
break;
case STATUS_MISSION_INFO:
drawMissonList(g);
break;
case STATUS_GAME_OVER:
drawGameOver(g);
break;
case STATUS_FIGHT_OVER:
drawFightOver(g);
break;
case STATUS_MISSION_OVER:
drawMissonOver(g);
break;
}
}
public void drawFlash() {
Graphics g = this.getGraphics();
if (ResourceLoader.flashImage != null) {
g.drawImage(ResourceLoader.flashImage, width / 2, height / 2,
Graphics.HCENTER | Graphics.VCENTER);
}
this.flushGraphics();
}
/**
* 绘制主角拥有的东西列表
*
* @param bType
* :1:武器 2:护具,3:药品 4:技能
*/
private void drawBelongs(int bType, Graphics g) {
int y1 = 10;
y1 += 36;
Vector ps = this.weaponVec;
switch (curMenuIndex) {
case 1:
ps = myPlayer.weaponVec;
break;
case 2:
ps = myPlayer.medicVec;
break;
case 3:
ps = myPlayer.jacketVec;
break;
case 4:
ps = myPlayer.skillVec;
break;
}
for (int i = 0; i < ps.size(); i++) {
RPGObject object = (RPGObject) ps.elementAt(i);
paintMenu(g, object.name, 66, y1, i == curMenuIndex2);
y1 += 18;
}
RPGObject ro = null;
if (curMenuIndex2 < ps.size()) {
ro = (RPGObject) ps.elementAt(curMenuIndex2);
}
if (ro != null) {
String allDesc = ro.description;
g.drawString(allDesc, width / 2, 208 - 15, Graphics.HCENTER
| Graphics.BOTTOM);
}
paintMenu(g, ResourceLoader.StringResource[3], 66, 193,
ps.size() == curMenuIndex2);
}
private void drawSaling(Graphics g) {
g.drawImage(ResourceLoader.menu[0], width / 2, 0, Graphics.TOP
| Graphics.HCENTER);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
switch (this.saleType) {
//武器护具
case 1:
int y1 = 10;
g.drawImage(ResourceLoader.menu[8], width / 2, y1, Graphics.TOP
| Graphics.HCENTER);
String type = ResourceLoader.StringResource[27];
if (!isArm) {
type = ResourceLoader.StringResource[28];
}
g.drawString(type, width / 2, y1 + 1, Graphics.TOP
| Graphics.HCENTER);
y1 += 18;
g.drawString(ResourceLoader.StringResource[29] + ":"
+ myPlayer.money, width / 2, y1, Graphics.TOP
| Graphics.HCENTER);
Vector ps = myPlayer.weaponVec;
if (!isArm) {
ps = myPlayer.jacketVec;
}
y1 += 18;
//武器护具列表
for (int i = 0; i < ps.size(); i++) {
Property property = (Property) ps.elementAt(i);
paintMenu(g, property.name, width / 2, y1, i == curMenuIndex);
y1 += 18;
}
//退出按钮
paintMenu(g, ResourceLoader.StringResource[3], width / 2, y1,
curMenuIndex == ps.size());
Property curP = null;
if (curMenuIndex < ps.size()) {
curP = (Property) ps.elementAt(curMenuIndex);
}
if (curP != null) {
String allDesc = ResourceLoader.StringResource[6] + ":"
+ curP.description + " "
+ ResourceLoader.StringResource[7] + ":" + curP.price;
g.drawString(allDesc, width / 2, 208, Graphics.HCENTER
| Graphics.BOTTOM);
}
break;
case 2:
//药品
int y = 10;
g.drawString(ResourceLoader.StringResource[29] + ":"
+ myPlayer.money, width / 2, y, Graphics.TOP
| Graphics.HCENTER);
ps = myPlayer.medicVec;
y += 18;
for (int i = 0; i < ps.size(); i++) {
Property property = (Property) ps.elementAt(i);
paintMenu(g, property.name, width / 2, y, i == curMenuIndex);
y += 18;
}
paintMenu(g, ResourceLoader.StringResource[3], width / 2, y,
curMenuIndex == ps.size());
curP = null;
if (ps.size() > curMenuIndex) {
curP = (Property) ps.elementAt(curMenuIndex);
}
if (curP != null) {
String allDesc = ResourceLoader.StringResource[6] + ":"
+ curP.description + " "
+ ResourceLoader.StringResource[7] + ":" + curP.price;
g.drawString(allDesc, width / 2, 208, Graphics.HCENTER
| Graphics.BOTTOM);
}
break;
}
}
/**
*
*/
private void drawMissonList(Graphics g) {
g.drawImage(ResourceLoader.menu[0], width / 2, 0, Graphics.TOP
| Graphics.HCENTER);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_LARGE));
g.drawString(ResourceLoader.StringResource[42], width / 2, 0,
Graphics.TOP | Graphics.HCENTER);
g.drawString(curLevel.missionDesc, width / 2, 40, Graphics.TOP
| Graphics.HCENTER);
}
private void drawMissonOver(Graphics g) {
g.drawImage(ResourceLoader.menu[0], width / 2, 0, Graphics.TOP
| Graphics.HCENTER);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_LARGE));
g.drawString(ResourceLoader.StringResource[40], width / 2,
height / 2 - 20, Graphics.TOP | Graphics.HCENTER);
//g.drawString(""+this.killTigerCount,40,40,0);
}
private void drawFightOver(Graphics g) {
fightLayerManager = new LayerManager();
fightLayerManager.append(this.fightBackGround);
fightLayerManager.paint(g, 0, 0);
g.setColor(255, 255, 255);
g.drawImage(ResourceLoader.menu[3], width / 2, height / 2,
Graphics.HCENTER | Graphics.VCENTER);
g.drawString(ResourceLoader.StringResource[30]
+ (myPlayer.exp - this.beforeFightExp), width / 2,
height / 2 - 40, Graphics.HCENTER | Graphics.TOP);
g.drawString(ResourceLoader.StringResource[31]
+ (myPlayer.money - this.beforeFightMoney), width / 2,
height / 2 - 20, Graphics.HCENTER | Graphics.TOP);
//升级了
if (myPlayer.exp >= myPlayer.nextExp) {
g.drawString(ResourceLoader.StringResource[39], width / 2,
height / 2, Graphics.HCENTER | Graphics.TOP);
}
//学会技能
if (ranSkill != null) {
g
.drawString(ResourceLoader.StringResource[41]
+ ranSkill.name, width / 2, height / 2 + 20,
Graphics.HCENTER | Graphics.TOP);
}
}
/**
*
*/
private void drawPlayerInfo(Graphics g) {
// TODO 自动生成方法存根
g.drawImage(ResourceLoader.menu[0], 0, 0, 0);
g.setColor(255, 255, 255);
g
.drawImage(ResourceLoader.menu[8], 30, 0, Graphics.TOP
| Graphics.LEFT);
g.setClip(118, 0, 58, 52);
g.drawImage(ResourceLoader.menu[4], 118, 0, 0);
g.setClip(0, 0, width, height);
String type = type = ResourceLoader.StringResource[38];//一般
switch (curMenuIndex) {
//通常信息
case 0:
int xx = 8,
yy = 30;
int interval = 50;
g.drawString(ResourceLoader.StringResource[20], xx, yy, 0);
g.drawString(myPlayer.rank + "", xx + interval, yy, 0);
g.drawString(ResourceLoader.StringResource[21], xx, yy + 15, 0);
g.drawString(myPlayer.exp + "", xx + interval, yy + 15, 0);
g.drawString(ResourceLoader.StringResource[22], xx, yy + 30, 0);
g.drawString(myPlayer.nextExp + "", xx + interval, yy + 30, 0);
g.drawString("HP", xx, yy + 45, 0);
g.drawString(myPlayer.hp + "", xx + interval, yy + 45, 0);
g.drawString("MP", xx, yy + 60, 0);
g.drawString(myPlayer.mp + "", xx + interval, yy + 60, 0);
Property w = (Property) curLevel.properties.get(new Integer(
myPlayer.curWeaponID));
Property d = (Property) curLevel.properties.get(new Integer(
myPlayer.curDefendID));
g.drawString(ResourceLoader.StringResource[32], xx, yy + 75, 0);
g.drawString(myPlayer.attackPoint + "", xx + interval, yy + 75, 0);
g.drawString(ResourceLoader.StringResource[33], xx, yy + 90, 0);
g.drawString(myPlayer.defendPoint + "", xx + interval, yy + 90, 0);
g.drawString(ResourceLoader.StringResource[35], xx, yy + 105, 0);
g.drawString(myPlayer.money + "", xx + interval, yy + 105, 0);
g.drawString(ResourceLoader.StringResource[27], xx, yy + 120, 0);
if (w != null) {
g.drawString(w.name + "(AP:" + w.addAttackPoint + ",DP:"
+ w.addDefendPoint + ")", xx + interval, yy + 120, 0);
}
g.drawString(ResourceLoader.StringResource[28], xx, yy + 135, 0);
if (d != null) {
g.drawString(d.name + "(AP:" + d.addAttackPoint + ",DP:"
+ d.addDefendPoint + ")", xx + interval, yy + 135, 0);
}
paintMenu(g, ResourceLoader.StringResource[3], 66, 193, true);
break;
case 1:
type = ResourceLoader.StringResource[27];//武器
drawBelongs(1, g);
break;
case 2:
type = ResourceLoader.StringResource[36];//药品
drawBelongs(2, g);
break;
case 3:
type = ResourceLoader.StringResource[28];//护具
drawBelongs(3, g);
break;
case 4:
type = ResourceLoader.StringResource[37];//技能
drawBelongs(4, g);
break;
}
g.drawString(type, 63, 0, Graphics.TOP | Graphics.HCENTER);
}
private void drawLoadingLevel(Graphics g) {
g.drawImage(ResourceLoader.menu[0], width / 2, height / 2,
Graphics.HCENTER | Graphics.VCENTER);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
Font.SIZE_LARGE));
g.drawString(ResourceLoader.StringResource[26], width / 2, 90,
Graphics.HCENTER | Graphics.TOP);
}
/**
*
*/
private void drawAbout(Graphics g) {
// TODO 自动生成方法存根
g.drawImage(ResourceLoader.menu[0], 0, 0, 0);
g.drawImage(ResourceLoader.menu[3], width / 2, 63, Graphics.HCENTER
| Graphics.TOP);
g.setColor(255, 255, 255);
g.drawString(ResourceLoader.StringResource[17], width / 2, 90,
Graphics.HCENTER | Graphics.TOP);
g.drawString(ResourceLoader.StringResource[18], width / 2, 110,
Graphics.HCENTER | Graphics.TOP);
g.drawString(ResourceLoader.StringResource[19], width / 2, 150,
Graphics.HCENTER | Graphics.TOP);
}
/**
*
*/
private void drawHelp(Graphics g) {
// TODO 自动生成方法存根
g.drawImage(ResourceLoader.menu[0], 0, 0, 0);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
g.drawString(ResourceLoader.StringResource[16], width / 2, 10,
Graphics.HCENTER | Graphics.TOP);
g.drawString(ResourceLoader.StringResource[43], width / 2, 30,
Graphics.HCENTER | Graphics.TOP);
g.drawString(ResourceLoader.StringResource[44], width / 2, 50,
Graphics.HCENTER | Graphics.TOP);
g.drawString(ResourceLoader.StringResource[45], width / 2, 70,
Graphics.HCENTER | Graphics.TOP);
}
/**
*
*/
private void drawMainMenu(Graphics g) {
// TODO 自动生成方法存根
g.setColor(255, 255, 255);
g.drawImage(ResourceLoader.menu[0], width / 2, 0, Graphics.HCENTER
| Graphics.TOP);
g.drawImage(ResourceLoader.menu[9], width / 2, 0, Graphics.HCENTER
| Graphics.TOP);
this.paintMenu(g, ResourceLoader.StringResource[11], width / 2, 80,
curMenuIndex == 0);
if (hasStored) {
this.paintMenu(g, ResourceLoader.StringResource[12], width / 2, 96,
curMenuIndex == 1);
this.paintMenu(g, ResourceLoader.StringResource[13], width / 2,
112, curMenuIndex == 2);
this.paintMenu(g, ResourceLoader.StringResource[14], width / 2,
128, curMenuIndex == 3);
this.paintMenu(g, ResourceLoader.StringResource[15], width / 2,
144, curMenuIndex == 4);
} else {
this.paintMenu(g, ResourceLoader.StringResource[13], width / 2, 96,
curMenuIndex == 1);
this.paintMenu(g, ResourceLoader.StringResource[14], width / 2,
112, curMenuIndex == 2);
this.paintMenu(g, ResourceLoader.StringResource[15], width / 2,
128, curMenuIndex == 3);
}
}
public void constructsFightMap() {
fightLayerManager = new LayerManager();
fightLayerManager.append(myPlayer
.getSprite(Player.STATUS_BEFORE_ATTACK));
fightLayerManager.append(this.ranEnemy.sprite);
fightBackGround = engine.getRandomFightBg();
fightLayerManager.append(fightBackGround);
this.beforeFightHp = myPlayer.hp;
this.beforeFightMp = myPlayer.mp;
this.beforeFightExp = myPlayer.exp;
this.beforeFightMoney = myPlayer.money;
this.beforeFightHp_npc = this.ranEnemy.hp;
curMenuIndex = 0;
}
/**
*
*/
private void drawFightScene(Graphics g) {
fightLayerManager.paint(g, 0, 0);
g.drawImage(ResourceLoader.menu[11], 0, 208, Graphics.LEFT
| Graphics.BOTTOM);
g.drawImage(ResourceLoader.menu[10], 176, 0, Graphics.RIGHT
| Graphics.TOP);
g.setColor(255, 255, 255);
g.drawString("HP:", 7, 175, 0);
g.drawString("MP:", 7, 195, 0);
g.drawString(this.ranEnemy.name, 120, 5, 0);
//
g.setColor(255, 0, 0);
g.drawRect(130, 22, 30, 5);
g.fillRect(130, 22, this.ranEnemy.hp * 30 / this.beforeFightHp_npc, 5);
g.drawRect(30, 177, 30, 5);
g.drawRect(30, 197, 30, 5);
g.fillRect(30, 177, myPlayer.hp * 30 / this.beforeFightHp, 5);
g.fillRect(30, 197, myPlayer.mp * 30 / this.beforeFightMp, 5);
if (!popSkill) {
g
.drawImage(ResourceLoader.menu[12], 73 + curMenuIndex * 33,
172, 0);
} else {
g.drawImage(ResourceLoader.menu[13], 0, 0, 0);
Vector skills = myPlayer.skillVec;
int x = 42;
int y = 20;
g.setColor(255, 255, 255);
if (skills.size() > 0) {
Skill skill = null;
for (int i = 0; i < skills.size(); i++) {
skill = (Skill) skills.elementAt(i);
paintMenu(g, skill.name, x + 5, y, curMenuIndex2 == i);
y += 16;
}
}
}
}
/**
*
*/
private void drawGameOver(Graphics g) {
g.drawImage(ResourceLoader.menu[3], getWidth() / 2, 60,
Graphics.HCENTER | Graphics.TOP);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
g.drawString(ResourceLoader.StringResource[23], width / 2, 90,
Graphics.HCENTER | Graphics.TOP);
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#tick()
*/
protected void tick() {
if (getGameStatus() == STATUS_WALKING) {
Map map = this.curLevel.curMap;
int mapWidth = map.cols * curLevel.tileWidth;
int mapHeight = map.rows * curLevel.tileHeight;
Sprite walkSprite = myPlayer.getSprite(Player.STATUS_WALK);
//防止英雄走出地图
if (walkSprite.getX() < 0
|| walkSprite.getX() + walkSprite.getWidth() > mapWidth) {
myPlayer.undoMove();
}
if (walkSprite.getY() < 0
|| walkSprite.getY() + walkSprite.getHeight() > mapHeight) {
myPlayer.undoMove();
}
//地图障碍
TiledLayer ctl = map.collisionArea;
if (walkSprite.collidesWith(ctl, true)) {
myPlayer.undoMove();
}
//碰到NPC和道具
Vector objs = map.rpgObjects;
for (int i = 0; i < objs.size(); i++) {
RPGObject obj = (RPGObject) objs.elementAt(i);
if (obj.sprite.collidesWith(walkSprite, true)) {
if (obj instanceof NPC) {
handleCrashNpc((NPC) obj);
} else if (obj instanceof Property) {
Property p = (Property) obj;
handleCrashProperty(p);
}
}
}
//碰到连接区,地图转移
Enumeration e = curLevel.links.elements();
while (e.hasMoreElements()) {
Linker linker = (Linker) e.nextElement();
if (linker.belongMapID == map.mapNo
&& linker.crashHero(myPlayer, curLevel.tileWidth,
curLevel.tileHeight)) {
curLevel.changeMap(linker, myPlayer);
break;
}
}
//视窗移动,ox,oy为主角中心位置
int ox = walkSprite.getX()+walkSprite.getWidth()/2 - width / 2;
int oy = walkSprite.getY()+walkSprite.getHeight()/2 - height / 2;
if (ox < 0) {
ox = 0;
}
if (oy < 0) {
oy = 0;
}
if (ox + width > mapWidth) {
ox = mapWidth - width;
}
if (oy + height > mapHeight) {
oy = mapHeight - height;
}
lm.setViewWindow(ox, oy, width, height);
//取下一条消息
if (System.currentTimeMillis() - lastPopMsgTime > 1000) {
this.curMessage = this.popMessage();
lastPopMsgTime = System.currentTimeMillis();
}
} else if (gameStatus == STATUS_FIGHT_OVER) {
synchronized (engine) {
if (System.currentTimeMillis() - lastFightOverTime > 2000) {
//升级了
if (myPlayer.exp >= myPlayer.nextExp) {
myPlayer.nextExp = myPlayer.exp * 3 / 2;
myPlayer.rank++;
}
//学会技能了
if (ranSkill != null) {
myPlayer.skillVec.addElement(ranSkill);
}
ranSkill = null;
this.ranEnemy = null;
myPlayer.attackStatus = -1;
if (curLevel.isCompleted()) {
myPlayer = new Player(ResourceLoader.StringResource[0]);
ranSkill = null;
this.ranEnemy = null;
this.setGameStatus(STATUS_MISSION_OVER);
} else {
this.setGameStatus(STATUS_WALKING);
}
}
}
} else if (gameStatus == STATUS_GAME_OVER
&& System.currentTimeMillis() - lastGameOverTime > 2000) {
this.setGameStatus(STATUS_MAIN_MENU);
}
}
private void handleCrashNpc(NPC npc) {
switch (npc.type) {
//对话类NPC
case 1:
//改变可对话NPC的朝向
changeNpcDirect(npc, myPlayer);
//主角undo
myPlayer.undoMove();
//在对话队列里面加入谈话内容
for (int i = 0; i < npc.talkContents.size(); i++) {
String t = (String) npc.talkContents.elementAt(i);
int index = t.indexOf('_');
String nt = t.substring(0, index).trim();
String pt = t.substring(index + 1).trim();
//第一次碰上
if (!npc.isAgainCrash) {
if (!nt.equals("null")) {
String temp = nt;
if (nt.startsWith("$")) {
temp = nt.substring(1);
}
pushMessage(npc.name + ":" + temp);
}
if (!pt.equals("null")) {
pushMessage(myPlayer.name + ":" + pt);
}
} else {
//非第一次碰上
if (nt.startsWith("$")) {
pushMessage(npc.name + ":" + nt.substring(1));
if (!pt.equals("null")) {
pushMessage(myPlayer.name + ":" + pt);
}
}
}
}
npc.isAgainCrash = true;
break;
//武器护具贩卖型NPC
case 2:
changeNpcDirect(npc, myPlayer);
myPlayer.undoMove();
this.saleType = 1;
this.weaponVec = npc.weaponVec;
this.jacketVec = npc.jacketVec;
setGameStatus(STATUS_POPMENU_CONFIRM);
break;
case 3:
//药品贩卖类型NPC
changeNpcDirect(npc, myPlayer);
myPlayer.undoMove();
this.saleType = 2;
this.medicVec = npc.medicVec;
setGameStatus(STATUS_POPMENU_CONFIRM);
break;
case 4:
break;
case 5:
break;
}
}
private void changeNpcDirect(NPC npc, Player player) {
if (npc.type == 1) {
int dir = Tools.comparePos(player.getSprite(Player.STATUS_WALK),
npc.sprite);
//英雄在NPC的左边
if (dir == Canvas.LEFT) {
if (npc.startFrameIndex + 2 <= npc.endFrameIndex) {
npc.sprite.setFrame(npc.startFrameIndex + 2);
}
//英雄在NPC的上边
} else if (dir == Canvas.UP) {
if (npc.startFrameIndex + 1 <= npc.endFrameIndex) {
npc.sprite.setFrame(npc.startFrameIndex + 1);
}
//英雄在NPC右边
} else if (dir == Canvas.RIGHT) {
if (npc.startFrameIndex + 3 <= npc.endFrameIndex) {
npc.sprite.setFrame(npc.startFrameIndex + 3);
}
} else {
npc.sprite.setFrame(npc.startFrameIndex);
}
}
}
private void handleCrashProperty(Property pro) {
}
/**
* 往消息队列中添加消息
*
* @param message
* 要添加到消息队列中的消息
*/
public void pushMessage(String message) {
if (messageQueue == null) {
messageQueue = new Vector();
}
if (message != null && message.trim().length() > 0) {
messageQueue.addElement(message);
}
}
/**
* 从消息队列中取消息
*
* @return 从消息队列中取第一条消息
*/
public String popMessage() {
String message = null;
if (messageQueue != null && messageQueue.size() > 0) {
message = (String) messageQueue.elementAt(0);
messageQueue.removeElementAt(0);
}
return message;
}
/**
* 游戏主循环
*/
public void run() {
while (isRunning && this.gameThread == Thread.currentThread()) {
long start = System.currentTimeMillis();
//只有MyGameCanvas是当前Displayable对象才进行
if (this.isShown()) {
//处理输入
input();
//调整
tick();
//绘制
render();
//输出
flushGraphics();
}
long end = System.currentTimeMillis();
int duration = (int) (end - start);
if (duration < GAME_LOOP_INTERVAL) {
try {
Thread.sleep(GAME_LOOP_INTERVAL - duration);
} catch (InterruptedException ie) {
}
}
}
}
public MyGameCanvas(MIDlet _midlet) {
super(false);
this.midlet = _midlet;
width = getWidth();
height = getHeight();
this.myRPGGameMIDlet = (MyRPGGameMIDlet) _midlet;
this.setFullScreenMode(true);
gs = new GameStore(this);
new InnerThread().start();
}
public void constructMap() {
Level level = this.curLevel;
Map map = level.curMap;
int hc = map.heroAppearCol;
int hr = map.heroAppearRow;
int tw = level.tileWidth;
int th = level.tileHeight;
if (myPlayer.row == -1 || myPlayer.col == -1) {
myPlayer.getSprite(Player.STATUS_WALK)
.setPosition(hc * tw, hr * th);
} else {
myPlayer.getSprite(Player.STATUS_WALK).setPosition(
myPlayer.col * tw, myPlayer.row * th);
}
lm = new LayerManager();
//添加英雄
lm.append(myPlayer.getSprite(Player.STATUS_WALK));
//添加NPC和道具
Vector v = map.rpgObjects;
for (int i = 0; i < v.size(); i++) {
RPGObject obj = (RPGObject) v.elementAt(i);
obj.sprite.setPosition(obj.colNo * tw, obj.rowNo * th);
obj.sprite.setFrame(obj.startFrameIndex);
lm.append(obj.sprite);
}
lm.append(map.collisionArea);
for (int i = map.walkableArea.size() - 1; i >= 0; i--) {
TiledLayer tl = (TiledLayer) map.walkableArea.elementAt(i);
lm.append(tl);
}
}
public void saveGame() {
gs.saveGame();
}
public void handLevelLoaded() {
curLevel.curMap = (Map) curLevel.maps
.get(new Integer(curLevel.curMapNo));
constructMap();
setGameStatus(MyGameCanvas.STATUS_WALKING);
engine = new GameInnerThread(MyRPGGameMIDlet.mc);
engine.start();
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#loadGame()
*/
public void loadGame() {
myPlayer = new Player(ResourceLoader.StringResource[0]);
if (gs.canLoad()) {
gs.loadGame();
}
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#pauseGame()
*/
public void pauseGame() {
this.isRunning = false;
}
/*
* (non-Javadoc)
*
* @see org.gamecollege.j2me.rpg.RPGGameCanvas#stopGame()
*/
public void sizeChanged(int w, int h) {
width = w;
height = h;
// startGame();
// new InnerThread().start();
}
public void drawSubImg(Graphics g, Image img, int x, int y, int swidth,
int sheight) {
g.setClip(x, y, swidth, sheight);
g.drawImage(img, x - swidth, y - sheight, 0);
g.setClip(0, 0, width, height);
}
private void drawConfirmMenu(Graphics g) {
g.drawImage(ResourceLoader.menu[3], width / 2, height / 2,
Graphics.HCENTER | Graphics.VCENTER);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
switch (this.saleType) {
case 1:
//确认是否买卖武器
paintMenu(g, ResourceLoader.StringResource[1], width / 2, 80,
curMenuIndex == 0);
paintMenu(g, ResourceLoader.StringResource[2], width / 2, 100,
curMenuIndex == 1);
paintMenu(g, ResourceLoader.StringResource[3], width / 2, 120,
curMenuIndex == 2);
break;
case 2:
//确认是否买卖药品
paintMenu(g, ResourceLoader.StringResource[4], width / 2, 80,
curMenuIndex == 0);
paintMenu(g, ResourceLoader.StringResource[5], width / 2, 100,
curMenuIndex == 1);
paintMenu(g, ResourceLoader.StringResource[3], width / 2, 120,
curMenuIndex == 2);
break;
}
}
private void drawBuying(Graphics g) {
g.drawImage(ResourceLoader.menu[0], width / 2, 0, Graphics.TOP
| Graphics.HCENTER);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
switch (this.saleType) {
case 1:
int y1 = 10;
g.drawImage(ResourceLoader.menu[8], width / 2, y1, Graphics.TOP
| Graphics.HCENTER);
String type = ResourceLoader.StringResource[27];
if (!isArm) {
type = ResourceLoader.StringResource[28];
}
g.drawString(type, width / 2, y1 + 1, Graphics.TOP
| Graphics.HCENTER);
y1 += 18;
g.drawString(ResourceLoader.StringResource[29] + ":"
+ myPlayer.money, width / 2, y1, Graphics.TOP
| Graphics.HCENTER);
Vector ps = weaponVec;
if (!isArm) {
ps = jacketVec;
}
y1 += 18;
//武器护具列表
for (int i = 0; i < ps.size(); i++) {
Property property = (Property) ps.elementAt(i);
paintMenu(g, property.name, width / 2, y1, i == curMenuIndex);
y1 += 18;
}
//退出按钮
paintMenu(g, ResourceLoader.StringResource[3], width / 2, y1,
curMenuIndex == jacketVec.size());
Property curP = null;
if (isArm == false) {
if (curMenuIndex < jacketVec.size()) {
curP = (Property) jacketVec.elementAt(curMenuIndex);
}
} else {
if (curMenuIndex < weaponVec.size()) {
curP = (Property) weaponVec.elementAt(curMenuIndex);
}
}
if (curP != null) {
String allDesc = ResourceLoader.StringResource[6] + ":"
+ curP.description + " "
+ ResourceLoader.StringResource[7] + ":" + curP.price;
g.drawString(allDesc, width / 2, 208, Graphics.HCENTER
| Graphics.BOTTOM);
}
break;
case 2:
//药品
int y = 10;
g.drawString(ResourceLoader.StringResource[29] + ":"
+ myPlayer.money, width / 2, y, Graphics.TOP
| Graphics.HCENTER);
y += 18;
for (int i = 0; i < this.medicVec.size(); i++) {
Property property = (Property) medicVec.elementAt(i);
paintMenu(g, property.name, width / 2, y, i == curMenuIndex);
y += 18;
}
paintMenu(g, ResourceLoader.StringResource[3], width / 2, y,
curMenuIndex == medicVec.size());
curP = null;
if (medicVec.size() > curMenuIndex) {
curP = (Property) medicVec.elementAt(curMenuIndex);
}
if (curP != null) {
String allDesc = ResourceLoader.StringResource[6] + ":"
+ curP.description + " "
+ ResourceLoader.StringResource[7] + ":" + curP.price;
g.drawString(allDesc, width / 2, 208, Graphics.HCENTER
| Graphics.BOTTOM);
}
break;
}
}
/**
*
*/
protected void keyPressed(int keycode) {
switch (gameStatus) {
case STATUS_WALKING:
switch (keycode) {
case Canvas.KEY_NUM7:
this.setGameStatus(STATUS_PLAYER_INFO);
break;
case Canvas.KEY_NUM9:
this.setGameStatus(STATUS_MISSION_INFO);
break;
case Canvas.KEY_NUM0:
this.stopGame();
setGameStatus(STATUS_MAIN_MENU);
break;
}
break;
case STATUS_MISSION_INFO:
this.setGameStatus(STATUS_WALKING);
break;
case STATUS_MISSION_OVER:
this.stopGame();
this.setGameStatus(STATUS_MAIN_MENU);
break;
}
}
/**
* 绘制一个菜单项
* @param g Graphics 对象
* @param menu 菜单内容
* @param x x坐标位置
* @param y y坐标位置
* @param isCurrent 菜单项是否为当前菜单项
*/
private void paintMenu(Graphics g, String menu, int x, int y,
boolean isCurrent) {
g.drawImage(ResourceLoader.menu[1], x, y, Graphics.HCENTER
| Graphics.TOP);
g.setColor(255, 255, 255);
g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,
Font.SIZE_SMALL));
g.drawString(menu, x, y + 1, Graphics.HCENTER | Graphics.TOP);
if (isCurrent) {
g.drawImage(ResourceLoader.menu[2], x, y + 1, Graphics.HCENTER
| Graphics.TOP);
}
}
/**
* @param gameStatus
* The gameStatus to set.
*/
void setGameStatus(int gameStatus) {
if (gameStatus == STATUS_BUYING || gameStatus == STATUS_POPMENU_CONFIRM
|| gameStatus == STATUS_SALING
|| gameStatus == STATUS_PLAYER_INFO) {
curMenuIndex = 0;
} else if (gameStatus == STATUS_MAIN_MENU) {
hasStored = gs.canLoad();
}
this.gameStatus = gameStatus;
}
/**
* @return Returns the gameStatus.
*/
int getGameStatus() {
return gameStatus;
}
class InnerThread extends Thread {
public void run() {
//绘制Flash画面
drawFlash();
//资源装载器
ResourceLoader rl = new ResourceLoader();
//开始装载资源
rl.start();
//循环直到所有资源装载完毕
while (true) {
if (rl.loadOver) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//资源装载完毕,出现主菜单画面
setGameStatus(MyGameCanvas.STATUS_MAIN_MENU);
//开启游戏主循环线程
MyGameCanvas.this.startGame();
}
}
}