enemysprite32.java

来自「经典FC游戏《超惑星战记》的J2ME版本!!功能基本上都实现了」· Java 代码 · 共 418 行

JAVA
418
字号
/**
 * <p>Title: Transpanzer</p>
 * <p>Description:
 * You cannot remove this copyright and notice.
 * You cannot use this file any part without the express permission of the author.
 * All Rights Reserved</p>
 * @author Jjyo
 * @email jjyo@163.com
 * @version 1.0.0
 */
public class EnemySprite32 extends FSprite{
	GameManage gm;
	int dy;
	int dx;
	int attack;
	int index;
	int frameSpeed;
	int frame[]={0,1,2,3,4,5};
	boolean isJumpOver=true;
	public EnemySprite32(GameManage gm){
		this.gm=gm;
		rect1.dx=32;
		rect1.dy=32;
		rect2.dx=16;
		rect2.dy=16;
		frameSpeed=5;
	}
	
	//与地图水平碰撞检测
	protected boolean checkTileCollisionHorizon() {
		rect1.y = y + Maps.y+16;
		if (dx < 0) {
			rect1.x = x + Maps.x + dx+4;
		} else {
			rect1.x = x + Maps.x + dx+24;
		}
		int i = rect1.x / Maps.tileSize;
		if (i < 0 || i > Maps.w - 1)
			return false;
		int yTile1 = rect1.y / Maps.tileSize;
		if (yTile1 < 0 || yTile1 > Maps.h - 1)
			return false;
		if (((Maps.mapArray[yTile1][i] >> 8) & 0x0f) == 0x0f) {
			rect2.x = i * 16;
			rect2.y = yTile1 * 16;
			if (Rect.IntersectRect(rect1, rect2)) {
				return true;
			}
		}
		return false;
	}
	
	//与地图的纵向碰撞检测
	protected boolean checkTileCollisionVertical(){
		if (this.dy >= 0) {
			isJumpOver = false;
			for (int i = (y + Maps.y) / Maps.tileSize + 2; i <= (y + Maps.y + dy)
					/ Maps.tileSize + 2; i++) {
				if (i < 0 || i > Maps.h - 1) {
					continue;
				}
				int xTile1 = (x + Maps.x + 4) / Maps.tileSize;
				int xTile2 = (x + Maps.x + 20) / Maps.tileSize;
				if (xTile2 > Maps.w - 1 || xTile1 < 0) {
					return false;
				}

				if (((Maps.mapArray[i][xTile1] >> 8) & 0x0f) == 0x0f) {
					dy = (i - 2) * Maps.tileSize - (y + Maps.y);
					isJumpOver=true;
					return true;
				}
				if (((Maps.mapArray[i][xTile2] >> 8) & 0x0f) == 0x0f) {
					dy = (i - 2) * Maps.tileSize - (y + Maps.y);
					isJumpOver=true;
					return true;
				}
			}
		} else if (dy < 0) {
			for (int i = (y + Maps.y + dy) / Maps.tileSize; i <= (y + Maps.y)
					/ Maps.tileSize; i++) {
				if (i < 0 || i > Maps.h - 1) {
					continue;
				}
				int xTile1 = (x + Maps.x + 12) / Maps.tileSize;
				int xTile2 = (x + Maps.x + 20) / Maps.tileSize;
				if (xTile2 > Maps.w - 1 || xTile1 < 0) {
					return false;
				}

				if (((Maps.mapArray[i][xTile1] >> 8) & 0x0f) == 0x0f) {
					dy = 0;
					return true;
				}
				if (((Maps.mapArray[i][xTile2] >> 8) & 0x0f) == 0x0f) {
					dy = 0;
					return true;
				}
			}
		}
		isJumpOver = false;
		return false;
	}
	
	
	//子弹碰撞检测
	protected boolean checkBulletDamage(){
		rect1.dx = 8;
		rect1.dy = 8;
		rect2.dx = 32;
		rect2.dy = 32;
		for(int i=gm.lgr.tankBullet.length-1;i>=0;i--){
			if(!gm.lgr.tankBullet[i].isHidden){
				rect1.x=gm.lgr.tankBullet[i].x;
				rect1.y=gm.lgr.tankBullet[i].y;
				rect2.x=x;
				rect2.y=y;
				if(Rect.IntersectRect(rect1, rect2)){
					if(gm.lgr.tankBullet[i].pow<18)
						gm.lgr.tankBullet[i].isHidden=true;
					attack=gm.lgr.tankBullet[i].pow;
					return true;
				}
			}
		}
		return false;
	}
	
	protected void nextFrame(int frameNum,int frameIndex){
		if(index>=frameNum)
			index=0;
		frameCnt=frame[index]+frameIndex;
		if(++timeCnt%frameSpeed==0)
			index++;
		
	}
	
}
/**
 *石头人------------------------------------------------------------
 */
class RockMan extends EnemySprite32{

	public RockMan(GameManage gm,int x,int y) {
		super(gm);
		this.x=x;
		this.y=y;
		dx=-1;
		frameCnt=0;
		isHidden=false;
		this.pow=8;
	}
	
	public void action(){
		if(!isHidden){
			if (x > TankGameCanvas.width + 50 || x < -50 || y < -50
					|| y > TankGameCanvas.height + 50) {
				return ;
			}
			attack=0;
			dy+=2;
			checkTileCollisionVertical();//纵向碰撞检测
			if(checkTileCollisionHorizon()){
				dx=-dx;
			} 
			if(dx<0){
				if(x-gm.lgr.tankSprite.x<=50&&x-gm.lgr.tankSprite.x>=0){
					if(isJumpOver)
						dy-=12;
				}
				nextFrame(2,0);
			}else{
				if(x-gm.lgr.tankSprite.x>=-50&&x-gm.lgr.tankSprite.x<=0){
					if(isJumpOver)
						dy-=12;
				}
				nextFrame(2,2);
			}
			if(checkBulletDamage()){
				frameCnt=-1;
			}
			pow-=attack;
			x+=dx;
			y+=dy;
			timeCnt++;
			if(pow<=0){
				isHidden=true;
				gm.creatBombEffect(x, y, 1);
				if(timeCnt%4==0){
					gm.creatGoods(x, y, Goods.POW);
				}
			}
		}
	}
	
	
}

/**
 *太空船------------------------------------------------------------
 */
class SpaceShip extends EnemySprite32{
	
	boolean isOpen;
	public SpaceShip(GameManage gm,int x,int y) {
		super(gm);
		this.x=x;
		this.y=y;
		this.dx=-6;
		this.dy=0;
		this.pow=4;
		this.isHidden=false;
		isOpen=false;
	}
	
	public void action(){
		if(!isHidden){
			if (x > TankGameCanvas.width + 50 || x < -50 || y < -50
					|| y > TankGameCanvas.height + 50) {
				return ;
			}
			attack=0;
			if(checkBulletDamage()){
				frameCnt=-1;
			}
			if(dx<0){
				frameCnt=4;
			}else if(dx>0){
				frameCnt=5;
			}
			if(x-gm.lgr.tankSprite.x<32&&x-gm.lgr.tankSprite.x>=0){
				dx=0;
				dy=-8;
				frameCnt=6;
				if(!isOpen){
					gm.creatEnemyBullet(x+16, y+32, 0, 8, 2);
					isOpen=true;
				}
			}
			if(checkTileCollisionHorizon()){
				dx=-dx;
			}
			pow-=attack;
			x+=dx;
			y+=dy;
			timeCnt++;
			if(pow<=0){
				isHidden=true;
				gm.creatBombEffect(x, y, 1);
				if(timeCnt%4==0){
					gm.creatGoods(x, y, Goods.HOV);
				}
			}
		}
	}
}
/**
 * 机枪兵------------------------------------------------------
 */
class GunArms extends EnemySprite32{

	public GunArms(GameManage gm,int x,int y) {
		super(gm);
		this.x=x;
		this.y=y;
		dx=1;
		dy=0;
		frameCnt=0;
		isHidden=false;
		this.pow=16;
	}
	
	public void action(){
		if(!isHidden){
			if (x > TankGameCanvas.width + 50 || x < -50 || y < -50
					|| y > TankGameCanvas.height + 50) {
				return ;
			}
			dy+=2;
			attack=0;
			if(dx<0){
				if(timeCnt++%40==0){
					gm.creatEnemyBullet(x+16, y+14, -6, 0, 3);
				}
				nextFrame(2,12);
			}else{
				if(timeCnt++%40==0){
					gm.creatEnemyBullet(x+16, y+14, 6, 0, 3);
				}
				nextFrame(2,14);
			}
			if(checkBulletDamage()){
				frameCnt=-1;
			}
			if(checkTileCollisionHorizon()||!checkTileCollisionVertical()){
				dx=-dx;
			}
			pow-=attack;
			x+=dx;
			y+=dy;
			if(pow<=0){
				isHidden=true;
				gm.creatBombEffect(x, y, 1);
				if(timeCnt%4==0){
					gm.creatGoods(x, y, Goods.POW);
				}
			}
			
		}
	}
}

/**
 * ---------------------石孔雀------------------------
 */
class RockFowl extends EnemySprite32{

	boolean running;
	int type;
	public RockFowl(GameManage gm,int x,int y) {
		super(gm);
		this.x=x;
		this.y=y;
		pow=48;
		isHidden=false;
	}
	
	public void action(){
		if(!isHidden){
			if (x > TankGameCanvas.width + 32 || x < -32 || y < -32
					|| y > TankGameCanvas.height + 32) {
				return ;
			}
			attack=0;
			nextFrame(2,8);
			if(running){
				if(type==0){
					gm.creatEnemyBullet(x+12, y, 10, 0, 5);
					type+=1;
				}else if(type==1){
					gm.creatEnemyBullet(x+12, y+12, 10, 90, 5);
					type+=1;
				}else if(type==2){
					gm.creatEnemyBullet(x+12, y+24, 10, 180, 5);
					type=0;
				}
//				gm.creatEnemyBullet(x+16, y+32, 5, 180, 5);
			}
			if(timeCnt%20==0){
				running=!running;
				timeCnt=0;
			}
			if(checkBulletDamage()){
				frameCnt=-1;
			}
			pow-=attack;
			if(pow<=0){
				isHidden=true;
				gm.creatBombEffect(x, y, 2);
			}
		}
	}
}
/**
 * -------------------势利者-----------------
 */
class Snoot extends EnemySprite32{

	public Snoot(GameManage gm,int x,int y) {
		super(gm);
		this.x=x;
		this.y=y;
		isHidden=false;
		pow=64;
	}
	
	public void action(){
		if(!isHidden){
			if (x > TankGameCanvas.width + 32 || x < -32 || y < -32
					|| y > TankGameCanvas.height + 32) {
				return ;
			}
			attack=0;
			if(x-gm.lgr.tankSprite.x<0){
				dx=1;
			}else{
				dx=-1;
			}
			if(y-gm.lgr.tankSprite.y<0){
				dy=1;
			}else{
				dy=-1;
			}
			if(checkBulletDamage()){
				frameCnt=-1;
			}
			if(checkTileCollisionHorizon()){
				dx=0;
			}
			if(checkTileCollisionVertical()){
				dy=0;
			}
			nextFrame(2,10);
			x+=dx;
			y+=dy;
			pow-=attack;
			timeCnt++;
			if(pow<=0){
				isHidden=true;
				gm.creatBombEffect(x, y, 2);
				if(timeCnt%5==0){
					gm.creatGoods(x, y, Goods.POW);
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?