forked from TrevorAPCS/AsteroidsGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTargeter.pde
79 lines (79 loc) · 2.47 KB
/
Targeter.pde
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
class Targeter extends Spaceship{
private Spaceship target;
private double mySpeed;
private int maxRotation;
public Targeter(int x, int y, int dir, int c, Spaceship t){
myCenterX = x;
myCenterY = y;
myPointDirection = dir;
myColor = 255;
myDefaultColor = myColor;
corners = c;
myXspeed = 0;
myYspeed = 0;
xCorners = new int[]{10, -10, -5, -10};
yCorners = new int[]{0, 5, 0, -5};
accelerating = false;
reversing = false;
health = 300;
target = t;
mySpeed = 1;
maxRotation = 1;
thrusterTimer = 0;
thrusterTimerRequirement = 10;
}
public void targetShip(){
double targetDirection = Math.toDegrees(Math.atan2(target.getY() - myCenterY, target.getX() - myCenterX));
boolean playerBehind = myCenterX - target.getX() > 0;
stroke(255, 255, 255);
line((float)myCenterX, (float)myCenterY, (float)(myCenterX + 1000*Math.cos(Math.toRadians(targetDirection))), (float)(myCenterY + 1000*Math.sin(Math.toRadians(targetDirection))));
stroke(0);
if(targetDirection > 180){targetDirection -= 180;}
if(targetDirection < -180){targetDirection += 180;}
if(playerBehind){
if(targetDirection >= 0){
targetDirection = 180 - targetDirection;
}
else{
targetDirection = -180 - targetDirection;
}
if(myPointDirection >= 0){
myPointDirection = 180 - myPointDirection;
}
else{
myPointDirection = -180 - myPointDirection;
}
}
if(Math.abs(targetDirection - myPointDirection) <= maxRotation){
myPointDirection = targetDirection;
}
else if(targetDirection - myPointDirection > maxRotation){
myPointDirection += maxRotation;
}
else if(targetDirection - myPointDirection < -maxRotation){
myPointDirection -= maxRotation;
}
if(playerBehind){
if(myPointDirection >= 0){
myPointDirection = 180 - myPointDirection;
}
else{
myPointDirection = -180 - myPointDirection;
}
}
if(myPointDirection > 180){myPointDirection -= 180;}
if(myPointDirection < -180){myPointDirection += 180;}
}
public void moveShip(){
if(thrusters){
setAccelerating(true);
double dRadians =myPointDirection*(Math.PI/180);
myXspeed = mySpeed * Math.cos(dRadians);
myYspeed = mySpeed * Math.sin(dRadians);
}
move();
}
public Spaceship getTarget(){
return target;
}
}