-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectile.java
More file actions
80 lines (70 loc) · 2.55 KB
/
Projectile.java
File metadata and controls
80 lines (70 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.awt.*;
/**
* The Projectile class contains data on the player
* ship's weapon to destroy asteroids.
*
* @author Herman Lin
* @author Devin Zhu
*/
public class Projectile extends Polygon {
// Data on the Projectile Polygon
private static final int[] xcoords = {-10,-8,-8,-10};
private static final int[] ycoords = {-10,-10,-8,-8};
private static final int nVertex = 4;
// The color of the projectile
private Color color;
// Position of the projectile
private double positionx;
private double positiony;
// Velocity of the projectile (set speed of 5)
private double velocity = 5;
// Direction that the projectile moves in
private double direction;
// Boolean for determining when the projectile is offscreen/destroyed
private boolean alive = true;
/**
* Constructor for creating a new Projectile
*
* @param color the color of the projectile
* @param direction the direction the projectile moves
* @param posx the starting x-coord of the projectile
* @param posy the starting y-coord of the projectile
*/
Projectile(Color color, double direction, double posx, double posy) {
super(xcoords, ycoords, nVertex);
this.color = color;
this.direction = direction;
positionx = posx;
positiony = posy;
}
// Getters
public Polygon getProjectile() { return this; }
public double getPositionX() { return positionx; }
public double getPositionY() { return positiony; }
public double getDirection() { return direction; }
public boolean isAlive() { return alive; }
public void destroy() { alive = false; }
public Rectangle getBounds() {
return new Rectangle((int)positionx, (int)positiony, 2, 2);
}
/**
* Moves the projectile a specific velocity in a
* specific direction.
*/
public void move() {
positionx += velocity * Math.cos(Math.toRadians(direction + 90));
positiony += velocity * Math.sin(Math.toRadians(direction + 90));
// if the projectile goes off screen, make it unalive
if (positionx < 0 ||
positionx > SpaceRangers.SCREEN_WIDTH ||
positiony < 0 ||
positiony > SpaceRangers.SCREEN_HEIGHT) { alive = false; }
}
public String dataString() {
return positionx + " " + positiony + " " + direction + " ";
}
public String toString() {
return "Projectile | X: " + positionx + ", Y: " + positiony +
", Direction: " + direction + " degrees";
}
}