Skip to content

Commit 6c7f05d

Browse files
committed
Add Utils module
1 parent 5808778 commit 6c7f05d

File tree

6 files changed

+72
-27
lines changed

6 files changed

+72
-27
lines changed

js/modules/DOMRenderer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Renderer from "./Renderer.js";
1111
/** @module DOMRenderer - Manages the game view (DOM). */
1212
export default class extends Renderer {
1313
/**
14-
* Initialize a DOM-based renderer
14+
* Initialize a DOM-based renderer.
1515
* @param {GameData} model - A game model instance.
1616
*/
1717
constructor(model) {

js/modules/Game.js

-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export default class Game {
3737
* @param {MouseEvent} e - Mouse down event
3838
*/
3939
handleMouseDown(e) {
40-
// Update GameData#mouse properties
4140
let mouse = this.model.mouse;
4241
mouse.isDown = true;
4342
[mouse.initPosition.x, mouse.initPosition.y] = [e.pageX, e.pageY];
@@ -49,20 +48,15 @@ export default class Game {
4948
* @param {MouseEvent} e - Mouse up event
5049
*/
5150
handleMouseUp(e) {
52-
// Update GameData#mouse properties
5351
let mouse = this.model.mouse;
5452
mouse.isDown = false;
5553
[mouse.position.x, mouse.position.y] = [e.pageX, e.pageY];
56-
57-
// TEST! Log all the mouse data
58-
console.log(mouse);
5954
}
6055
/**
6156
* Handle mouse move events
6257
* @param {MouseEvent} e - Mouse move event
6358
*/
6459
handleMouseMove(e) {
65-
// Update GameData#mouse properties
6660
let mouse = this.model.mouse;
6761
[mouse.position.x, mouse.position.y] = [e.pageX, e.pageY];
6862
}

js/modules/GameData.js

+10-13
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@
44
* @version 2021.06.01
55
*/
66

7+
/*----- Imports --------------------------------------------------------------*/
8+
import { Point, Vector } from "./Utils.js";
9+
710
/** @module GameData - Manages the game model. */
811
export default class {
912
/** Initialize a game model. */
1013
constructor() {
11-
/** @var {string} id - Short game ID based on the current time */
14+
/** @var {string} id - Short game ID based on the current time. */
1215
this.id = Math.round(+new Date() / 1e3).toString(26);
13-
/** @var {Object} mouse - Data regarding the user's mouse */
16+
/** @var {Object} mouse - Data regarding the user's mouse. */
1417
this.mouse = {
15-
/** @var {boolean} mouse.isDown - Is mouse currently down */
18+
/** @var {boolean} mouse.isDown - Is mouse currently down? */
1619
isDown: false,
17-
/** @var {Object} mouse.initPosition - Position of mouse on down */
18-
initPosition: {
19-
x: 0,
20-
y: 0,
21-
},
22-
/** @var {Object} mouse.position - Current position of mouse */
23-
position: {
24-
x: 0,
25-
y: 0,
26-
},
20+
/** @var {Object} mouse.initPosition - Position of mouse on down. */
21+
initPosition: new Point(0, 0),
22+
/** @var {Object} mouse.position - Current position of mouse. */
23+
position: new Point(0, 0),
2724
};
2825
}
2926
}

js/modules/Physics.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export default class {
1212
}
1313

1414
/*----- Constants ----------------------------------------------------------*/
15-
/** @const {number} G - Gravitational constant */
15+
/** @const {number} G - Gravitational constant. */
1616
static G = 6.67e-11;
17-
/** @const {Object} scale - Conversion rates for physics equations */
17+
/** @const {Object} scale - Conversion rates for physics equations. */
1818
static scale = {
19-
/** @const {number} scale.space - Space scale (meters per pixel) */
19+
/** @const {number} scale.space - Space scale (meters per pixel). */
2020
space: 0.3e7,
21-
/** @const {number} scale.time - Time scale (seconds per frame) */
21+
/** @const {number} scale.time - Time scale (seconds per frame). */
2222
time: 0.5e3,
2323
};
2424
}

js/modules/Renderer.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ export default class {
1414
* @param {GameData} model - A game model instance.
1515
*/
1616
constructor(model) {
17-
/** @var {GameData} model - Reference to the game's model */
17+
/** @var {GameData} model - Reference to the game's model. */
1818
this.model = model;
1919
}
2020
/**
2121
* Sets the view container
22-
* @param {HTMLElement} el - Element to set as view container
22+
* @param {HTMLElement} el - Element to set as view container.
2323
*/
2424
setContainer(el) {
2525
el.id = `gravity-${this.model.id}`;
26-
/** @var {HTMLElement} container - DOM Element holding all game views.*/
26+
el.classList.add("gravity");
27+
/** @var {HTMLElement} container - DOM Element holding all game views. */
2728
this.container = el;
2829
}
2930
}

js/modules/Utils.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @file A collection of utility classes.
3+
* @author Una Ada <una@anarchy.website>
4+
* @version 2021.06.02
5+
*/
6+
7+
/** A single point in cartesian coordinated. */
8+
export class Point {
9+
/**
10+
* Create a single point.
11+
* @param {number} x - The point's x coordinate.
12+
* @param {number} y - The point's y coordinate.
13+
*/
14+
constructor(x, y) {
15+
/** @var {number} x - The point's x coordinate. */
16+
this.x = x;
17+
/** @var {number} y - The point's y coordinate. */
18+
this.y = y;
19+
}
20+
}
21+
/** A 2-dimensional vector in cartesian coordinated. */
22+
export class Vector extends Point {
23+
/**
24+
* Create a 2-dimensional vector.
25+
* @param {number} x - The vector's x length.
26+
* @param {number} y - The vector's y length.
27+
*/
28+
constructor(x, y) {
29+
super(x, y);
30+
}
31+
32+
/*----- Getters and setters ------------------------------------------------*/
33+
/** @type {number} */
34+
get magnitude() {
35+
return Math.sqrt(this.x ** 2 + this.y ** 2);
36+
}
37+
set magnitude(magnitude) {
38+
let direction = this.direction;
39+
this.x = magnitude * Math.cos(direction);
40+
this.y = magnitude * Math.sin(direction);
41+
}
42+
/** @type {number} */
43+
get direction() {
44+
/** @TODO */
45+
}
46+
set direction(direction) {
47+
let magnitude = this.magnitude;
48+
// Keep direction within [-pi, pi]
49+
if(Math.abs(direction) > Math.PI) direction %= Math.PI;
50+
this.x = magnitude * Math.cos(direction);
51+
this.y = magnitude * Math.sin(direction);
52+
}
53+
}

0 commit comments

Comments
 (0)