-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPig.pde
119 lines (97 loc) · 2.96 KB
/
Pig.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
abstract class Pig extends HitBody
{
int m_timer;
int m_timeDying;
int m_durationDying;
int m_hp;
// Constructor
Pig(float x, float y, float w, float h)
{
super(HitBodyType.Pig);
m_w = w; m_h = h;
m_enableCollision = true;
makeBody(new Vec2(x, y), m_w, m_h);
m_timer = 0;
m_timeDying = -1; m_durationDying = 30;
}
boolean isDying(){return m_timeDying >= 0;}
void onImpulseCollision(ContactImpulse impulse)
{
float impulseVal = maximumNormalImpulse(impulse);
float threshold = 10000;
if(impulseVal > threshold){
float harm = (impulseVal - threshold) / 20;
m_hp -= harm;
}
}
boolean done()
{
boolean hasDone = false;
/*ContactEdge ce = m_body.getContactList();
while(ce != null){
Contact c = ce.contact;
ContactEdge prev = ce.prev;
ce = ce.next;
if(prev == null) continue;
WorldManifold worldManifold = new WorldManifold(); c.getWorldManifold(worldManifold);
//println("World", worldManifold.normal);
Collision.PointState[] state1 = new Collision.PointState[2];
Collision.PointState[] state2 = new Collision.PointState[2];
Manifold oldManifold = prev.contact.getManifold();
Collision.getPointStates(state1, state2, oldManifold, c.getManifold());
//println("State", state2[0]);
if(state2[0] != Collision.PointState.NULL_STATE && state2[0] != Collision.PointState.REMOVE_STATE){
Body bodyA = c.getFixtureA().getBody();
Body bodyB = c.getFixtureB().getBody();
Vec2 point = worldManifold.points[0];
Vec2 vA = bodyA.getLinearVelocityFromWorldPoint(point);
Vec2 vB = bodyB.getLinearVelocityFromWorldPoint(point);
float approachVel = abs(Vec2.dot(vB.sub(vA), worldManifold.normal));
float harm = approachVel * 200; m_hp -= harm;
if(approachVel > 0.0){
m_hp -= approachVel * 200;
}
}
}*/
if(m_hp <= 0){
if(m_timeDying < 0){
m_timeDying = m_timer;
}else{
if(m_timer - m_timeDying > m_durationDying){
hasDone = true;
}
}
}
return hasDone;
}
// Drawing the box
void onDisplay()
{
m_timer++;
Vec2 pos = box2d.getBodyPixelCoord(m_body);
float a = m_body.getAngle();
pushMatrix();
pushStyle();
onDraw(pos.x, pos.y, -a);
popStyle();
popMatrix();
}
abstract void onDraw(float x, float y, float a);
abstract Shape getShape();
abstract FixtureDef getFixture();
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();
}
}