Skip to content

Commit 243f822

Browse files
committed
Add play area check
1 parent d59d954 commit 243f822

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

Diff for: js/modules/DOMRenderer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default class DOMRenderer extends Renderer {
5757
model.healthDisplay.textContent = `${Math.max(
5858
0,
5959
model.health
60-
)} lives remaining.`;
60+
)} attempts remaining.`;
6161
model.message.innerHTML =
6262
model.condition === "PLAY"
6363
? `Shoot the moon from the

Diff for: js/modules/Game.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/*----- Imports --------------------------------------------------------------*/
88
import Celestial from "./Celestial.js";
99
import GameData from "./GameData.js";
10+
import Physics from "./Physics.js";
1011
import Renderer from "./Renderer.js";
1112

1213
/*----- Classes --------------------------------------------------------------*/
@@ -71,6 +72,7 @@ export default class Game {
7172
if (!this.running) return;
7273
const model = this.model,
7374
mouse = this.model.mouse;
75+
let scaledClick;
7476
// Handle player input
7577
model.condition === "PLAY" &&
7678
(mouse.isDown
@@ -86,15 +88,19 @@ export default class Game {
8688
model.customMassAllowed && (model.newborn.mass *= Game.SCALE.mass)
8789
: // Create new Celestial on mouse down
8890
model.health > 0 &&
91+
Physics.pointInRectangle(
92+
(scaledClick = this.view.origin
93+
.copy()
94+
.subtract(mouse.position)
95+
.subtract(this.view.offset)
96+
.scale(0 - this.view.scale)),
97+
model.playArea
98+
) &&
8999
(model.isCreating = true) &&
90100
(model.newborn = new Celestial({
91101
name: "played",
92102
mass: 1.35e23,
93-
position: this.view.origin
94-
.copy()
95-
.subtract(mouse.position)
96-
.subtract(this.view.offset)
97-
.scale(0 - this.view.scale),
103+
position: scaledClick,
98104
size: 0.54e8,
99105
})) &&
100106
model.scene.push(model.newborn)

Diff for: js/modules/GameData.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ export default class GameData {
5353
/** @var {Area} target The level target area. */
5454
get target() {
5555
return this.scene.find(
56-
(obj) => obj instanceof Area && obj.name.toLowercase() === "target"
56+
(obj) => obj instanceof Area && obj.name.toLowerCase() === "target"
5757
);
5858
}
5959
/** @var {Area} playArea The level play area. */
6060
get playArea() {
6161
return this.scene.find(
62-
(obj) => obj instanceof Area && obj.name.toLowerCase() === "play area"
62+
(obj) => obj instanceof Area && obj.name.toLowerCase() === "play"
6363
);
6464
}
6565
}

Diff for: js/modules/Physics.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import GameData from "./GameData.js";
99
import Celestial from "./Celestial.js";
1010
import Area from "./Area.js";
11-
import { Vector } from "./Utils.js";
11+
import { Point, Vector } from "./Utils.js";
1212

1313
/*----- Classes --------------------------------------------------------------*/
1414
/** @module Physics Manages the physics simulation. */
@@ -40,6 +40,7 @@ export default class Physics {
4040
* @returns {boolean}
4141
*/
4242
static pointInRectangle(point, rect) {
43+
if(!(point instanceof Point && rect instanceof Area)) return;
4344
let p1 = rect.position.copy(),
4445
p2 = p1.copy().add(rect.size);
4546
return point.x > p1.x && point.x < p2.x && point.y > p1.y && point.y < p2.y;
@@ -71,7 +72,9 @@ export default class Physics {
7172
* @returns {boolean}
7273
*/
7374
(a, b) => Physics.pointInRectangle(b.position, a),
74-
RECTANGLE: () => false,
75+
RECTANGLE:
76+
// This will always be false because only Areas are rectangles for now!
77+
() => false,
7578
},
7679
};
7780
/**

0 commit comments

Comments
 (0)