Skip to content

Commit 81efd39

Browse files
committed
Make Celestials in view reflect model
1 parent d51bdf9 commit 81efd39

File tree

7 files changed

+55
-13
lines changed

7 files changed

+55
-13
lines changed

css/main.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1+
/**
2+
* Styles for DOM Rendering
3+
* by Una Ada <una@anarchy.website>
4+
* June 3, 2021
5+
*/
6+
7+
/*---- Override Agent Style --------------------------------------------------*/
18
body {
29
margin: 0;
310
}
11+
12+
/*----- Game View ------------------------------------------------------------*/
413
gravity {
514
height: 100%;
615
left: 0;
716
position: absolute;
817
top: 0;
918
width: 100%;
1019
}
20+
gravity > celestial {
21+
background-color: black;
22+
position: absolute;
23+
}

js/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ let saturn = new Celestial({
3737
velocity: new Vector(15.26e3, 0),
3838
size: 18, // not to scale
3939
});
40+
console.log(new Point(0, -389) || "Point is falsey???");
4041
model.scene.push(saturn);
4142
model.scene.push(titan);
4243
view.render();

js/modules/Celestial.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file Celestial game objects.
33
* @author Una Ada <una@anarchy.website>
4-
* @version 2021.06.02
4+
* @version 2021.06.03
55
*/
66

77
/*----- Imports --------------------------------------------------------------*/
@@ -23,19 +23,25 @@ export default class Celestial {
2323
*/
2424
constructor(options) {
2525
/*----- Metadata ---------------------------------------------------------*/
26+
/** @var {string} name Display name. */
2627
this.name = options.name || "Unnamed Celestial";
2728
/** @var {number} birth Creation timestamp. */
2829
this.birth = +new Date();
2930

3031
/*----- Physics ----------------------------------------------------------*/
31-
this.position = options.positions || new Point(0, 0);
32+
/** @var {Point} position Position coordinates in meters. */
33+
this.position = options.position || new Point(0, 0);
34+
/** @var {Vector} velocity Velocity vector in meters/second. */
3235
this.velocity = options.velocity || new Vector(0, 0);
36+
/** @var {number} mass Mass in kilograms. */
3337
this.mass = options.mass || 0;
3438
/** @TODO Add HitBox class */
3539
this.hitBox = null;
3640

3741
/*----- Rendering --------------------------------------------------------*/
42+
/** @var {number} size Width or diameter in meters. */
3843
this.size = options.size || 0;
44+
/** @var {ImageData} texture The texture for rendering. */
3945
this.texture = options.texture || null;
4046
}
4147
}

js/modules/DOMRenderer.js

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/*----- Imports --------------------------------------------------------------*/
88
import GameData from "./GameData.js";
99
import Renderer from "./Renderer.js";
10+
import Celestial from "./Celestial.js";
1011

1112
/*----- Classes --------------------------------------------------------------*/
1213
/** @module DOMRenderer - Manages the game view (DOM). */
@@ -21,13 +22,31 @@ export default class DOMRenderer extends Renderer {
2122
}
2223

2324
/*---- Methods -------------------------------------------------------------*/
24-
/** Render the game scene. */
25+
/**
26+
* Render the game scene.
27+
* @override
28+
*/
2529
render() {
26-
const scene = this.model.scene;
27-
scene.forEach((obj) => {
28-
// Make sure each Celestial has an Element
29-
const elem = obj.element || this.generateElement(obj);
30-
});
30+
const scene = this.model.scene,
31+
bounds = this.bounds,
32+
origin = {
33+
x: bounds.width / 2,
34+
y: bounds.height / 2,
35+
};
36+
scene.forEach(
37+
/** @arg obj {Celestial} */
38+
(obj) => {
39+
// Make sure each Celestial has an Element
40+
const elem = obj.element || this.generateElement(obj),
41+
// Avoid repeating elem.style a whole bunch.
42+
style = elem.style,
43+
offset = 0 - obj.size / 2;
44+
style.top = `${origin.y + obj.position.y + offset}px`;
45+
style.left = `${origin.x + obj.position.x + offset}px`;
46+
style.width = `${obj.size}px`;
47+
style.height = `${obj.size}px`;
48+
}
49+
);
3150
}
3251
/**
3352
* Create a rendering Element for a Celestial and append it to the container.

js/modules/GameData.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file Manages the game model.
33
* @author Una Ada <una@anarchy.website>
4-
* @version 2021.06.01
4+
* @version 2021.06.03
55
*/
66

77
/*----- Imports --------------------------------------------------------------*/
@@ -10,7 +10,7 @@ import Celestial from "./Celestial.js";
1010

1111
/*----- Classes --------------------------------------------------------------*/
1212
/** @module GameData Manages the game model. */
13-
export default class {
13+
export default class GameData {
1414
/** Initialize a game model. */
1515
constructor() {
1616
/** @var {string} id Short game ID based on the current time. */

js/modules/Renderer.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ export default class Renderer {
3737
/** @var {HTMLElement} container - DOM Element holding all game views. */
3838
this.container = el;
3939
}
40-
/** Render the game scene. */
40+
/**
41+
* Render the game scene.
42+
* @abstract
43+
*/
4144
render() {
42-
console.error("Renderer#render() has not been overwritten by subclass!");
45+
throw new Error("Renderer#render() must be implemented by subclass!");
4346
}
4447
}

js/modules/Utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @file A collection of utility classes.
33
* @author Una Ada <una@anarchy.website>
4-
* @version 2021.06.02
4+
* @version 2021.06.03
55
*/
66

77
/*----- Classes --------------------------------------------------------------*/

0 commit comments

Comments
 (0)