forked from Pomax/Pjs-2D-Game-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoundedInteractor.pde
executable file
·115 lines (100 loc) · 2.96 KB
/
BoundedInteractor.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
/**
* A bounded interactor is a normal Interactor with
* one or more boundaries associated with it.
*/
abstract class BoundedInteractor extends Interactor implements BoundaryCollisionListener {
// the list of associated boundaries
ArrayList<Boundary> boundaries;
// are the boundaries active?
boolean bounding = true;
// simple constructor
BoundedInteractor(String name) { this(name,0,0); }
// full constructor
BoundedInteractor(String name, float dampening_x, float dampening_y) {
super(name, dampening_x, dampening_y);
boundaries = new ArrayList<Boundary>();
}
// add a boundary
void addBoundary(Boundary boundary) {
boundary.setImpulseCoefficients(ixF,iyF);
boundaries.add(boundary);
}
// add a boundary, and register as listener for collisions on it
void addBoundary(Boundary boundary, boolean listen) {
addBoundary(boundary);
boundary.addListener(this);
}
// FIXME: make this make sense, because setting 'next'
// should only work on open-bounded interactors.
void setNext(BoundedInteractor next) {
if(boundaries.size()==1) {
boundaries.get(0).setNext(next.boundaries.get(0));
}
}
// FIXME: make this make sense, because setting 'previous'
// should only work on open-bounded interactors.
void setPrevious(BoundedInteractor prev) {
if(boundaries.size()==1) {
boundaries.get(0).setPrevious(prev.boundaries.get(0));
}
}
// enable all boundaries
void enableBoundaries() {
bounding = true;
for(Boundary b: boundaries) {
b.enable();
}
}
// disable all boundaries
void disableBoundaries() {
bounding = false;
for(int b=boundaries.size()-1; b>=0; b--) {
Boundary boundary = boundaries.get(b);
boundary.disable();
}
}
/**
* We must make sure to remove all
* boundaries when we are removed.
*/
void removeActor() {
disableBoundaries();
boundaries = new ArrayList<Boundary>();
super.removeActor();
}
// draw boundaries
void drawBoundaries(float x, float y, float w, float h) {
for(Boundary b: boundaries) {
b.draw(x,y,w,h);
}
}
/**
* Is something attached to one of our boundaries?
*/
boolean havePassenger() {
// no passengers
return false;
}
/**
* listen to collisions on bounded boundaries
*/
abstract void collisionOccured(Boundary boundary, Actor actor, float[] intersectionInformation);
// when we update our coordinates, also
// update our associated boundaries.
void update() {
super.update();
// how much did we actually move?
float dx = x-previous.x;
float dy = y-previous.y;
// if it's not 0, move the boundaries
if(dx!=0 && dy!=0) {
for(Boundary b: boundaries) {
// FIXME: somehow this goes wrong when the
// interactor is contrained by another
// boundary, where the actor moves, but the
// associated boundary for some reason doesn't.
b.moveBy(dx,dy);
}
}
}
}