Skip to content

Commit 1656f8d

Browse files
committed
Add target and play area getters
1 parent 5a0af76 commit 1656f8d

File tree

5 files changed

+59
-39
lines changed

5 files changed

+59
-39
lines changed

css/main.css

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ gravity {
4343
}
4444
gravity > viewarea {
4545
border-radius: 5px;
46-
border-style: solid;
47-
border-width: 3px;
4846
position: absolute;
47+
z-index: 0;
4948
}
5049
gravity > celestial {
5150
background-color: #333;
5251
border-radius: 50%;
5352
position: absolute;
53+
z-index: 50;
5454
}
5555

5656
/*----- Game Stats -----------------------------------------------------------*/
@@ -80,8 +80,8 @@ div#repo {
8080
z-index: 200;
8181
}
8282
span.blue {
83-
color: blue;
83+
color: rgba(0, 0, 255, 0.7);
8484
}
8585
span.red {
86-
color: red;
86+
color: rgba(255, 0, 0, 0.7);
8787
}

js/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ let saturn = new Celestial({
3333
size: 3.06e8,
3434
texture: "./img/celestial-saturn.svg",
3535
}),
36-
target = new Area(new Point(1.0e9, -1e8), new Point(0.559e9, 2e8), {
36+
target = new Area(new Point(1.0e9, -1e8), new Point(2e8, 2e8), {
3737
name: "Target",
3838
});
3939
model.scene.push(saturn);

js/modules/DOMRenderer.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import GameData from "./GameData.js";
99
import Renderer from "./Renderer.js";
1010
import Celestial from "./Celestial.js";
11+
import Area from "./Area.js";
1112

1213
/*----- Classes --------------------------------------------------------------*/
1314
/** @module DOMRenderer Manages the game view (DOM). */
@@ -63,8 +64,8 @@ export default class DOMRenderer extends Renderer {
6364
<span class="red">red</span> area into the
6465
<span class="blue">blue</span> area.`
6566
: model.condition === "WIN"
66-
? `You win! Congratulations!`
67-
: `<span class="red">You lost! Oh no!</span>`;
67+
? `You win! Congratulations!`
68+
: `<span class="red">You lost! Oh no!</span>`;
6869
}
6970
/**
7071
* Create an Element for a game object and append it to the container.
@@ -74,7 +75,6 @@ export default class DOMRenderer extends Renderer {
7475
generateElement(obj) {
7576
const type = obj instanceof Celestial ? "celestial" : "viewarea",
7677
element = document.createElement(type),
77-
// Clean up the name for adding into a CSS class
7878
cleanName = obj.name.replace(/\s+/g, "-").toLowerCase();
7979
element.classList.add(`gravity__${type}_${cleanName}`);
8080
obj.texture &&
@@ -83,6 +83,11 @@ export default class DOMRenderer extends Renderer {
8383
element.style.borderRadius,
8484
element.style.backgroundImage,
8585
] = [`transparent`, `0`, `url(${obj.texture})`]);
86+
obj instanceof Area &&
87+
(obj.name.toLowerCase() === "target"
88+
? (element.style.backgroundColor = `rgba(0, 0, 255, 0.7)`)
89+
: obj.name.toLowerCase() === "play" &&
90+
(element.style.backgroundColor = `rgba(255, 0, 0, 0.7)`));
8691
obj.element = element;
8792
this.container.append(obj.element);
8893
return element;

js/modules/Game.js

+31-30
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,37 @@ export default class Game {
7272
const model = this.model,
7373
mouse = this.model.mouse;
7474
// Handle player input
75-
mouse.isDown
76-
? model.isCreating
77-
? mouse.change.magnitude > 18
78-
// Set velocity if mouse outside threshold
79-
? (model.newborn.velocity = mouse.change
80-
.copy()
81-
.scale(Game.SCALE.velocity)
82-
.reflectHorizontal()
83-
.reflectVertical())
84-
// Customize mass if mouse still in threshold
85-
: model.customMassAllowed &&
86-
(model.newborn.mass *= Game.SCALE.mass)
87-
// Create new Celestial on mouse down
88-
: model.health > 0 &&
89-
(model.isCreating = true) &&
90-
(model.newborn = new Celestial({
91-
mass: 1.35e23,
92-
position: this.view.origin
93-
.copy()
94-
.subtract(mouse.position)
95-
.subtract(this.view.offset)
96-
.scale(0 - this.view.scale),
97-
size: 0.54e8,
98-
})) &&
99-
model.scene.push(model.newborn)
100-
// Release newborn Celestial on mouse up
101-
: model.isCreating &&
102-
(model.newborn.physical = true) &&
103-
(model.health -= 1) &&
104-
(model.isCreating = false);
75+
model.condition === "PLAY" &&
76+
(mouse.isDown
77+
? model.isCreating
78+
? mouse.change.magnitude > 18
79+
? // Set velocity if mouse outside threshold
80+
(model.newborn.velocity = mouse.change
81+
.copy()
82+
.scale(Game.SCALE.velocity)
83+
.reflectHorizontal()
84+
.reflectVertical())
85+
: // Customize mass if mouse still in threshold
86+
model.customMassAllowed && (model.newborn.mass *= Game.SCALE.mass)
87+
: // Create new Celestial on mouse down
88+
model.health > 0 &&
89+
(model.isCreating = true) &&
90+
(model.newborn = new Celestial({
91+
name: "played",
92+
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),
98+
size: 0.54e8,
99+
})) &&
100+
model.scene.push(model.newborn)
101+
: // Release newborn Celestial on mouse up
102+
model.isCreating &&
103+
(model.newborn.physical = true) &&
104+
(model.health -= 1) &&
105+
(model.isCreating = false));
105106
requestAnimationFrame(this.loop.bind(this));
106107
}
107108
}

js/modules/GameData.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66

77
/*----- Imports --------------------------------------------------------------*/
8+
import Area from "./Area.js";
89
import { Point, Vector } from "./Utils.js";
9-
import Celestial from "./Celestial.js";
1010

1111
/*----- Classes --------------------------------------------------------------*/
1212
/** @module GameData Manages the game model. */
@@ -48,4 +48,18 @@ export default class GameData {
4848
/** @var {Celestial} newborn Newest created Celestial. */
4949
this.newborn = null;
5050
}
51+
52+
/*----- Setters and Getters ------------------------------------------------*/
53+
/** @var {Area} target The level target area. */
54+
get target() {
55+
return this.scene.find(
56+
(obj) => obj instanceof Area && obj.name.toLowercase() === "target"
57+
);
58+
}
59+
/** @var {Area} playArea The level play area. */
60+
get playArea() {
61+
return this.scene.find(
62+
(obj) => obj instanceof Area && obj.name.toLowerCase() === "play area"
63+
);
64+
}
5165
}

0 commit comments

Comments
 (0)