⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ssmlauncher.java

📁 坦克大战游戏源代码
💻 JAVA
字号:
import java.awt.*;

public class ssmLauncher implements enemy{
	public surface Surface;
	public SSMbody body;
	public SSMtower tower;
	public boolean turnLeft, turnRight, moveUp, moveDown, aimLeft, aimRight;
	private double speed, rSpeedB, rSpeedT;
	public paste[] Paste;
	public int fireInterval;
	public boolean fire, reLoad;
	public int x_shift, y_shift;
	public int health;
	public enemy[] Enemy;
	public int range;

	//================== AI related varibales=======================
	public vector origin;
	public vector tankPosition, myPosition, tankDirection, originDirection;
	public double targetDistance;
	public boolean attacking;
	public boolean lookingForSpace;
	public int count;
	public int stuckCount;
	//==============================================================

	public ssmLauncher(surface Surface, int xPos, int yPos, vector direction2D, int x_shift, int y_shift){
		this.Surface = Surface;
		this.x_shift = x_shift;
		this.y_shift = y_shift;
		Paste = new paste[16];
		origin = new vector(xPos, yPos);
		speed = 1;
		range = 350;
		health = 100;
		rSpeedB = Math.PI/96;
		rSpeedT = Math.PI/48;
		body = new SSMbody(Surface, xPos, yPos, 0, 0, direction2D, x_shift, y_shift);
		tower = new SSMtower(Surface, xPos, yPos, body.height + 2 , 6, direction2D, body.direction3D, x_shift, y_shift);
		fireInterval = 100;
	}

	public void move(int x_shift, int y_shift, explosion[] explosions, enemy[] Enemy, rail[] Rail){
		this.x_shift = x_shift;
		this.y_shift = y_shift;
		this.Enemy = Enemy;

		body.shift(x_shift, y_shift);
		tower.shift(x_shift, y_shift);

		performAI();

		if(turnLeft){
			body.rotate(rSpeedB);
			tower.rotate(rSpeedB, body.direction3D);
		}
		else if(turnRight){
			body.rotate(-rSpeedB);
			tower.rotate(-rSpeedB, body.direction3D);
		}
		if(moveUp){
			body.move(speed);
			tower.x = body.x;
			tower.y = body.y;
			tower.bodyDirection = body.direction3D;
			tower.defindStatus();
		}
		if(aimLeft)
			tower.rotate(rSpeedT, body.direction3D);
		else if(aimRight)
			tower.rotate(-rSpeedT, body.direction3D);

		if(fireInterval < 120)
			fireInterval++;
		if(fireInterval == 120)
			reLoad = false;

		decideWhichToDrawFirst();
		checkIfHurt(explosions, Rail);
	}

	public void draw(Graphics g){
		vector shift = new vector(x_shift, y_shift);
		if(body.centre.subtract(shift).length() <750){
			for(int i = 0; i < Paste.length; i++)
				Paste[i].draw(g);
		}
	}

	public void decideWhichToDrawFirst(){
		Paste = new paste[16];
		for(int i = 0; i < 16; i++){
			if(i < 10)
				Paste[i] = body.Paste[i];
			else
				Paste[i] = tower.Paste[i-10];
		}
		paste temp;
		int j  = 1;
		while(j<Paste.length){
			for(int i = 0; i <Paste.length - j; i++){
				if(Paste[i].position > Paste[i+1].position){
					temp = Paste[i+1];
					Paste[i+1] = Paste[i];
					Paste[i] = temp;
				}
			}
			j++;
		}
	}

	public rocket fireRocket(){
		if(fireInterval == 120){
			fireInterval = 0;
			return 	new rocket(Surface, tower.direction3D, tower.centre.add(tower.direction3D.scale(20)).add(new vector(0, 0, 5)).add(tower.direction3DCrossNor.scale(-4)),tower.direction3DCrossNor, x_shift, y_shift);

		}
		if(fireInterval >= 20){
			reLoad = true;
			return 	new rocket(Surface, tower.direction3D, tower.centre.add(tower.direction3D.scale(20)).add(new vector(0, 0, 5)).add(tower.direction3DCrossNor.scale(4)),tower.direction3DCrossNor, x_shift, y_shift);

		}
		return null;
	}

	public boolean canFire(){
		return !reLoad && fire;
	}

	public double position(){
		vector c = body.centre.subtract(new vector(x_shift, y_shift, -6));
		return c.dot(new vector(1,-2,1));
	}

	public vector centre(){
		return new vector(body.centre.a, body.centre.b, body.centre.c+6);
	}

	public void checkIfHurt(explosion[] explosions, rail[] Rail){
		for(int i = 0; i < explosions.length; i++){
			if(explosions[i] != null){
				if(tower.centre.subtract(explosions[i].position).length() <= 5 + explosions[i].time)
					damage(5);
			}
		}
		for(int i = 0; i < Rail.length; i++){
			if(Rail[i] != null){
				if(Rail[i].time == 1){
					for(int j = 0; j < Rail[i].crashPoint; j++){
						if(j%6 == 0){
							if(tower.centre.subtract(Rail[i].helixWave[j]).subtract(new vector(x_shift, y_shift)).length() <= 13){
								damage(100);
								break;
							}
						}
					}
				}
			}
		}
	}

	public void damage(int damage){
		if(health >0)
			health-=damage;
		if(health <=0)
			health = 0;
	}

	public explosion explode(){
		return new explosion(body.centre, body.gradient, 25, "ground", x_shift, y_shift);
	}


//==========================================================AI Programming ================================================================
	public void performAI(){
		tankPosition = new vector(x_shift, y_shift);
		myPosition = new vector(body.centre.a, body.centre.b);
		tankDirection = tankPosition.subtract(myPosition).unit();
		originDirection = origin.subtract(myPosition).unit();
		moveUp = false; turnLeft = false; turnRight = false; aimLeft = false; aimRight = false; fire = false; attacking = false;

		double distance = tankPosition.subtract(myPosition).length();
		targetDistance = distance;

		if(lookingForSpace && distance >= range && distance <= range + 100)
			checkFreeSpace();
		else if(distance > range + 100){
			if(origin.subtract(myPosition).length() > 1)
				GoTo(originDirection);
		} else if(distance < range)
			TargetTank();
		else
			GoTo(tankDirection);
	}

	public void GoTo(vector P){
		//============================decide ssmBody action======================
		vector direction = body.direction2D.unit();
		double difference = P.subtract(direction).length();
		if(difference < 0.02){
			body.direction2D = P;
			body.defindStatus();
			checkFreeSpace();
		} else if(leftTurn(direction, P, difference,rSpeedB))
			turnLeft = true;
		else
			turnRight = true;

		//==========================decide ssmTower action=======================
		direction = tower.direction2D.unit();
		difference = P.subtract(direction).length();
		if(difference < 0.04){
			tower.direction2D = P;
			tower.defindStatus();
		}else if(leftTurn(direction, P, difference,rSpeedT))
			aimLeft = true;
		else
			aimRight = true;
	}

	public void TargetTank(){
		attacking = true;
		vector direction = tower.direction2D.unit();
		double difference = tankDirection.subtract(direction).length();
		if(difference < 0.04){
			tower.direction2D = tankDirection;
			tower.defindStatus();
		}else if(leftTurn(direction, tankDirection, difference,rSpeedT)){
			aimLeft = true;
		}
		else
			aimRight = true;
		fire = true;
		count = 0;
		lookingForSpace = false;
	}

	public boolean leftTurn(vector a, vector b, double d, double s){
		return a.rotate2D(s).unit().subtract(b).length() < d;
	}


	//=================================avoid moving into other objects========================	public void checkFreeSpace(){
		vector nextPosition = body.centre.add(body.direction3D.scale(speed));
		for(int i = 0; i < Enemy.length; i++){
			double distance = Enemy[i].centre().subtract(nextPosition).length();
			if(distance < 30 && distance > 10.05){
				moveUp = false;
				if(Enemy[i].isAttacking() || targetDistance < Enemy[i].distanceFromTarget() || Enemy[i].StuckTime() >=22){
					count = 25;
					if(stuckCount < 45)
						stuckCount++;
					lookingForSpace = true;
					vector myDirection = body.direction2D.unit();
					vector otherDirection = Enemy[i].centre().subtract(myPosition).unit();
					double difference = myDirection.subtract(otherDirection).length();
					if(stuckCount == 45){
						body.rotate(rSpeedB);
						tower.rotate(rSpeedB, body.direction3D);
					} else if(leftTurn(myDirection, otherDirection, difference,-rSpeedB)){
						body.rotate(rSpeedB);
						tower.rotate(rSpeedB, body.direction3D);
					} else{
						body.rotate(-rSpeedB);
						tower.rotate(-rSpeedB, body.direction3D);
					}
				}
				return;
			}
		}
		if(stuckCount > 0){
			stuckCount--;
			boolean changeDirection = true;
			nextPosition = body.centre.add(tankDirection.scale(5));
			for(int i = 0; i < Enemy.length; i++){
				double distance = Enemy[i].centre().subtract(nextPosition).length();
				if(distance < 30 && Enemy[i].centre().subtract(centre()).length() > 3){
					changeDirection = false;
					if(stuckCount < 45)
						stuckCount++;
				}
			}
			if(changeDirection){
				vector direction = body.direction2D.unit();
				double difference = tankDirection.subtract(direction).length();
				if(leftTurn(direction, tankDirection, difference,rSpeedB))
					turnLeft = true;
				else
					turnRight = true;
			}
		}
		if(count > 0){
			count--;
			moveUp = true;
			return;
		} else if(lookingForSpace){
			nextPosition = body.centre.add(tankDirection.scale(40));
			for(int i = 0; i < Enemy.length; i++){
				double distance = Enemy[i].centre().subtract(nextPosition).length();
				if(distance < 30 && Enemy[i].centre().subtract(centre()).length() > 3){
					moveUp = true;
					count = 25;
					return;
				}
			}
		}
		moveUp = true;
		lookingForSpace = false;
	}

	public boolean isAttacking(){
		return attacking;
	}

	public double distanceFromTarget(){
		return targetDistance;
	}

	public int StuckTime(){
		return stuckCount;
	}

	public vector getDirection3D(){
		return body.direction3D;
	}

	public vector miniMapPosition(){
		vector v = body.centre.subtract(new vector(x_shift, y_shift)).scale(0.05);
		return new vector(v.a, -v.b);
	}

}

⌨️ 快捷键说明

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