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

📄 rocket.java

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

public class rocket{
	public vector direction3D, centre, cross, normal;
	public double speed;
	public surface Surface;
	public int time;
	public vector[] points;
	public paste[] Paste;
	public boolean crashed;
	public int width, length, height;
	public vector flame;
	public vector trail[][];
	public int x_shift, y_shift;
	public int disapearTime;

	public rocket(surface Surface, vector direction3D, vector centre, vector cross, int x_shift, int y_shift){
		this.direction3D = direction3D.unit();
		this.centre = centre;
		this.Surface = Surface;
		this.cross = cross;
		this.x_shift = x_shift;
		this.y_shift = y_shift;
		normal = direction3D.cross(cross).unit();
		speed = 3;
		points = new vector[8];
		width = 1; length = 7; height = 1;
		flame = centre.add(direction3D.scale(-15));
		trail = new vector[7][4];
		for(int i = 0; i < 7; i++){
			for(int j = 0; j < 4; j++)
				trail[i][j] = flame;
		}
		addPoints();
	}

	public void fly(vector targetCentre){
		this.x_shift = (int)targetCentre.a;
		this.y_shift = (int)targetCentre.b;
		if(centre.subtract(targetCentre).length() < 200)
			flyToTarget(targetCentre);

		else if((centre.c - Surface.evaluate(centre.a, centre.b)) < 10)
			flyUp();
		else if(direction3D.c > 0)
			flyDown();
		flame = centre.add(direction3D.scale(-15));
		if(time % 4 == 0 && time/4 >=1)
			makeTrail();
		addPoints();
		if(speed < 6)
			speed+=0.05;
		time++;
		if(crashed)
			disapearTime++;
		checkIfCrashed(targetCentre);
		centre = centre.add(direction3D.scale(speed));
	}

	public void makeTrail(){
		for(int i = 0; i < trail.length - 1; i++){
			int current = trail.length - i - 1, next = trail.length - i - 2;
			for(int j = 0; j < trail[current].length; j++){
				trail[current][0] = trail[next][0].add(cross);
				trail[current][1] = trail[next][1].subtract(cross);
				trail[current][2] = trail[next][2].add(normal);
				trail[current][3] = trail[next][3].subtract(normal);
			}
		}

		for(int i = 0; i < trail[0].length; i++)
			trail[0][i] = flame;

	}

	public void flyUp(){
		direction3D = new vector(direction3D.a, direction3D.b, direction3D.c + 0.05);
		direction3D = direction3D.unit();
		normal = direction3D.cross(cross).unit();
	}

	public void flyDown(){
		direction3D = new vector(direction3D.a, direction3D.b, direction3D.c - 0.05);
		if(direction3D.c - 0.01 <= 0)
			direction3D.c = 0;
		direction3D = direction3D.unit();
		normal = direction3D.cross(cross).unit();
	}

	public void flyToTarget(vector targetCentre){
		vector difference = targetCentre.subtract(centre).unit();
		direction3D = direction3D.add(difference.scale(0.05)).unit();
		normal = new vector(direction3D.a, -direction3D.a*direction3D.a/direction3D.b, 0);
		normal = normal.unit();
		cross = normal.cross(direction3D).unit();

	}

	public void checkIfCrashed(vector targetCentre){
		if(centre.subtract(targetCentre).length() < 15 || time >= 100 || Surface.evaluate(centre.a, centre.b) >= centre.c)
			crashed = true;
	}

	public void addPoints(){
		points[0] = createPoint(width, length, height);
		points[1] = createPoint(-width, length, height);
		points[2] = createPoint(-width, -length, height);
		points[3] = createPoint(width, -length, height);
		points[4] = createPoint(width, length, -height);
		points[5] = createPoint(-width, length, -height);
		points[6] = createPoint(-width, -length, -height);
		points[7] = createPoint(width, -length, -height);
		paintSurface();
	}

	public vector createPoint(int w, int l, int h){
		return centre.add(cross.scale(w)).add(direction3D.scale(l)).add(normal.scale(h)).subtract(new vector(x_shift, y_shift));
	}

	public void paintSurface(){
		vector[] no1 = new vector[]{points[0], points[1], points[2], points[3]};
		paste NO1 = new paste(no1, findCentre(no1), Color.gray);
		vector[] no2 = new vector[]{points[4], points[5], points[6], points[7]};
		paste NO2 = new paste(no2, findCentre(no2), Color.gray);
		vector[] no3 = new vector[]{points[0], points[1], points[5], points[4]};
		paste NO3 = new paste(no3, findCentre(no3),  Color.red);
		vector[] no4 = new vector[]{points[1], points[2], points[6], points[5]};
		paste NO4 = new paste(no4, findCentre(no4),  Color.gray);
		vector[] no5 = new vector[]{points[0], points[4], points[7], points[3]};
		paste NO5 = new paste(no5, findCentre(no5),  Color.gray);
		vector[] no6 = new vector[]{points[2], points[3], points[7], points[6]};
		paste NO6 = new paste(no6, findCentre(no6), Color.orange);
		Paste = new paste[]{NO1, NO2, NO6, NO4, NO5, NO3};
	}

	public vector findCentre(vector[] v){
		return v[0].add(v[1]).scale(0.5).add(v[2].add(v[3]).scale(0.5)).scale(0.5);
	}


	public void draw(Graphics g){
		if(crashed){
			paintTail(g);
			return;
		}
		vector shift = new vector(x_shift, y_shift);
		if(centre.subtract(shift).length() <750){
			if(flame.dot(new vector(1,-2,1)) < Paste[5].position)
				paintTail(g);
			for(int i = 0; i < Paste.length; i++)
				Paste[i].draw(g);
			if(flame.dot(new vector(1,-2,1)) > Paste[5].position)
				paintTail(g);
			paintShadow(g);
		}
	}

	public void paintShadow(Graphics g){
		g.setColor(Color.gray);
		vector v = new vector(points[0].a, points[0].b, Surface.evaluate(points[0].a + x_shift, points[0].b+y_shift));
		vector w = new vector(points[3].a, points[3].b, Surface.evaluate(points[3].a + x_shift, points[3].b+y_shift));
		g.drawLine(v.getX(), v.getY(), w.getX(), w.getY());
	}

	public void paintTail(Graphics g){
		int green = 40, red = 40, blue = 40;
		for(int i = 0; i < time/4 && i < 7; i++){
			g.setColor(new Color(blue, green, red));
			if(i >= disapearTime/4){
				for(int j = 0; j < 4; j++){
					trail[i][j].subtract(new vector(x_shift, y_shift)).draw(g);
				}
			}
			green+=30;
			blue+=30;
			red+=30;
		}
		g.setColor(Color.yellow);
		if(time%4 >1 && !crashed)
			g.fillOval(flame.subtract(new vector(x_shift, y_shift)).getX()- 3, flame.subtract(new vector(x_shift, y_shift)).getY()-3, 7, 7);
	}

	public explosion explode(){
		vector c = new vector(centre.a, centre.b, centre.c - 10);
		if(time >= 100)
			return new explosion(c, new vector(1, -1), 13, "air", x_shift, y_shift);
		else
			return new explosion(c, new vector(Surface.fx(centre.a, centre.b), Surface.fy(centre.a, centre.b)), 25, "ground", x_shift, y_shift);
	}

}

⌨️ 快捷键说明

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