-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpaceship.pde
137 lines (121 loc) · 3.08 KB
/
Spaceship.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class Spaceship {
//Config
float rotSpeed=0.09;
float accel = 0.046;
//float decay = 0.004; //Not working, the logic is wrong, remove it for now
int gunCooldownTime = 16;//frames
int respawnTime = 120; //frames
float gunOrigin = 14; //Pixels in X from center
//shipState
PVector position;
PVector force;
float angle;
boolean accelerating = false;
float gunCooldown = gunCooldownTime;
float respawnTimer = 0;
ParticleSystem explode;
Spaceship(float x, float y) {
position = new PVector(x, y);
force = new PVector();
audioMgr.loadFile("thruster", "thrust.wav");
audioMgr.loadFile("shoot", "shoot.wav");
audioMgr.loadFile("shipexplode", "shipexplode.wav");
}
void update() {
if (gunCooldown > 0) {
gunCooldown--;
}
//Check if we have recently died and have to respawn the ship
if (respawnTimer > 0) {
respawnTimer--;
if (respawnTimer <= 0) {
respawnShip();
}
}
//Process Movement
if (forward) {
force.x += accel*cos(angle);
force.y += accel*sin(angle);
accelerating = true;
audioMgr.loop("thruster");
} else {
audioMgr.stop("thruster");
accelerating=false;
}
//Position
position.x += force.x;
position.y += force.y;
position.x = position.x%width;
position.y = position.y%height;
if (position.x < 0) {
position.x = width + position.x;
}
if (position.y < 0) {
position.y = height + position.y;
}
//Rotation
if (left) {
angle -= rotSpeed;
}
if (right) {
angle += rotSpeed;
}
angle = angle% TWO_PI;
}
/**
* Resets all the variables so that the ship returns into neutral position.
*/
void respawnShip() {
position.x = width/2;
position.y = height/2;
force.x = 0.0;
force.y = 0.0;
angle = 0;
}
/**
* Shoots a bullet in the direction the ship is facing and returns it
*/
Bullet shoot() {
if (gunCooldown <= 0) {
gunCooldown = gunCooldownTime;
audioMgr.play("shoot");
return new Bullet(position.x+cos(angle)*gunOrigin, position.y+sin(angle)*gunOrigin, angle);
} else {
return null;
}
}
boolean isInvulnerable() {
//println("Ship is invulnerable="+ (respawnTimer > 0));
return respawnTimer > 0;
}
//Draw the ship to the screen
void drawToScreen() {
if (isInvulnerable()) { //Don't draw the ship if it is invulnerable. TODO: This isn't cute. I should show it.
explode.run();
return;
}
pushMatrix();
translate(position.x, position.y);
rotate(angle);
//ship
noFill();
strokeWeight(1);
line(-15, -10, 15, 0);
line(-15, 10, 15, 0);
ellipseMode(CENTER);
arc(-44, 0, 60, 60, -0.34, 0.34);
//flame
if (accelerating) {
translate(-15, 0);
line(0, -6, -11-2*sin(frameCount*2), 0);
line(0, 6, -11-2*sin(frameCount*2), 0);
}
popMatrix();
}
void destroy() {
respawnTimer = respawnTime;
audioMgr.play("shipexplode");
explode = new ParticleSystem();
explode.start(position.x, position.y, 30);
}
}