enemybullet.java

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

JAVA
175 行
字号
/**
 * <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 EnemyBullet extends FSprite{
	public GameManage gm;
 	protected int xSpeed;
 	protected int ySpeed;
 	protected int bulletType;
 	protected int arc;
 	private int arcos;
 	private int r;
 	public int growCnt;
	private Rect rect1=new Rect();
 	private Rect rect2=new Rect();
 	
 	public EnemyBullet(GameManage gm){
 		this.gm=gm;
 		isHidden=true;
	    rect1.dx=8;
	    rect1.dy=8;
	    rect2.dx=16;
	    rect2.dy=16;
 	}
 	
 	public void initBullet(int x,int y,int xSpeed,int ySpeed,int bulletType){
		
		this.x=x;
		this.y=y;
		this.xSpeed=xSpeed;
		this.ySpeed=ySpeed;
		this.bulletType=bulletType;
		isHidden=false;
		r=xSpeed;
		arcos=ySpeed;
		arc=0;
	}
 	
 	public void action(){
 		if(isHidden)
 			return ;
 		if(x>TankGameCanvas.width||x<-32||y<-32)
		{
			isHidden=true;
			return;
		}
 		switch(bulletType){
 		case 1:	//抛物线
 			frameCnt=1;
 			arc++;
 			ySpeed=(arc*arc)>>4;
 			
 			if(checkTileCollision()){
 				isHidden=true;
 				creatBombEffect(x,y,bulletType);
 			}
 			break;
 		case 2:	//定时
 			frameCnt=2;
 			if(checkTileCollision()){
 				arc++;
 				if(arc>=2){
 					isHidden=true;
 					creatBombEffect(x, y, bulletType);
 				}
 				y-=12;
 				x-=4;
 			}
 			break;
 		case 3:	//直线	大红点
 			frameCnt=1;
 			if(checkTileCollision()){
 				isHidden=true;
 				creatBombEffect(x,y,bulletType);
 			}
 			break;
 		case 4:	//直线	小红点
 			frameCnt=0;
 			if(checkTileCollision()){
 				isHidden=true;
 				creatBombEffect(x,y,bulletType);
 			}
 			break;
 		case 5:	//螺璇型
 			frameCnt=32;
 			if(arc%180==0){
 				r+=5;
 			}
 			xSpeed=(int)(r*Math.cos(Math.toRadians(arc-arcos)));
 			ySpeed=(int)(r*Math.sin(Math.toRadians(arc-arcos)));
 			arc+=30;
 			if(arc>=360)	//角度>360时复位
 				arc=0;
 			if(timeCnt++%30==0){
 				isHidden=true;
 			}
 			break;
 		case 6:	//直线	大红点
 			frameCnt=2;
 			if(checkTileCollision()){
 				isHidden=true;
 				creatBombEffect(x,y,bulletType);
 			}
 			break;
 		}
 		x+=xSpeed;
		y+=ySpeed;
 	}
 	
 	/**碰撞检测*/
 	private boolean checkTileCollision() {
		int j;
		if(bulletType==TankSprite.BULLET_DEFANUL||bulletType==TankSprite.BULLET_HYPER||bulletType==TankSprite.BULLET_CRUCHER){
			if(xSpeed<0){
				rect1.x=this.x+Maps.x;
				rect1.y=this.y+Maps.y+16;
			}else if(xSpeed>0){
				rect1.x=this.x+Maps.x+16;
				rect1.y=this.y+Maps.y+16;
			}else{
				rect1.x=this.x+Maps.x+16;
				rect1.y=this.y+Maps.y;
			}
		}else{
			rect1.x=this.x+Maps.x;
			rect1.y=this.y+Maps.y+4;
		}
		int yTile;
		j = rect1.x / Maps.tileSize;
		if(j<0||j>Maps.w)
		      isHidden=true;
		yTile = rect1.y / Maps.tileSize;
		rect2.x = j * 16;
		rect2.y = yTile * 16;
		if(yTile<0||yTile>Maps.h-1)
		     isHidden=true;
		if (((Maps.mapArray[yTile][j] >> 8) & 0x0f) == 0x0f) {
			rect2.x = j * 16;
			rect2.y = yTile * 16;
			if (Rect.IntersectRect(rect1, rect2)) {
				return true;
			}
		}
		return false;
	}
 	//创建爆炸效果
 	public void creatBombEffect(int x,int y,int bulletType){
		switch(bulletType){
		case 1:
		case 3:
		case 4:
			gm.creatBombEffect(x-16, y-12, 0);
			break;
			
		case 2:
			gm.creatBombEffect(x-14, y-14, 1);
			break;
			
		case 5:
			gm.creatBombEffect(x-4, y-6, 0);
			break;
		case 6:
			gm.creatBombEffect(x-14, y-14, 5);
			break;
		}
	}
}

⌨️ 快捷键说明

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