-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.pde
150 lines (129 loc) · 4.22 KB
/
Window.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
class Window implements CompletionCallback, GuiCallback {
final int RESET_BUTTON_WIDTH = 100;
final int RESET_BUTTON_HEIGHT = 60;
private int ballCount;
private int healthyCount;
private int recoveredCount;
private int infectedCount;
private boolean simulationComplete = true;
private Chart chart;
private ArrayList<Ball> balls;
private PFont SFFont_25;
private PFont SFFont_14;
private Gui gui;
Window(PApplet applet) {
this.gui = new Gui(applet, this);
this.SFFont_25 = loadFont("SFProDisplay-Light-25.vlw");
this.SFFont_14 = loadFont("SFProDisplay-Light-14.vlw");
}
void setup() {
gui.setupGui();
}
void resetView() {
ballCount = gui.getBallCountFromTextField();
balls = createBalls();
chart = new Chart(ballCount, gui.getDurationFromTextField(), this);
}
void start() {
simulationComplete = false;
}
void drawWindow(int mouseX, int mouseY) {
if (!simulationComplete) {
for (Ball ball : balls) {
ball.display();
ball.updateLocation(gui.getInfectionMovementReductionPercentage(), gui.getMovementPercentage(), gui.getSuperSpreaderPercentage());
ball.checkBoundaryCollision();
ball.checkCollision();
}
}
stroke(Constants.Color.BLACK);
line(0, Constants.View.TOP_LINE_POS, Constants.View.SCREEN_WIDTH, Constants.View.TOP_LINE_POS);
line(0, Constants.View.BOTTOM_LINE_POS, Constants.View.SCREEN_WIDTH, Constants.View.BOTTOM_LINE_POS);
updateCountText();
chart.display(healthyCount, infectedCount, recoveredCount, simulationComplete);
handleResetOverlay();
}
// Create visual components
ArrayList<Ball> createBalls() {
ArrayList<Ball> balls = new ArrayList<Ball>();
balls.add(new Ball(random(Constants.View.SCREEN_WIDTH), random(Constants.View.TOP_LINE_POS, Constants.View.BOTTOM_LINE_POS), ballCount));
while(balls.size() < ballCount) {
Ball newBall = new Ball(random(Constants.View.SCREEN_WIDTH), random(Constants.View.TOP_LINE_POS, Constants.View.BOTTOM_LINE_POS), ballCount);
boolean overlapping = false;
for (int j = 0; j < balls.size(); j++) {
if (newBall.overlapsWith(balls.get(j))) {
overlapping = true;
break;
}
}
if (!overlapping) {
balls.add(newBall);
}
}
updateBalls(balls);
return balls;
}
void handleResetOverlay() {
if (simulationComplete) {
fill(0, 0, 0, 200);
rect(0, Constants.View.TOP_LINE_POS, Constants.View.SCREEN_WIDTH, Constants.View.BOTTOM_LINE_POS - Constants.View.TOP_LINE_POS);
gui.showResetButton(true);
} else {
gui.showResetButton(false);
}
}
// Utility
void updateBalls(ArrayList<Ball> balls) {
int infectedId = (int)random(ballCount);
for (int i = 0; i < balls.size(); i++) {
Ball ball = balls.get(i);
ball.update(i,balls);
if (i == infectedId) {
ball.setHealthStatus(HealthStatus.INFECTED);
}
}
}
void updateCountText() {
textFont(SFFont_25);
getCounts();
fill(Constants.Color.COVID_GREEN);
textSize(25);
text("Healthy: " + healthyCount, Constants.Gui.STANDARD_PADDING, 27);
fill(Constants.Color.COVID_RED);
textSize(25);
text("Infected: " + infectedCount, Constants.Gui.STANDARD_PADDING, 57);
fill(Constants.Color.COVID_PURPLE);
textSize(25);
text("Recovered: " + recoveredCount, Constants.Gui.STANDARD_PADDING, 87);
}
void getCounts() {
int healthy = 0;
int infected = 0;
int recovered = 0;
for (Ball ball : balls) {
if (ball.getHealthStatus() == HealthStatus.HEALTHY) {
healthy++;
} else if (ball.getHealthStatus() == HealthStatus.INFECTED) {
infected++;
} else if (ball.getHealthStatus() == HealthStatus.RECOVERED) {
recovered++;
}
}
healthyCount = healthy;
infectedCount = infected;
recoveredCount = recovered;
}
// CompletionCallback functions
void simulationComplete() {
simulationComplete = !simulationComplete;
}
// GuiCallback functions
void startButtonTapped() {
resetView();
start();
}
void resetButtonTapped() {
simulationComplete = true;
resetView();
}
}