-
Notifications
You must be signed in to change notification settings - Fork 7
/
SketchStateMachineWithObjects.java
executable file
·186 lines (145 loc) · 5.07 KB
/
SketchStateMachineWithObjects.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package de.hfkbremen.algorithmiccliches.examples;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PVector;
import java.util.ArrayList;
public class SketchStateMachineWithObjects extends PApplet {
private final ArrayList<Entity> mEntities = new ArrayList<>();
public void settings() {
size(1024, 768, P3D);
}
public void setup() {
rectMode(CENTER);
for (int i = 0; i < 100; i++) {
mEntities.add(new Entity(this));
}
}
public void draw() {
final float mDelta = 1.0f / frameRate;
background(255);
for (Entity m : mEntities) {
m.update(mDelta);
m.draw(g);
}
}
public class Entity {
static final float IDEAL_SCALE = 20.0f;
PVector position = new PVector();
int entity_color;
float speed;
float scale;
private State mState;
public Entity(PApplet pPApplet) {
switchState(new StateFollowMouse(this, pPApplet));
position.set(pPApplet.random(pPApplet.width), pPApplet.random(pPApplet.height));
speed = pPApplet.random(1, 5) * 20;
scale = IDEAL_SCALE;
}
public void update(final float pDelta) {
mState.update(pDelta);
}
public void draw(PGraphics g) {
g.noStroke();
g.fill(entity_color);
g.pushMatrix();
g.translate(position.x, position.y);
g.ellipse(0, 0, scale, scale);
if (mState instanceof StateBrownianMotion) {
g.stroke(entity_color);
g.line(scale, scale, -scale, -scale);
g.line(-scale, scale, scale, -scale);
}
g.popMatrix();
}
public final void switchState(State pState) {
if (mState != null) {
mState.done();
}
mState = pState;
mState.setup();
}
}
public abstract class State {
protected final Entity entity;
protected final PApplet sketch;
public State(Entity pParent, PApplet pPApplet) {
entity = pParent;
sketch = pPApplet;
}
public abstract void setup();
public abstract void update(final float pDelta);
public abstract void done();
}
public class StateBrownianMotion
extends State {
private static final float STATE_DURATION = 4.0f;
private static final float BROWNIAN_SPEED = 15.0f;
private float mStateTime;
public StateBrownianMotion(Entity pParent, PApplet pPApplet) {
super(pParent, pPApplet);
}
public void setup() {
entity.entity_color = color(255, 127, 0, 127);
}
public void update(final float pDelta) {
mStateTime += pDelta;
if (mStateTime > STATE_DURATION) {
entity.switchState(new StateChangeRandomly(entity, sketch));
} else {
entity.position.add(sketch.random(-BROWNIAN_SPEED, BROWNIAN_SPEED),
sketch.random(-BROWNIAN_SPEED, BROWNIAN_SPEED));
}
}
public void done() {
}
}
public class StateChangeRandomly
extends State {
private static final float STATE_DURATION = 1.5f;
private float mStateTime;
public StateChangeRandomly(Entity pParent, PApplet pPApplet) {
super(pParent, pPApplet);
}
public void setup() {
}
public void update(float pDelta) {
mStateTime += pDelta;
if (mStateTime > STATE_DURATION) {
entity.switchState(new StateFollowMouse(entity, sketch));
} else {
entity.entity_color = color(sketch.random(127, 255), sketch.random(127, 255), 0, 127);
entity.scale = sketch.random(50, 100);
}
}
public void done() {
}
}
public class StateFollowMouse
extends State {
private static final float MIN_DISTANCE = 10.0f;
public StateFollowMouse(Entity pParent, PApplet pPApplet) {
super(pParent, pPApplet);
}
public void setup() {
entity.entity_color = color(0, 127, 255, 127);
}
public void update(float pDelta) {
PVector mDiff = PVector.sub(new PVector(sketch.mouseX, sketch.mouseY), entity.position);
if (mDiff.mag() < MIN_DISTANCE) {
entity.switchState(new StateBrownianMotion(entity, sketch));
} else {
entity.scale += (Entity.IDEAL_SCALE - entity.scale) * pDelta;
mDiff.normalize();
mDiff.mult(pDelta);
mDiff.mult(entity.speed);
entity.position.add(mDiff);
}
}
public void done() {
entity.scale = Entity.IDEAL_SCALE;
}
}
public static void main(String[] args) {
PApplet.main(new String[]{SketchStateMachineWithObjects.class.getName()});
}
}