-
Notifications
You must be signed in to change notification settings - Fork 1
/
Ship.pde
177 lines (161 loc) · 3.82 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//----------------------------------------------------------
// Ship class
//----------------------------------------------------------
class Ship {
// fields
float x, y, speed;
String dirFlag;
BulletEmitter emitter;
ParticleSystem ps;
Ship(float x, float y){
this.x = x;
this.y = y;
this.dirFlag = "w";
this.speed = 10;
emitter = new BulletEmitter();
ps = new ParticleSystem(x,y,30,new color[]{color(255,255,0),color(255,100,0),color(255,200,0)});
}
void update(){
shipCollision();
// Fire Bullets!!!
if(keyInput.get('o')){
emitter.direction_player_is_facing(bullets, new float[]{x,y}, 2.5*player.speed, 1.0, millis()/100);
}
// Drop Bombs
if(keyInput.get('p')){
emitter.bomb_behind(bombs, new float[]{x,y}, 1.2*player.speed, 1.0, millis()/100);
}
// Animate bullets
ArrayList<Bullet> clearList = new ArrayList<Bullet>();
for(int i=0; i<bullets.size(); i++){
Bullet b = bullets.get(i);
if(b.y > (player.y-len) && b.y < (player.y+len) &&
b.x > (player.x-len) && b.x < (player.x+len)){
b.display();
b.update();
if(playerBulletCollision(b)){
clearList.add(b);
}
} else{
clearList.add(b);
}
}
// Animate Bombs
// Animate bullets
ArrayList<Bomb> clearList2 = new ArrayList<Bomb>();
for(int i=0; i<bombs.size(); i++){
Bomb b = bombs.get(i);
if(b.y > (player.y-len) && b.y < (player.y+len) &&
b.x > (player.x-len) && b.x < (player.x+len)){
b.display();
b.update();
// note: no collision yet
} else{
clearList2.add(b);
}
}
// clear bullets that collided
bullets.removeAll(clearList);
// set direction:
if(keyInput.get('w'))
dirFlag = "w";
if(keyInput.get('a'))
dirFlag = "a";
if(keyInput.get('s'))
dirFlag = "s";
if(keyInput.get('d'))
dirFlag = "d";
// north
if(dirFlag == "w")
y-=speed;
//west
if(dirFlag == "a")
x-=speed;
// south
if(dirFlag == "s")
y+=speed;
// east
if(dirFlag == "d")
x+=speed;
ps.update();
}
void display(){
noStroke();
rectMode(CENTER);
pushMatrix();
switch(dirFlag){
case "w":
shipSprite();
break;
case "a":
translate(x,y);
rotate(radians(-90));
translate(-x,-y);
shipSprite();
break;
case "s":
translate(x,y);
rotate(radians(180));
translate(-x,-y);
shipSprite();
break;
case "d":
translate(x,y);
rotate(radians(90));
translate(-x,-y);
shipSprite();
break;
}
popMatrix();
rectMode(CORNER);
}
// private function that draws ship sprite
void shipSprite(){
fill(255);
// tall part
rect(x,y-10,5,45);
// 2nd tall part
rect(x,y+5,15,60);
// chubby white middle
rect(x,y+5,25,45);
// left side
rect(x-14,y+5,5,20);
rect(x-18,y+7,5,15);
rect(x-22,y+5,5,50);
rect(x-27,y+15,5,15);
rect(x-31,y+12,5,20);
// right side
rect(x+14,y+5,5,20);
rect(x+18,y+7,5,15);
rect(x+22,y+5,5,50);
rect(x+27,y+15,5,15);
rect(x+31,y+12,5,20);
//red part
fill(255,0,0,255);
//red middle sqaure
rect(x,y,15,10);
//left middle
rect(x-5,y+7,5,5);
//right middle
rect(x+5,y+7,5,5);
//top middle
rect(x,y-7,5,10);
//bottom red
rect(x,y+35,15,2.5);
//right bottom
rect(x+29,y+23,9,2.5);
//left bottom
rect(x-29,y+23,9,2.5);
//right top
rect(x+22,y-21,5,2.5);
//left top
rect(x-22,y-21,5,2.5);
}
void die(){
numLives--;
ps.setXY(x,y);
ps.act();
if(numLives == 0){
}
}
}