Skip to content

Commit 69e6cd5

Browse files
Use Observables to communicate between objects and decouple classes (#32)
1 parent 4a8dbb4 commit 69e6cd5

8 files changed

Lines changed: 162 additions & 93 deletions

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"gulp": "^3.9.1",
1515
"gulp-typescript": "^3.2.2",
1616
"gulp-util": "^3.0.8",
17+
"rxjs": "^7.8.1",
1718
"tsify": "^5.0.4",
1819
"typescript": "^2.5.2",
1920
"vinyl-source-stream": "^1.1.0",
@@ -28,4 +29,4 @@
2829
"overrides": {
2930
"graceful-fs": "^4.2.11"
3031
}
31-
}
32+
}

src/energyIndicatorView.ts

Lines changed: 80 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
import { PlantGlucoseSimulation } from './plantGlucoseSimulation';
2+
13
/**
24
* EnergyIndicatorView --- object to display the energy indicator box
35
* and its contents repair damage/transport nutrients energy remaining
46
* @author Hiroki Terashima
57
* @author Geoffrey Kwan
68
*/
79
export class EnergyIndicatorView {
8-
BATTERY_FULL_HEIGHT = 66; // height of battery rectangle when full, in pixles.
9-
BATTERY_FULL_COLOR = "#82c940"; // color of battery rectangle when full
10-
BATTERY_HALF_FULL_COLOR = "#ff9b0c"; // color of battery rectangle when half full
11-
BATTERY_NEAR_EMPTY_COLOR = "#fc0d1b"; // color of battery rectangle when near empty
12-
DEFAULT_FONT_SIZE = 40; // default font-size for text in this view
10+
BATTERY_FULL_HEIGHT = 66; // height of battery rectangle when full, in pixles.
11+
BATTERY_FULL_COLOR = '#82c940'; // color of battery rectangle when full
12+
BATTERY_HALF_FULL_COLOR = '#ff9b0c'; // color of battery rectangle when half full
13+
BATTERY_NEAR_EMPTY_COLOR = '#fc0d1b'; // color of battery rectangle when near empty
14+
DEFAULT_FONT_SIZE = 40; // default font-size for text in this view
1315
INDICATOR_BATTERY_ORIGINAL_Y = 821; // where battery mask's y is
1416

1517
batteryImageRepairDamage: SVG;
@@ -30,78 +32,109 @@ export class EnergyIndicatorView {
3032
* Instantiates elements in the energy indication view.
3133
* @param draw the SVG object where the view will be drawn on
3234
*/
33-
constructor(draw: SVG) {
34-
this.draw = draw;
35+
constructor(simulation: PlantGlucoseSimulation) {
36+
this.draw = simulation.draw;
3537

3638
// draw a border around the view
37-
this.border = this.draw.rect(900, 200).x(50).y(750)
38-
.fill('white').stroke({width:2}).opacity(1)
39+
this.border = this.draw
40+
.rect(900, 200)
41+
.x(50)
42+
.y(750)
43+
.fill('white')
44+
.stroke({ width: 2 })
45+
.opacity(1)
3946
.attr({
40-
'fill-opacity': 1
47+
'fill-opacity': 1,
4148
});
4249

4350
// draw the "Energy Needs" text and indication markers
44-
this.energyIndicatorText = this.draw.text('Energy\nNeeds')
45-
.x(100).y(760).font({size: this.DEFAULT_FONT_SIZE});
51+
this.energyIndicatorText = this.draw
52+
.text('Energy\nNeeds')
53+
.x(100)
54+
.y(760)
55+
.font({ size: this.DEFAULT_FONT_SIZE });
4656

4757
this.greenCheck = this.draw.image('./images/greenCheck.png').attr({
48-
'x': 125,
49-
'y': 875
58+
x: 125,
59+
y: 875,
5060
});
5161

52-
this.redExclamation = this.draw.image('./images/redExclamation.png')
62+
this.redExclamation = this.draw
63+
.image('./images/redExclamation.png')
5364
.attr({
54-
'x': 125,
55-
'y': 875
56-
}).hide();
65+
x: 125,
66+
y: 875,
67+
})
68+
.hide();
5769

58-
this.yellowExclamation = this.draw.image('./images/yellowExclamation.png')
70+
this.yellowExclamation = this.draw
71+
.image('./images/yellowExclamation.png')
5972
.attr({
60-
'x': 125,
61-
'y': 875
62-
}).hide();
73+
x: 125,
74+
y: 875,
75+
})
76+
.hide();
6377

64-
this.redX = this.draw.image('./images/redX.png')
78+
this.redX = this.draw
79+
.image('./images/redX.png')
6580
.attr({
66-
'x': 125,
67-
'y': 875
68-
}).hide();
81+
x: 125,
82+
y: 875,
83+
})
84+
.hide();
6985

7086
// draw the "Repair Damage" text and battery remaining indication
71-
this.repairDamageRect = this.draw.rect(48, this.BATTERY_FULL_HEIGHT)
72-
.x(325).y(this.INDICATOR_BATTERY_ORIGINAL_Y).fill(this.BATTERY_FULL_COLOR);
73-
74-
this.batteryImageRepairDamage = this.draw.image('./images/batteryEmpty.png')
87+
this.repairDamageRect = this.draw
88+
.rect(48, this.BATTERY_FULL_HEIGHT)
89+
.x(325)
90+
.y(this.INDICATOR_BATTERY_ORIGINAL_Y)
91+
.fill(this.BATTERY_FULL_COLOR);
92+
93+
this.batteryImageRepairDamage = this.draw
94+
.image('./images/batteryEmpty.png')
7595
.attr({
76-
'x': 325,
77-
'y': 815
96+
x: 325,
97+
y: 815,
7898
});
7999

80-
this.repairDamageIndicatorText = this.draw.text('Repair\nDamage')
81-
.x(385).y(795).font({size: 40});
100+
this.repairDamageIndicatorText = this.draw
101+
.text('Repair\nDamage')
102+
.x(385)
103+
.y(795)
104+
.font({ size: 40 });
82105

83106
// draw the "Transport Nutrients" text and battery remaining indication
84-
this.transportNutrientsRect = this.draw.rect(48, this.BATTERY_FULL_HEIGHT)
85-
.x(625).y(this.INDICATOR_BATTERY_ORIGINAL_Y).fill(this.BATTERY_FULL_COLOR);
86-
87-
this.batteryImageTransportNutrients = this.draw.image('./images/batteryEmpty.png')
107+
this.transportNutrientsRect = this.draw
108+
.rect(48, this.BATTERY_FULL_HEIGHT)
109+
.x(625)
110+
.y(this.INDICATOR_BATTERY_ORIGINAL_Y)
111+
.fill(this.BATTERY_FULL_COLOR);
112+
113+
this.batteryImageTransportNutrients = this.draw
114+
.image('./images/batteryEmpty.png')
88115
.attr({
89-
'x': 625,
90-
'y': 815
116+
x: 625,
117+
y: 815,
91118
});
92119

93-
this.transportNutrientsIndicatorText = this.draw.text('Transport\nNutrients')
94-
.x(685).y(795).font({size: 40});
120+
this.transportNutrientsIndicatorText = this.draw
121+
.text('Transport\nNutrients')
122+
.x(685)
123+
.y(795)
124+
.font({ size: 40 });
95125

96126
this.showEnergyNeeds(100);
97127
this.showBatteryIndicator(100);
128+
simulation.energyLeftEvent$.subscribe((energyLeft) =>
129+
this.updateEnergyDisplay(energyLeft)
130+
);
98131
}
99132

100133
/**
101134
* Updates the energy display section at the bottom of the window
102135
* @param energyRemaining an integer between 0 and 100
103136
*/
104-
updateEnergyDisplay(energyRemaining: number) {
137+
private updateEnergyDisplay(energyRemaining: number): void {
105138
this.showEnergyNeeds(energyRemaining);
106139
this.showBatteryIndicator(energyRemaining);
107140
}
@@ -117,12 +150,13 @@ export class EnergyIndicatorView {
117150
if (energyRemaining < 0) {
118151
return;
119152
}
120-
let newBatteryHeight = this.BATTERY_FULL_HEIGHT * energyRemaining / 100;
153+
let newBatteryHeight = (this.BATTERY_FULL_HEIGHT * energyRemaining) / 100;
121154
this.repairDamageRect.height(newBatteryHeight);
122155
this.transportNutrientsRect.height(newBatteryHeight);
123156

124-
let newBatteryY = this.INDICATOR_BATTERY_ORIGINAL_Y +
125-
(this.BATTERY_FULL_HEIGHT - newBatteryHeight);
157+
let newBatteryY =
158+
this.INDICATOR_BATTERY_ORIGINAL_Y +
159+
(this.BATTERY_FULL_HEIGHT - newBatteryHeight);
126160
this.repairDamageRect.y(newBatteryY);
127161
this.transportNutrientsRect.y(newBatteryY);
128162

src/graph.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ export class Graph {
118118

119119
this.chart = new Highcharts.Chart(this.chartOptions);
120120
this.registerGraphLineToggleListener();
121-
simulation.onReset = () => this.resetGraph();
122-
simulation.onStudentDataChanged = () => this.updateGraph();
121+
simulation.resetEvent$.subscribe(() => this.resetGraph());
122+
simulation.studentDataChangedEvent$.subscribe(() => this.updateGraph());
123123
}
124124

125125
/**

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import * as $ from 'jquery';
44
import { ResetButton } from './resetButton';
55
import { PlayPauseButton } from './playPauseButton';
66
import { Graph } from './graph';
7+
import { SimulationEndFeedback } from './simulationEndFeedback';
8+
import { EnergyIndicatorView } from './energyIndicatorView';
79

810
/**
911
* Entry point for the application. Initializes the simulation with parameters
@@ -82,6 +84,8 @@ $(document).ready(function () {
8284
);
8385
new PlayPauseButton(simulation);
8486
new ResetButton(simulation);
87+
new SimulationEndFeedback(simulation);
88+
new EnergyIndicatorView(simulation);
8589
if (showGraph) {
8690
new Graph(
8791
simulation,

0 commit comments

Comments
 (0)