📄 hero.java
字号:
package com.xiaoyu.rpggame;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;
import java.io.IOException;
import java.util.Vector;
public class Hero {
int mp, hp;
int at;// 攻击力
int ma;//灵力--决定魔法攻击力
int def;
int level;
int exp;
String name;
String brand;
String[] magic;
int mpTotal, hpTotal;
// 当前动作
byte curAction[];
// 当前播放的动画索引
byte curAniIndex;
static int curDialogIndex;
// 人物坐标
int x, y;
int fx, fy;
byte stepLength; // 步长
int magicIndex;
// 寻找敌人时所走得步数
byte goSteps;
// 人物图片得长,宽
int playerWidth, playerHeight;
// 动画图片
Image pest = null;
Image head = null;
//魔法图片
Image magic1 = null;
Image magic2 = null;
Image curMagic = null;
//魔法图片长,宽
int magicWidth;
int magicHeight;
// 屏幕对象
GameView m_View;
// 人物道具和任务容器
Vector tools;
public Hero(GameView view) {
try {
pest = Image.createImage("/player4.png");
// head = Image.createImage("/playerHead.png");
magic1 = Image.createImage("/magic1.png");
magic2 = Image.createImage("/magic2.png");
} catch (IOException e) {
e.printStackTrace();
}
m_View = view;
playerWidth = pest.getWidth() / 4;
playerHeight = pest.getHeight() / 4;
magicWidth = magic1.getWidth() / 5;
magicHeight = magic1.getHeight() / 4;
x = 90;
y = 50;
fx = Const.HERO_X;
fy = Const.HERO_Y;
mpTotal = mp = Const.HERO_MP;
hpTotal = hp = Const.HERO_HP;
at = Const.HERO_AT;
ma = 5;
def = 5;
level = 1;
name = "天才小鱼";
brand = "无门无派";
stepLength = 4;
goSteps = Const.MOVE_STEPS;
magic = Const.MAGIC_NAME;
curAction = new byte[] { Const.HERO_STAND_FRAME[2] };
tools = new Vector();
for(byte i=0; i<Const.TOOLS_NAME.length; i++) {
tools.addElement(new Tool(i));
}
}
//升级
public boolean levelUp() {
if(level <= 10) {
if(exp >= level * 10) {
exp = exp - level * 10;
level++;
def += 2;
at += 5;
ma += 1;
hpTotal += 20;
mpTotal += 5;
mp = mpTotal;
hp = hpTotal;
return true;
}
} else {
if(exp >= level * 20) {
exp = exp - level * 20;
level++;
def += 4;
at += 10;
ma += 1;
hpTotal += 30;
mpTotal += 10;
mp = mpTotal;
hp = hpTotal;
return true;
}
}
return false;
}
// 移动
public void move() {
int bx = x;
int by = y;
switch (m_View.curKey) {
case Const.KEY_LEFT:
bx = bx - stepLength < 0 ? 0 : bx - stepLength;
break;
case Const.KEY_UP:
by = by - stepLength < 0 ? 0 : by - stepLength;
break;
case Const.KEY_RIGHT:
bx = bx + playerWidth + stepLength > m_View.maxWidth ? m_View.maxWidth
- playerWidth
: x + stepLength;
break;
case Const.KEY_DOWN:
by = by + playerHeight + stepLength > m_View.maxHeight ? m_View.maxHeight
- playerHeight
: y + stepLength;
break;
}
if (block(bx, by)) {
return;
}
x = bx;
y = by;
if(x >= (m_View.mapTrigger[0] - 1) * m_View.MAP_TILE_SIZE && x <= m_View.mapTrigger[0] * m_View.MAP_TILE_SIZE
&& y >= (m_View.mapTrigger[1] - 1) * m_View.MAP_TILE_SIZE && y <= m_View.mapTrigger[1] * m_View.MAP_TILE_SIZE)
m_View.ChangeMap(m_View.mapTrigger[2]);
}
int[] blocks = { 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 18, 19, 20, 21 };
public boolean checkBlock(int ix, int iy) {
for (int i = 0; i < blocks.length; i++) {
if (m_View.mapData[iy][ix] == blocks[i]) {
return true;
}
}
return false;
}
// 地图碰撞检测
public boolean block(int x, int y) {
int mx = 0;
int my = 0;
// 将当前坐标转换为在地图上的索引
switch (m_View.curKey) {
case Const.KEY_LEFT:
// 左上角
mx = x / m_View.MAP_TILE_SIZE;
// my = y / m_View.MAP_TILE_SIZE;
my = (y + 35) / m_View.MAP_TILE_SIZE;
if (checkBlock(mx, my)) {
return true;
}
// 左下角
mx = x / m_View.MAP_TILE_SIZE;
// my = (y + playerHeight - 3) / m_View.MAP_TILE_SIZE;
my = (y + playerHeight - 3 - 15) / m_View.MAP_TILE_SIZE;
if (checkBlock(mx, my)) {
return true;
}
break;
case Const.KEY_UP:
// 左上角
mx = x / m_View.MAP_TILE_SIZE;
my = (y + 15) / m_View.MAP_TILE_SIZE;
// my = y / m_View.MAP_TILE_SIZE;
if (checkBlock(mx, my)) {
return true;
}
// 右上角
mx = (x + playerWidth - 3) / m_View.MAP_TILE_SIZE;
my = (y + 15) / m_View.MAP_TILE_SIZE;
// my = y / m_View.MAP_TILE_SIZE;
if (checkBlock(mx, my)) {
return true;
}
break;
case Const.KEY_RIGHT:
// 右上角
mx = (x + playerWidth - 3) / m_View.MAP_TILE_SIZE;
// my = y / m_View.MAP_TILE_SIZE;
my = (y + 35) / m_View.MAP_TILE_SIZE;
if (checkBlock(mx, my)) {
return true;
}
// 右下角
mx = (x + playerWidth - 3) / m_View.MAP_TILE_SIZE;
// my = (y + playerHeight - 3) / m_View.MAP_TILE_SIZE;
my = (y + playerHeight - 3 - 15) / m_View.MAP_TILE_SIZE;
if (checkBlock(mx, my)) {
return true;
}
break;
case Const.KEY_DOWN:
// 左下角
mx = x / m_View.MAP_TILE_SIZE;
my = (y + playerHeight - 3) / m_View.MAP_TILE_SIZE;
if (checkBlock(mx, my)) {
return true;
}
// 右下角
mx = (x + playerWidth - 3) / m_View.MAP_TILE_SIZE;
my = (y + playerHeight - 3) / m_View.MAP_TILE_SIZE;
if (checkBlock(mx, my)) {
return true;
}
break;
}
return false;
}
// 攻击后返回
public boolean BackHome() {
this.curAction = Const.HERO_RIGHT_FRAME;
this.fx += Const.MOVE_STEP_X;
if (this.fy < Const.HERO_Y) {
this.fy += Const.MOVE_STEP_Y;
} else if (this.fy > Const.HERO_Y) {
this.fy -= Const.MOVE_STEP_Y;
}
goSteps--;
if (goSteps <= 0) {
goSteps = Const.MOVE_STEPS;
this.curAction = Const.HERO_LEFT_FRAME;
return true;
}
return false;
}
// 战斗时寻找敌人
public boolean FindEnemy(Enemy e) {
this.fx -= Const.MOVE_STEP_X;
if (this.fy < e.fy) {
this.fy += Const.MOVE_STEP_Y;
} else if (this.fy > e.fy) {
this.fy -= Const.MOVE_STEP_Y;
}
goSteps--;
if (goSteps <= 0) {
goSteps = Const.MOVE_STEPS;
return true;
}
return false;
}
/**
* 绘制人物动画
*
* @param g
* Graphics
*/
public void drawAnimation(Graphics g) {
curAniIndex = ++curAniIndex >= curAction.length ? 0 : curAniIndex;
// m_View.drawClipImg(x, y, playerWidth, playerHeight,
// curAction[curAniIndex]%3*playerWidth,
// curAction[curAniIndex]/3*playerHeight, pest, g);
m_View.drawClipImg(x - m_View.sx, y - m_View.sy, playerWidth,
playerHeight, curAction[curAniIndex] % 4 * playerWidth,
curAction[curAniIndex] / 4 * playerHeight, pest, g);
// move();
}
/**
* 绘制人物再战斗中动画
*
* @param g
* Graphics
*/
public void drawAnimationFight(Graphics g) {
curAniIndex = ++curAniIndex >= curAction.length ? 0 : curAniIndex;
m_View.drawClipImg(fx, fy, playerWidth, playerHeight,
curAction[curAniIndex] % 4 * playerWidth,
curAction[curAniIndex] / 4 * playerHeight, pest, g);
g.setColor(0);
g.drawRect(fx, fy - 6, playerWidth, 3);
g.setColor(0xff0000);
g.fillRect(fx, fy - 6, playerWidth * hp / hpTotal, 3);
g.setColor(0xffffff);
g.drawRect(fx, fy - 3, playerWidth, 3);
g.setColor(0x00ff00);
g.fillRect(fx, fy - 3, playerWidth * mp / mpTotal, 3);
}
// 使用物品
public void useTool(int index) {
Tool t = (Tool)tools.elementAt(index);
int id = t.id;
hp = (hp + Const.TOOLS_PROPERTICE[id][1]) > 100 ? 100 : hp + Const.TOOLS_PROPERTICE[id][1];
mp = (mp + Const.TOOLS_PROPERTICE[id][2]) > 100 ? 100 : mp + Const.TOOLS_PROPERTICE[id][2];
t.count--;
if(t.count <= 0) {
tools.removeElementAt(index);
}
}
//获得物品
public void getTool(int index) {
for(int i=0; i< tools.size(); i++) {
Tool t = (Tool)tools.elementAt(i);
if(t.id == index) {
t.count++;
break;
}
}
tools.addElement(new Tool((byte)index));
}
public void useMagic(int index) {
if(index == 0) {
if(mp < 10 * 3) {
m_View.curFightState = Const.FIGHT_HERO_WAIT;
return;
}
curMagic = magic1;
} else if (index == 1) {
if(mp < 15 * 3) {
m_View.curFightState = Const.FIGHT_HERO_WAIT;
return;
}
curMagic = magic2;
}
Enemy e = null;
for(int i=0; i<m_View.enemys.size(); i++) {
e = (Enemy)m_View.enemys.elementAt(i);
if(magicIndex >= 20) {
magicIndex = 0;
for(int j=0; j<m_View.enemys.size(); j++) {
e = (Enemy)m_View.enemys.elementAt(j);
if(index == 0) {
e.hp -= (ma * 5 - e.md);
mp -= 10;
} else if (index == 1) {
e.hp -= (ma * 7 - e.md);
mp -= 15;
}
if (e.hp <= 0) {
m_View.enemys.removeElement(e);
}
}
m_View.curIndex = 0;
m_View.curFightState = Const.FIGHT_ENEMY_WAIT;
return;
}
m_View.drawClipImg(e.fx - 13, e.fy - 13, magicWidth, magicHeight,
magicIndex % 5 * magicWidth, magicIndex / 5 * magicHeight, curMagic, m_View.bufGraphics);
magicIndex++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -