📄 enemy.java
字号:
import javax.microedition.lcdui.*;
public class Enemy
{
final static int rest1 = 1; //静止的兵
final static int tank2 = 2; //坦克
final static int tower3 = 3; //塔
final static int soldier_small4 = 4;//小远士兵
final static int soldier_big5 = 5; //近士兵
final static int plane6 = 6; //飞机
final static int Hp9 = 9; //药水瓶子
final static int smallstone10 = 10; //小石头
final static int bigstone11 = 11; //大石头
final static int casern12 = 12; //兵库
final static int supersoj13 = 13; //特种兵
final static int bus14 = 14; //汽车
final static int bomb15 = 15; //手雷
final static int Hp16 = 16; //药水瓶子
final static int casern19 = 19; //兵库
final static int casern20 = 20; //兵库
final static int iteam23 = 23; //物品
final static int bhouse24 = 24; //房子
final static int shouse25 = 25; //房子
final static int grass26 = 26; //草
final static int coconut27 = 27; //椰子树
final static int ship28 = 28; //船
/** ******************精灵的属性********************** */
int spriteType; //精灵的类型
protected int hp = 100; //生命值
protected int execution;//杀伤力
int spriteX, spriteY; //精灵的位置
int width, height; //图片的宽和高
protected int collisionW, collisionH; //检测碰撞的宽和高
protected int collisionX, collisionY; //检测碰撞区域的起始坐标
protected int currentFrame; //单前的帧数
protected int totalFrame; //总帧数
protected Gameworld gameworld; //Canvas对象的实例
boolean visible; //是否显示
private int itemType; //枪支宝物的类型
/** ******************精灵的各种状态********************** */
int enemyState; //精灵的状态
protected int nextState;
protected int tempState; //临时状态,状态改变时保存前一个状态
protected int tempFrame; //用来临时保存当前的状态的起始帧
protected int tempTotalFrame; //用来临时保存当前状态的结束帧
protected boolean isChangeState;//状态是否更新
final static int STA_GO_RIGHT = 0;//向右运动
final static int STA_DEAD = 1;//死亡
final static int STA_FIRE = 2;//开火
final static int STA_GO_LEFT = 3;//向左运动
final static int STA_REST = 4;//静止
final static int STA_TURN_HEAD = 5;//调转炮头
final static int STA_IS_FIRED = 8;//被攻击
final static int STA_FIRE_END = 9;//攻击动作结束,继续播放爆炸效果
final static int STA_ROLL_RIGHT = 10;//向右翻滚
final static int STA_ROLL_LEFT = 11;//向左翻滚
final static int STA_HIDEING = 12;//隐藏中
final static int STA_SPUER = 13; //无敌状态
/**
* 敌人类型说明
* 01 远景敌人1 02 坦克1 03 塔
* 04 近景敌人1(机枪) 05 近景敌人1(手雷) 06 飞机1
* 09 血瓶 010 小石头 011 大石头
* 012 兵库 013 特种兵1 015 敌人手雷1 016 血瓶
* 019 兵库 020 兵库 021 血瓶
* 022 血瓶 023 掉落的物品 024房子
* 026 大汽油罐 027 小汽油罐 028 船1
* 14 汽车 17 高射炮炮弹 18 空袭
* 29 探照灯 30 障碍球
*/
private long time; //子弹发射间隔
private int frameSpace; //动画帧间隔时间
private int randomTime; //随机时间
private boolean isTrigger = false; //触发器控制开关
private boolean isFisrt = true;
int seed;//多桢图片随机种子
private int movedTimes; //已经移动的时间
private int moveTime; //移动限制时间
private long lastTime; //时间控制
boolean isFire;//开火子弹显示控制
private int addPosition; //画面被撕裂时的位置增量
//构造
public Enemy(int w, int h, int type, Gameworld world)
{
width = w;
height = h;
spriteType = type;
gameworld = world;
seed = world.randomNum(3);
}
//准确设置生命值
public final void setHP(int i)
{
if (i > 100)
{
hp = 100;
} else if (i < 0)
{
i = 0;
} else
{
hp = i;
}
}
//开火
final void action()
{
gameworld.player.killHP(execution);
}
//设置精灵位置
public final void setPosition(int x, int y)
{
spriteX = x;
spriteY = y;
}
//与敌人的碰撞检测,矩形检测
final boolean isCollision1(Enemy sp, int x, int y)
{
if (sp.collisionY + sp.height < y
|| sp.collisionY > y + collisionH + 10
|| sp.collisionX + sp.width < x
|| sp.collisionX > x + collisionW + 10)
{
return false;
}
return true;
}
//移动精灵
public final void move(int i, int j)
{
spriteX += i;
spriteY += j;
collisionX += i;
collisionY += j;
//移动的敌人如果超出了边界自动消失
if (this.spriteX >= gameworld.screenWidth - gameworld.offsetX
|| this.spriteX <= Math.abs(gameworld.offsetX) - this.width)
{
this.visible = false;
gameworld.enemyCount--;
}
}
//计算触发时间
public final void trigger(int i_time)
{
i_time = i_time <= 0 ? 1000 : i_time;
time += System.currentTimeMillis() - lastTime;
if (time >= i_time)
{
time = 0;
isTrigger = true;
} else
{
isTrigger = false;
}
}
/**
* 被子弹击中后,扣掉生命值
*
* @param i_hp
* 损伤的生命值
*/
public final void killHP(int i_hp)
{
if (this.enemyState != Enemy.STA_DEAD)
{
hp -= i_hp;
setHP(hp);
if (hp <= 0)
{
if (gameworld.sound)
{
if (spriteType == 2 || spriteType == 3 || spriteType == 6
|| spriteType == 8 || spriteType == 9
|| spriteType == 21 || spriteType == 22
|| spriteType == 28)
{
try
{
gameworld.soundSFX[1].start();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
changeState(Enemy.STA_DEAD); //改变为死亡状态
return;
}
//特种兵被击中后自动朝屏幕中心滚动
if (spriteType == 13)
{
if ((this.spriteX + gameworld.offsetX >= gameworld.screenWidth / 2))
{
nextState = STA_ROLL_LEFT;
} else
{
nextState = STA_ROLL_RIGHT;
}
}
}
}
//设置敌人类型和坐标
public final void init(int x, int type)
{
this.visible = true;//设置为可见
spriteType = type; //保存类型
spriteX = x;
isFisrt = true;
isTrigger = false;
time = 0;
lastTime = 0;
movedTimes = 0;
moveTime = 0;
switch (type)
{
case 1: //静止的步兵生命为1
if (gameworld.level > 3 && gameworld.level < 7)
spriteY = 40;
else
spriteY = 80;
setHP(1);
width = 52;
height = 55;
collisionW = 15;
collisionH = 15;
collisionX = spriteX + 3;
collisionY = spriteY + 5;
execution = 1; //杀伤力
enemyState = Enemy.STA_REST; //状态
nextState = enemyState; //下一个状态
changeState(enemyState);//改变状态
randomTime = gameworld.randomNum(6); //随即1-5秒后射击
break;
case tank2: //坦克
spriteY = 120;
setHP(5);
width = 95;
height = 63;
collisionW = 80;
collisionH = 60;
this.collisionX = spriteX+60;
this.collisionY = spriteY;
execution = 5;
enemyState = Enemy.STA_REST;
nextState = enemyState;
changeState(enemyState);
randomTime = 4000; //坦克在4秒后触发
break;
case tower3: //塔
if (gameworld.level < 4)
spriteY = -5;
else
spriteY = 120;
width = 88;
height = 120;
enemyState = Enemy.STA_REST;
nextState = enemyState;
changeState(enemyState);
break;
case soldier_small4: //随机出现的步兵
case soldier_big5: //手雷兵
if (seed == 0)
spriteY = 150;
else
spriteY = 160;
setHP(1);
width = 52;
height = 56;
collisionW = 15;
collisionH = 20;
collisionX = spriteX + 10;
collisionY = spriteY + 5;
execution = 1;
enemyState = Enemy.STA_GO_RIGHT;
nextState = enemyState;
changeState(enemyState);
break;
case plane6: //飞机
if (seed < 2)
spriteY = 32;
else
spriteY = 16;
setHP(8);
width = 93;
height = 48;
collisionW = 50;
collisionH = 20;
collisionX = spriteX + 20;
collisionY = spriteY + 10;
execution = 1;
enemyState = Enemy.STA_GO_RIGHT;
nextState = enemyState;
changeState(enemyState);
break;
case Hp9: //油桶
spriteY = 160;
setHP(1);
width = 20;
height = 16;
this.collisionW = 16;
this.collisionH = 14;
collisionX = spriteX - 2;
collisionY = spriteY - 2;
enemyState = STA_GO_RIGHT;
nextState = STA_GO_RIGHT;
changeState(0);
break;
case smallstone10: //小石头
if (seed < 2)
spriteY = 144;
else
spriteY = 160;
width = 71;
height = 32;
enemyState = STA_GO_RIGHT;
nextState = STA_GO_RIGHT;
changeState(0);
break;
case bigstone11://大石头
if (seed == 0)
spriteY = 130;
else if (seed == 1)
spriteY = 140;
else
spriteY = 192;
width = 71;
height = 32;
enemyState = STA_GO_RIGHT;
nextState = STA_GO_RIGHT;
changeState(0);
break;
case casern12:
spriteY = 100;
setHP(3);
width = 115;
height = 54;
this.collisionW = 16;
this.collisionH = 16;
collisionX = spriteX + 16;
collisionY = spriteY + 30;
execution = 2;
enemyState = Enemy.STA_REST;
nextState = enemyState;
changeState(enemyState);
break;
case supersoj13: //特种兵
spriteY = 128;
setHP(8);
width = 52;
height = 54;
this.collisionW = 20;
this.collisionH = 20;
collisionX = spriteX + 10;
collisionY = spriteY + 5;
execution = 3;
enemyState = Enemy.STA_GO_LEFT;
nextState = enemyState;
changeState(enemyState);
break;
case bus14: //汽车
if (seed == 0)
spriteY = 110;
else
spriteY = 90;
setHP(8);
width = 125;
height = 72;
this.collisionW = 70;
this.collisionH = 30;
collisionX = spriteX + 20;
collisionY = spriteY + 20;
execution = 5;
this.randomTime = 8;
enemyState = Enemy.STA_REST;
nextState = enemyState;
changeState(enemyState);
break;
case bomb15: //敌人手雷
if (seed == 0)
spriteY = 150;
else
spriteY = 160;
width = 30;
height = 30;
this.collisionW = 5;
this.collisionH = 5;
collisionX = spriteX + 5;
collisionY = spriteY + 5;
enemyState = STA_GO_RIGHT;
execution = 2;
nextState = STA_GO_RIGHT;
changeState(STA_GO_RIGHT);
break;
case Hp16: //医疗箱
if (seed <2)
spriteY = 160;
else
spriteY = 192;
height = 12;
width = 28;
this.collisionW = 16;
this.collisionH = 16;
collisionX = spriteX;
collisionY = spriteY;
enemyState = STA_GO_RIGHT;
nextState = STA_GO_RIGHT;
changeState(STA_GO_RIGHT);
break;
case 21: //血瓶
case 22:
if (seed < 2)
spriteY = 176;
else
spriteY = 160;
setHP(1);
width = 10;
height = 16;
this.collisionW = 8;
this.collisionH = 14;
collisionX = spriteX;
collisionY = spriteY;
enemyState = STA_GO_RIGHT;
nextState = STA_GO_RIGHT;
changeState(STA_GO_RIGHT);
break;
case iteam23: //各种宝物
if (seed < 2)
spriteY = 144;
else
spriteY = 160;
height = 16;
width = 16;
collisionW = 14;
collisionH = 14;
collisionX = spriteX;
collisionY = spriteY - 2;
enemyState = STA_GO_RIGHT;
nextState = STA_GO_RIGHT;
changeState(STA_GO_RIGHT);
itemType = gameworld.randomNum(3); //随机计算宝物类型1/3几率掉空袭导弹
break;
case bhouse24: //大房子
spriteY = 30;
width = 168;
height = 87;
enemyState = Enemy.STA_REST;
nextState = enemyState;
changeState(enemyState);
break;
case grass26: //草
if (seed == 0)
spriteY = 100;
else if (seed == 1)
spriteY = 120;
else
spriteY = 140;
width = 54;
height = 32;
enemyState = Enemy.STA_REST;
nextState = enemyState;
changeState(enemyState);
break;
case coconut27: //椰子
if (seed == 0)
spriteY = 10;
else
spriteY = 20;
width = 74;
height = 143;
enemyState = Enemy.STA_REST;
nextState = enemyState;
changeState(enemyState);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -