📄 trap.java
字号:
import javax.microedition.lcdui.Graphics;
public class Trap {
/**
* 构造函数,在此初始化陷阱的数据
* @param ge GameEngine 通过主轴进行控制
* @param trapData short[]陷阱的数据信息
*/
public Trap(Engine ge, short[] trapData)
{
engine = ge;
type = (byte) trapData[0]; //陷阱数据的格式为:第0项表示陷阱的类型
objectIndex = (byte) trapData[1]; //同上。此陷阱在表示动画数组中的索引
x = (short) (trapData[2]); //陷阱的位置x坐标
y = trapData[3]; //陷阱的位置y坐标
bround = trapData[4]; //陷阱的可以移动范围(不需要移动的陷阱,移动范围为0即可)
xSpeed = (byte) trapData[5]; //陷阱x方向的移动速度(即每帧x方向移动距离)
ySpeed = (byte) trapData[6]; //陷阱y方向的移动速度(即每帧y方向移动距离)
isLeft = trapData[7] == 0; //陷阱的初始化方向
isUp = isLeft;
startX = x; //陷阱的初始位置,需要记录初时位置
startY = y;
isDead = false; //陷阱是否存在。初时化存在
setStatus(ONE); //陷阱的初始化状态。为第1状态
Hp = 10; //初时生命值
}
Engine engine;
protected byte type; //陷阱的类型(火把、地刺、还是其他)
protected short x; //位置
protected short y;
protected short w; //此陷阱的宽度
protected short h; //此陷阱的高度
protected short bround; //移动范围
protected boolean isLeft; //是否为左方向
protected boolean isUp;//是否为向上方向移动(类似与isLeft)
protected byte xSpeed; //X轴速度
protected byte ySpeed;//Y轴速度
protected boolean isDead; //是否存在
protected byte curStatus; //表示陷阱的当前状态(我们定义陷阱的可用状态有5种)
protected short startX; //记录陷阱的初始状态
protected short startY;
protected byte drawLevel; //陷阱的绘制级别(可以不使用),考虑陷阱是在主角的里面还是外面显示
private byte Hp; //陷阱的生命值
private byte objectIndex; //陷阱在动画数组中的索引
private byte actionIndex; //陷阱的动作的索引
private byte frameIndex; //陷阱的帧的索引
private byte aniIndex; //处理动画帧变化的临时变量
protected static final byte ONE = 0; //陷阱的预留状态
protected static final byte TWO = 1; //陷阱的预留状态
protected static final byte THREE = 2; //陷阱的预留状态
protected static final byte FOUR = 3; //陷阱的预留状态
protected static final byte FIVE = 4; //陷阱的预留状态
/**
* 处理陷阱的逻辑
* 分类型分状态的处理
*/
protected final void move()
{
if (isDead)return; //如果陷阱不存在则不需要处理逻辑部分
getSize(); //获得陷阱的宽度、高度
playAni(-1);//以此游戏为例:这里的道具陷阱只有1个状态。
switch (type)
{
case 0: //海水
if(!engine.role.isDead && //主角没有死亡
engine.role.curStatus != Role.STATE_DEAD && //且主角不是死亡状态
engine.role.curStatus != Role.STATE_END && //且主角不是结束状态
engine.role.x > x - w/2 &&
engine.role.x < x + w/2 &&
Math.abs(engine.role.y - y) < 10)//两者y间距小于10(可以根据需求自由设定)
{
engine.role.setStatus(Role.STATE_DEAD);
}
break;
case 1: //石头
if(!engine.role.isDead && //主角没有死亡
engine.role.curStatus != Role.STATE_DEAD && //且主角不是死亡状态
engine.role.curStatus != Role.STATE_END && //且主角不是结束状态
Tools.hit(x,y,w,h,engine.role.x,engine.role.y,engine.role.w,engine.role.h)){
engine.role.setStatus(Role.STATE_INJURE);
int step = 0;
boolean moveLeft = engine.role.x < x;
boolean temp = engine.role.isLeft;
engine.role.isLeft = moveLeft;
step = (engine.role.w/2+w/2)-Math.abs(x - engine.role.x);
engine.role.roleMove(step,0);
engine.role.isLeft = temp;
}
break;
}
}
/**
* 绘制陷阱
* @param g Graphics
*/
protected final void drawTrap(Graphics g)
{
if (!isDead)
{
//陷阱存在则绘制陷阱(根据陷阱的参数objectIndex, actionIndex, frameIndex, x, y, isLeft)
Tools.drawNpcItemData(g, objectIndex, actionIndex, frameIndex, x, y, isLeft);
}
}
/**
* 改变陷阱状态的方法(类似于主角类里的改变状态的方法)
* @param nextState int
*/
protected final void setStatus(int nextState)
{
curStatus = (byte) nextState;
}
/**
* 输入参数nextState
* 当nextState > 0 切换状态
* nextState = -1 循环播放
* nextState < -1 停留在最后一帧
* @param nextState byte
*/
private final void playAni(int nextState)
{
/*当达到最后一帧时*/
if (aniIndex >= Data.frameItemIndex[objectIndex][actionIndex].length)
{
if (nextState >= 0)
{ //当nextState >= 0需要切换状态
aniIndex = 0; //帧设置为0
frameIndex = Data.frameItemIndex[objectIndex][actionIndex][aniIndex];
setStatus(nextState); //切换状态
return;
}
else if (nextState == -1)
{ //循环的播放此动作的动画
aniIndex = 0;
}
else
{ //停留在最后一帧不变
return;
}
}
frameIndex = Data.frameItemIndex[objectIndex][actionIndex][aniIndex];
aniIndex++;
}
/**
* 获得此陷阱的宽度、高度
*/
protected final void getSize()
{
/*根据保存每帧动画的宽度、高度数组获得宽度、高度*/
w = Data.npcItemDataSize[objectIndex][actionIndex][frameIndex * 4 + 2];
h = Data.npcItemDataSize[objectIndex][actionIndex][frameIndex * 4 + 3];
}
/**
* 获得此陷阱的绘制级别
* 可以不用
* @param type byte
*/
private final void getDrawLevel(byte type)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -