-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShip.pde
122 lines (121 loc) · 3.21 KB
/
Ship.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class Ship {
PVector loc;
PVector velocity;
PVector acceleration;
float heading;
float angularVelocity;
float size;
Score score;
Gun l, r;
HitBox hitBox;
DigitDisplay ammo;
int bullet;
Ship(PVector loc_, float size_) {
loc = loc_;
velocity = new PVector(0, 0);
acceleration = new PVector(0, 0);
heading = 0;
angularVelocity = 0;
size = size_;
l = new Gun(new PVector(-size/2, 0), size);
r = new Gun(new PVector(size/2, 0), size);
hitBox = new HitBox(loc, size);
score = new Score(new PVector(50, 50), 4);
ammo = new DigitDisplay(new PVector(50, 100), 4, 100);
bullet = 1;
}
void display() {
pushMatrix();
//bounce();
rollOver();
translate(loc.x, loc.y);
rotate(radians(heading));
l.display();
r.display();
fill(255, 80);
triangle(0, -size, size, size/2, -size, size/2);
fill(0, 255, 0);
ellipse(0, 0, 4, 4);
popMatrix();
l.dispBullets();
r.dispBullets();
hitBox.display();
score.display();
}
void fire() { //<>//
l.addBullet(PVector.add(loc, l.loc), PVector.fromAngle(radians(heading-90))); //<>//
r.addBullet(PVector.add(loc, r.loc), PVector.fromAngle(radians(heading-90))); //<>//
}
void update() {
velocity.add(acceleration);
loc.add(velocity);
PVector target = PVector.sub(new PVector(mouseX, mouseY), loc).normalize();
heading = degrees(target.heading())+90;
acceleration.mult(0);
velocity.mult(0.99);
hitBox.update(loc);
}
void run() {
if (!paused && !gameOver) update();
display();
}
void reset() {
loc = new PVector(width/2, height-50);
velocity.mult(0);
heading = 0;
}
void rotateLeft() {
heading+=10;println(loc.heading()*180/PI);
}
void rotateRight() {
heading-=10;
println(loc.heading()*180/PI);
}
void moveForward() {
//acceleration = PVector.fromAngle(radians(heading-90)).mult(0.5);
acceleration.add(new PVector(0, -MAXACC));
}
void moveBackward() {
//acceleration.add(PVector.fromAngle(radians(heading-90)).mult(-0.5));
acceleration.add(new PVector(0, MAXACC));
}
void moveLeft() {
acceleration.add(new PVector(-MAXACC, 0));
}
void moveRight() {
acceleration.add(new PVector(MAXACC, 0));
}
void bounce() {
if(loc.x>width || loc.x<0) velocity.x *= -0.5;
if(loc.y>height || loc.y<0) velocity.y *= -0.5;
loc = new PVector(constrain(loc.x, 0, width) , constrain(loc.y, 0, height));
}
void rollOver() {
if(loc.x>width) loc.x = 0;
if(loc.y>height) loc.y = 0;
if(loc.x<0) loc.x = width;
if(loc.y<0) loc.y = height;
}
boolean hasHit(Asteroid a) {
return hitBox.intersects(a);
}
void changeGun(int x) {
switch(x) {
case 1:
l = new Gun(new PVector(-size/2, 0), size, l.clip);
r = new Gun(new PVector(size/2, 0), size, r.clip);
bullet = 1;
break;
case 2:
l = new FastGun(new PVector(-size/2, 0), size, l.clip);
r = new FastGun(new PVector(size/2, 0), size, r.clip);
bullet = 2;
break;
case 3:
l = new ExplodingGun(new PVector(-0, -size/2), size, l.clip);
r = new EmptyGun(new PVector(size/2, 0), size, r.clip);
bullet = 3;
break;
}
}
}