-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullet.pde
210 lines (153 loc) · 4.67 KB
/
Bullet.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
abstract class Bullet extends Entity {
Entity source;
float radius;
float vx;
float vy;
float damage;
PImage[] dead_anim;
// x/y pixel coords
// vx/vy world scalars
// r pixel scalar
public Bullet(float x, float y, float vx, float vy, float r, Entity s) {
radius = r;
this.vx = vx;
this.vy = vy;
this.x = x;
this.y = y;
this.source = s;
frame_last_hit = -1;
}
void destroyBody() {
super.destroyBody();
Vec2 vel = walkbox.getLinearVelocity();
vx = vel.x;
vy = vel.y;
}
void create() {
Vec2 center = box2d.coordPixelsToWorld(x, y);
BodyDef walkboxbd = new BodyDef();
walkboxbd.position.set(center);
walkboxbd.type = BodyType.DYNAMIC;
walkboxbd.fixedRotation = true;
// set friction:
// bd.linearDamping = ...;
// bd.angularDamping = ...;
// if body is moving fast:
// bd.bullet = ...;
walkbox = box2d.createBody(walkboxbd);
// inital starting state:
walkbox.setLinearVelocity(new Vec2(vx, vy));
// body.setAngularVelocity(1.2);
CircleShape walkboxps = new CircleShape();
float world_radius = box2d.scalarPixelsToWorld(radius);
walkboxps.m_radius = world_radius;
FixtureDef walkboxfd = new FixtureDef();
walkboxfd.shape = walkboxps;
// set pararaters that affect physics of shape:
// fd.friction = ...;
// fd.restitution = ...;
// fd.density = ...;
walkboxfd.isSensor = true;
walkbox.createFixture(walkboxfd);
// can directly create fixture w/ shape & density:
// body.createFixture(ps, 1);
walkbox.setLinearVelocity(new Vec2(vx, vy));
walkbox.setUserData(new UserData(this, DataType.WALKBOX));
walkbox.setSleepingAllowed(false);
}
void step() {
// no need to do anything
}
abstract void show();
// draws the player's movement hitbox as a green rectangle
public void showHitbox() {
pushMatrix();
pushStyle();
stroke(0, 0, 255);
strokeWeight(2);
noFill();
Vec2 pos = box2d.getBodyPixelCoord(walkbox);
ellipse(pos.x, pos.y, radius*2, radius*2);
popStyle();
popMatrix();
}
void hit(Bullet bullet) {
walkbox.setLinearVelocity(new Vec2(0, 0));
frame_last_hit = frameCount;
}
}
class FireBullet extends Bullet {
public FireBullet(float x, float y, float vx, float vy, float r, Entity s) {
super(x, y, vx, vy, r, s);
run_anim = pixel_effects_fire;
dead_anim = explosion_6;
damage = 1;
}
void show() {
pushMatrix();
pushStyle();
Vec2 pos = box2d.getBodyPixelCoord(walkbox);
Vec2 vel = walkbox.getLinearVelocity();
translate(pos.x, pos.y);
imageMode(CENTER);
if (frame_last_hit == -1) {
rotate(atan2(-vel.y, vel.x) - PI/2);
translate(-radius/2, -radius*20/4);
image(run_anim[frameCount%run_anim.length], 0, 0, radius*22, radius*22);
} else {
image(dead_anim[((frameCount-frame_last_hit)/3)%dead_anim.length], 0, 0, radius*5, radius*5);
}
popMatrix();
popStyle();
if (frame_last_hit != -1 && (frameCount + 1 - frame_last_hit)/3 >= dead_anim.length)
toDestroy.add(this);
}
}
class BlueCircleBullet extends Bullet {
public BlueCircleBullet(float x, float y, float vx, float vy, float r, Entity s) {
super(x, y, vx, vy, r, s);
run_anim = new PImage[] {circle_bullet_blue};
dead_anim = explosion_1;
damage = 0;
}
void show() {
pushMatrix();
pushStyle();
Vec2 pos = box2d.getBodyPixelCoord(walkbox);
translate(pos.x, pos.y);
imageMode(CENTER);
if (frame_last_hit == -1) {
image(run_anim[frameCount%run_anim.length], 0, 0, radius*2, radius*2);
} else {
image(dead_anim[((frameCount-frame_last_hit)/3)%dead_anim.length], 0, 0, radius*2, radius*2);
}
popMatrix();
popStyle();
if (frame_last_hit != -1 && (frameCount + 1 - frame_last_hit)/3 >= dead_anim.length)
toDestroy.add(this);
}
}
class GreenCircleBullet extends Bullet {
public GreenCircleBullet(float x, float y, float vx, float vy, float r, Entity s) {
super(x, y, vx, vy, r, s);
run_anim = new PImage[] {circle_bullet_green};
dead_anim = explosion_1;
damage = 100;
}
void show() {
pushMatrix();
pushStyle();
Vec2 pos = box2d.getBodyPixelCoord(walkbox);
translate(pos.x, pos.y);
imageMode(CENTER);
if (frame_last_hit == -1) {
image(run_anim[frameCount%run_anim.length], 0, 0, radius*2, radius*2);
} else {
image(dead_anim[((frameCount-frame_last_hit)/3)%dead_anim.length], 0, 0, radius*4, radius*4);
}
popMatrix();
popStyle();
if (frame_last_hit != -1 && (frameCount + 1 - frame_last_hit)/3 >= dead_anim.length)
toDestroy.add(this);
}
}