-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapEntity.pde
94 lines (80 loc) · 1.77 KB
/
MapEntity.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
class MapEntity {
float w;
float h;
color col;
Vec2 pos;
Body body;
int type;
MapEntity(float x, float y, float w_, float h_, int type_) {
w = w_;
h = h_;
type = type_;
makeBody(x, y, w, h, type);
body.setUserData(this);
col = color(175);
}
void killBody() {
box2d.destroyBody(body);
}
MapEntity change() {
col = color(255, 0, 0);
return this;
}
MapEntity changeBack() {
col = color(150);
return this;
}
void display() {
pos = box2d.getBodyPixelCoord(body);
pushMatrix();
pushStyle();
translate(pos.x, pos.y);
fill(col);
strokeWeight(1);
if (type == 2) {
stroke(0);
ellipse(0, 0, w, h);
} else {
noStroke();
rectMode(CENTER);
rect(0, 0, w, h);
}
popStyle();
popMatrix();
}
void makeBody(float x, float y, float w, float h, int type) {
// Create the body
BodyDef bd = new BodyDef();
bd.position.set(box2d.coordPixelsToWorld(x, y));
if (type == 2) {
bd.type = BodyType.DYNAMIC;
w -=4;
h -=4;
} else {
bd.type = BodyType.STATIC;
}
body = box2d.createBody(bd);
Shape sd;
if (type == 2) {
sd = new CircleShape();
((CircleShape)sd).m_radius = box2d.scalarPixelsToWorld(w/2);
} else {
sd = new PolygonShape();
float box2dW = box2d.scalarPixelsToWorld(w/2);
float box2dH = box2d.scalarPixelsToWorld(h/2);
((PolygonShape)sd).setAsBox(box2dW, box2dH);
}
FixtureDef fd = new FixtureDef();
fd.shape = sd;
// Parameters that affect physics
fd.density = 100;
fd.friction = 0.1;
fd.restitution = 0.1;
// Attach fixture to body
body.createFixture(fd);
}
MapEntity setColor(color c) {
col = c;
return this;
}
}