-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollider.pde
153 lines (127 loc) · 4.58 KB
/
Collider.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
interface ICollidable<T>{
void manageCollision(ICollidable against);
PVector getPosition();
PVector getSize();
void manageCollider();
void toggleColliderDisplay(Camera camera, int layer, boolean toggleOn);
ColliderType getColliderType();
boolean isSolid();
PVector getVelocity();
void setVelocity(PVector _velocity);
float getMass();
String getTag();
void setTag(String _tag);
HashMap<ICollidable, CollisionInfo> getCollidedWith();
void addCollidedWith(ICollidable collider, CollisionInfo collisionInfo);
void setCollided(boolean hasCollided);
boolean hasCollided();
void resolveCollision(ICollidable against);
PVector[] getVertices();
boolean hasMoved();
PhysicsBody getPhysicsBody();
float getRestitutionCoeff();//0 to 1
}
public enum ColliderType { SQUARE, CIRCLE, TRIANGLE, POINT, LINE, POLYGON };
abstract class Collider extends Component implements IGraphic, ICollidable{
boolean isHit = false;
boolean collidedAgainstSolid = false;
boolean isSolid;
boolean hasMoved = true;
ColliderType colliderType;
Map<Camera, Integer> cameras = new HashMap();//Camera, Layer
boolean isCulled;
PVector relativePosition = PVector.zero();
PVector movementBuffer = PVector.zero();
PhysicsBody physicsBody;
float restitutionCoeff = 0.1f;
HashMap<ICollidable, CollisionInfo> collidedWith = new HashMap();
public void attachCamera(Camera camera, int layer){
//if (cameras.get(camera) != null)
cameras.put(camera, layer);
}
public void addToGraphicsList(Camera camera, int layer){
attachCamera(camera, layer);
setLayer(camera, layer);
camera.addToGraphicsList(this, layer);
}
public void setTag(String _tag){
gameObject.tag = _tag;
}
public String getTag() { return gameObject.tag; }
float getRestitutionCoeff() { return restitutionCoeff; }
float getMass() { return physicsBody != null? physicsBody.mass : 1; }
PVector getVelocity() { return physicsBody != null? physicsBody.getVelocity() : PVector.zero(); }
void setVelocity(PVector _velocity){ if (physicsBody != null) physicsBody.velocity = _velocity; }
void addPhysicsBody(PhysicsBody pb){
physicsBody = pb;
}
PhysicsBody getPhysicsBody(){
return physicsBody;
}
boolean isSolid(){ return isSolid; }
HashMap<ICollidable, CollisionInfo> getCollidedWith(){ return collidedWith; }
public PVector getPosition(){
PVector position = gameObject.transform.getPosition();
position.add(relativePosition);
return position;
}
public void removeFromGraphicsList(Camera camera, int layer){
camera.removeFromGraphicsList(this, cameras.get(camera));
}
public void setLayer(Camera camera, int _layer){
camera.removeFromGraphicsList(this, cameras.get(camera));
Integer layer = cameras.get(camera);
layer = _layer;
camera.addToGraphicsList(this, cameras.get(camera));
}
void addCollidedWith(ICollidable collider, CollisionInfo collisionInfo){
collidedWith.put(collider, collisionInfo);
}
void setCollided(boolean hasCollided){ if (physicsBody != null) physicsBody.hasCollided = hasCollided; }
boolean hasCollided(){ return isHit; }
public boolean isCulled(){
return false;//isCulled;
}
public void toggleColliderDisplay(Camera camera, int layer, boolean toggleOn){
if (toggleOn)
addToGraphicsList(camera, layer);
else
removeFromGraphicsList(camera, layer);
}
public void manageCulling(Camera camera){
/*if (gameObject.transform.getPosition() != gameObject.transform.getPreviousPosition())
checkCulling(camera);*/
}
public void checkCulling(Camera camera){
PVector currentPos = gameObject.transform.getPosition();
currentPos.add(camera.getPosition());
PVector currentSize = gameObject.transform.size.get();
isCulled = camera.isInFrame(currentPos, currentSize, camera.getPosition());
}
Collider(GameObject _gameObject, boolean _isSolid){
super(_gameObject, Collider.class);
GameEngine.addToColliderList(this);
isSolid = _isSolid;
}
void onDestroy(){
}
public ColliderType getColliderType(){
return colliderType;
}
abstract void setSize(PVector _size);
boolean hasMoved(){ return hasMoved; }
void manageCollider(){
isHit = false;
collidedAgainstSolid = false;
PVector currentPosition = getPosition();
collidedWith.clear();
PVector diff = PVector.sub(movementBuffer, currentPosition);
if (!(diff.x == 0 && diff.y == 0) || physicsBody != null){
hasMoved = true;
}
else{
hasMoved = false;
}
movementBuffer = currentPosition;
}
}