首页>代码>java swing写的坦克大战>/tank/src/game/tank/BulletFly.java
package game.tank;

import game.tank.entity.Boom;
import game.tank.entity.Bullet;
import game.tank.entity.Cell;
import game.tank.entity.Tank;
import game.tank.thread.BlinkThread;
import game.tank.thread.BoomThread;
import game.tank.ui.TankPanel;
import game.tank.util.Consts;
import game.tank.util.Utils;
import java.util.List;


public class BulletFly{

	private Tank tank;
	
	private int direction;

	public void flyBullet(Tank tank,Scene scene){
		Bullet bullet = tank.getBullet();
		List<Tank> tankList = scene.getTankList();
		int direction = bullet.getDirection();
		int[][] map = scene.getMap();
		
		while(true){
			
			if(direction == 1){
				bullet.setVertical(bullet.getVertical()-1);
			}else if(direction == 2){
				bullet.setVertical(bullet.getVertical()+1);
			}else if(direction == 3){
				bullet.setHorizon(bullet.getHorizon()-1);
			}else if(direction == 4){
				bullet.setHorizon(bullet.getHorizon()+1);
			}
			
			for(int i=0;i<tankList.size();i++){
				Tank t = tankList.get(i);
				if(tank.equals(t) || t.isAlive()==false || Utils.checkIfTeammate(t, tank)){
					continue;
				}
				
				if(Utils.checkCollide(bullet, t)) {
					t.setHp(t.getHp()-1);
					 if(!t.isComputer()){
						 TankPanel.decreaseLives();
					 }
					 if(t.getHp() <= 0) {
						 t.setAlive(false);
						 checkWinOrLose(scene);
						 if(t.isComputer())
							 joinFromWaitingTankList(scene);
					 }
					 this.bulletBlast(tank, t,scene);
					 tank.setBullet(null);
					 return;
				}
				if(t.getBullet() != null && Utils.checkCollide(tank.getBullet(), t.getBullet())){
					tank.setBullet(null);
				    t.setBullet(null);
					return;
				}
			}
			
			boolean collide = false;
			try {
				for(int i=0;i< scene.getMapElements().size();i++){
					Cell cell = scene.getMapElements().get(i);
					if(cell.getType() == 0){
						continue;
					}
					if(Utils.checkCollide(tank.getBullet(), cell)){
						 if(cell.getType() == Consts.ELEMENT_TYPE_WALL){
							 int col = cell.getCol();
							 int row = cell.getRow();
							 map[row][col] = 0;
							 cell.setType(0);
						 }else if(cell.getType() == Consts.ELEMENT_TYPE_BASE){
							 int col = cell.getCol();
							 int row = cell.getRow();
							 map[row][col] = Consts.ELEMENT_TYPE_WHITEFLAG;
							 MapManager.getInstance().endGame(Consts.LOSE);
							 cell.setType(Consts.ELEMENT_TYPE_WHITEFLAG);
						 }else if(cell.getType() == Consts.ELEMENT_TYPE_LAKE){
							 continue;
						 }
						 this.bulletBlast(tank, cell,scene);
						 collide = true;
						 
					}
					
				}
			}catch(Exception e){
				
				if(tank.getBullet() == null)
					System.out.println("bullet collide.[bullet]:"+tank.getBullet());
				else
					e.printStackTrace();
				return;
			}
			if(collide == true){
				tank.setBullet(null);
				return;
			}
			try{
				 Thread.sleep((long)(1 * 1000 / Bullet.SPEED));  
			 }catch(Exception ex){
				 ex.printStackTrace();
			 }
		}
	} 	// end method
	
	public void bulletBlast(Tank tank,Tank collideObj,Scene scene){
		ImageManager imgMgr = ImageManager.getInstance();
		int centerX = collideObj.getHorizon() + collideObj.getImg().getWidth(null)/2;
		int centerY = collideObj.getVertical() + collideObj.getImg().getHeight(null)/2;
		int blastX = centerX - imgMgr.getBoom().getWidth(null)/2;
		int blastY = centerY - imgMgr.getBoom().getHeight(null)/2;
		Boom boom = new Boom(blastX,blastY);
		showBoom(boom,scene);
	}
	
	private void checkWinOrLose(Scene scene){
		List<Tank> tankList = scene.getTankList();
		List<Tank> waitingList = scene.getWaitedTanks();
		Tank mainTank = scene.getMainTank();
		boolean PCElimated = true;
		if(mainTank.isComputer() == false && mainTank.isAlive() == false){
			MapManager.getInstance().endGame(Consts.LOSE);
			return;
		}
		if(waitingList.size() != 0){
			return;
		}else {
			for(int i=0;i<tankList.size();i++){
				Tank t = tankList.get(i);
				if(t.isComputer() == true && t.isAlive() == true){
					PCElimated = false;
				}
			}
		}
		if(PCElimated == true){
			MapManager.getInstance().endGame(Consts.WIN);
		}
	}
	
	public void bulletBlast(Tank tank,Bullet collideObj){
		
	}
	
	public void bulletBlast(Tank tank,Cell collideObj,Scene scene){
		ImageManager imgMgr = ImageManager.getInstance();
		int direction = tank.getBullet().getDirection();  
		int bulletX = tank.getBullet().getHorizon();
		int bulletY = tank.getBullet().getVertical();
		int blastImgWidth = imgMgr.getBoom().getWidth(null);
		int bulletImgWidth = tank.getBullet().getImg().getWidth(null);
		int blastX = 0;
		int blastY = 0;
		if(direction == 1 || direction == 2){
			blastX = bulletX + bulletImgWidth/2 - blastImgWidth/2;
			blastY = bulletY - blastImgWidth/2;
		}else if(direction == 3 || direction == 4){
			blastX = bulletX - blastImgWidth/2;
			blastY = bulletY + bulletImgWidth/2 - blastImgWidth/2;
		}
		Boom boom = new Boom(blastX,blastY);
		showBoom(boom,scene);
	}
	
	private void showBoom(Boom boom,Scene scene){
		BoomThread thread = new BoomThread(boom,scene);
		thread.start();
//		scene.addBoom(boom);
//		try{
//			Thread.sleep(20);
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		scene.removeBoom(boom);
	}
	
	/**
	 * 等待队列中的坦克加入战场
	 * @param scene
	 */
	private void joinFromWaitingTankList(Scene scene){
		new BlinkThread(scene).start();	
	}
	
	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}
}
最近下载更多
鲁一宇  LV5 2024年12月24日
ypf8312  LV22 2024年9月22日
dusk red  LV2 2024年9月2日
12345685  LV2 2024年1月7日
dengge123  LV14 2024年1月5日
微信网友_6778365748318208  LV1 2023年12月14日
法拉利  LV1 2023年12月12日
liangge2115  LV27 2023年12月3日
moonlx  LV1 2023年10月19日
lwl1207  LV4 2023年8月29日
最近浏览更多
开膛手杰克哈哈哈  LV1 6月22日
yangctz  LV25 3月1日
鲁一宇  LV5 2024年12月24日
ypf8312  LV22 2024年9月22日
dusk red  LV2 2024年9月2日
暂无贡献等级
krispeng  LV15 2024年7月21日
wyy1234 2024年7月16日
暂无贡献等级
wwwwww5632 2024年6月21日
暂无贡献等级
于子洲  LV1 2024年6月3日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友