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

📄 raillauncher.java

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

public class railLauncher implements enemy{
	public surface Surface;
	public SSMbody body;
	public RAILtower tower;
	public boolean turnLeft, turnRight, moveUp, moveDown, aimTarget;
	private double speed, rSpeedB;
	public paste[] Paste;
	public int fireInterval;
	public boolean fire;
	public int x_shift, y_shift;
	public int health;
	public enemy[] Enemy;
	public int range;
	public vector targetDirection;

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

	public railLauncher(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[18];
		origin = new vector(xPos, yPos);
		speed = 1;
		range = 250;
		health = 100;
		rSpeedB = Math.PI/96;
		body = new SSMbody(Surface, xPos, yPos, 0, 0, direction2D, x_shift, y_shift);
		tower = new RAILtower(xPos,yPos, body.z, body.height + 5, 6, body.normal, body.direction3D, x_shift, y_shift);
		decideWhichToDrawFirst();
		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;

		tower.bodyNormal = body.normal;

		performAI();

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

		if(turnLeft){
			body.rotate(rSpeedB);
			tower.bodyDirection = body.direction3D;
			tower.defindStatus();
		}
		else if(turnRight){
			body.rotate(-rSpeedB);
			tower.bodyDirection = body.direction3D;
			tower.defindStatus();
		}
		if(moveUp){
			body.move(speed);
			tower.x = body.x;
			tower.y = body.y;
			tower.z = body.z;
			tower.bodyDirection = body.direction3D;
			tower.adjust(body.gradient);
		}
		if(aimTarget)
		    tower.rotate(targetDirection, body.gradient);

		decideWhichToDrawFirst();
		checkIfHurt(explosions, Rail);

		if(fireInterval < 100)
			fireInterval++;
	}

	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 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 explosion explode(){
		return new explosion(body.centre, body.gradient, 25, "ground", x_shift, y_shift);
	}

	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[18];
		for(int i = 0; i < 18; 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 rail shoot(){
		fireInterval = 0;
		return new rail(Surface, tower.direction3D, tower.normal, tower.centre.add(tower.direction3D.scale(17)), x_shift, y_shift);
	}

	public boolean canFire(){
		return fireInterval == 100 && fire;
	}

	//=============================================================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();
		tankDirection3D = body.centre.subtract(new vector(x_shift, y_shift, Surface.evaluate(x_shift, y_shift))).scale(-1).unit();
		moveUp = false; turnLeft = false; turnRight = false; aimTarget = false; fire = false; attacking = false;

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

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

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

		targetDirection = body.direction3D;
		vector differ = targetDirection.subtract(tower.direction3D);
		if(differ.length() < 0.04){
			tower.direction3D = targetDirection;
			tower.adjust(body.gradient);
		}else
			aimTarget = true;
	}

	public void TargetTank(){
		attacking = true;
		vector direction = tower.direction3D.unit();
		targetDirection = tankDirection3D;
		double difference = targetDirection.subtract(direction).length();
		if(difference < 0.04){
			fire = true;
			tower.direction3D = targetDirection;
			tower.adjust(body.gradient);
		} else
			aimTarget = 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;
	}

	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.bodyDirection = body.direction3D;
						tower.rotate(body.direction3D, body.gradient);
					} else if(leftTurn(myDirection, otherDirection, difference,-rSpeedB)){
						body.rotate(rSpeedB);
						tower.bodyDirection = body.direction3D;
						tower.rotate(body.direction3D, body.gradient);
					} else{
						body.rotate(-rSpeedB);
						tower.bodyDirection = body.direction3D;
						tower.rotate(body.direction3D, body.gradient);
					}
				}
				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 + -