Skip to content

Commit f5e285d

Browse files
committed
Add win condition
1 parent ebe56e6 commit f5e285d

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

js/modules/DOMRenderer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export default class DOMRenderer extends Renderer {
6464
<span class="red">red</span> area into the
6565
<span class="blue">blue</span> area.`
6666
: model.condition === "WIN"
67-
? `You win! Congratulations!`
67+
? `<span class="blue">You win! Congrats!</span>`
6868
: `<span class="red">You lost! Oh no!</span>`;
6969
}
7070
/**

js/modules/Game.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
/*----- Imports --------------------------------------------------------------*/
8+
import Area from "./Area.js";
89
import Celestial from "./Celestial.js";
910
import GameData from "./GameData.js";
1011
import Physics from "./Physics.js";
@@ -112,15 +113,30 @@ export default class Game {
112113
// Loss conditions
113114
model.condition === "PLAY" &&
114115
model.health <= 0 &&
116+
// Last played Celestial older than 30s
115117
new Date() - 30e3 >
116118
model.scene
117119
.slice()
118120
.reverse()
119-
.find(
120-
(obj) =>
121-
obj instanceof Celestial && obj.name.toLowerCase() === "played"
122-
).birth &&
121+
.find((obj) => obj instanceof Celestial && obj.name === "played")
122+
.birth &&
123123
(model.condition = "LOSS");
124+
// Win conditions
125+
model.condition === "PLAY" &&
126+
model.scene.reduce(
127+
(acc, obj) =>
128+
// Check played Celestial
129+
obj instanceof Celestial && obj.name === "played"
130+
? obj.collisions.find(
131+
(hit) =>
132+
// Check if hit target
133+
hit.who instanceof Area &&
134+
hit.who.name.toLowerCase() === "target"
135+
) || acc
136+
: acc,
137+
false
138+
) &&
139+
(model.condition = "WIN");
124140
requestAnimationFrame(this.loop.bind(this));
125141
}
126142
}

0 commit comments

Comments
 (0)