-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBird.pde
104 lines (85 loc) · 2.16 KB
/
Bird.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
abstract class Bird extends HitBody
{
PVector m_pos; float m_rotation;
int m_timer;
int m_timeStop;
int m_durationDying;
// Constructor
Bird(float w, float h)
{
super(HitBodyType.Bird);
m_pos = new PVector(); m_rotation = 0.0;
m_w = w; m_h = h;
m_timer = 0;
m_timeStop = -1; m_durationDying = 100;
}
boolean done()
{
boolean hasDone = false;
if(m_body != null){
Vec2 v = m_body.getLinearVelocity();
if(v.length() < 1.0){
if(m_timeStop < 0){
m_timeStop = m_timer;
}else{
if(m_timer - m_timeStop > m_durationDying){
hasDone = true;
}
}
}else{
if(m_timeStop >= 0) m_timeStop = -1;
}
}
return hasDone;
}
// Drawing the box
void onDisplay()
{
m_timer++;
pushMatrix();
pushStyle();
// Before init the physical properties, use m_pos to locate and render bird
if(m_body != null){
Vec2 pos = box2d.getBodyPixelCoord(m_body);
float a = m_body.getAngle();
onDraw(pos.x, pos.y, -a);
}else{
onDraw(m_pos.x, m_pos.y, m_rotation);
}
popStyle();
popMatrix();
}
abstract void onDraw(float x, float y, float a);
abstract void onEmit();
abstract Shape getShape();
abstract FixtureDef getFixture();
void go(float vel, Vec2 target)
{
makeBody(new Vec2(m_pos.x, m_pos.y), m_w, m_h);
Vec2 bodyVec = m_body.getWorldCenter();
Vec2 worldTarget = box2d.coordPixelsToWorld(target.x, target.y);
worldTarget.subLocal(bodyVec);
worldTarget.normalize();
worldTarget.mulLocal(vel);
m_body.setLinearVelocity(worldTarget);
}
boolean gone()
{
return m_body != null;
}
protected void makeBody(Vec2 center, float w, float h)
{
Shape sd = getShape();
// Define a fixture
FixtureDef fd = getFixture();
fd.shape = sd;
// Define the body and make it from the shape
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld(center));
m_body = box2d.createBody(bd);
m_body.createFixture(fd);
m_body.setUserData(this);
//body.setMassFromShapes();
}
}