Skip to content

Commit

Permalink
Handle game state started, paused, unpaused, and ended
Browse files Browse the repository at this point in the history
  • Loading branch information
ndorfin committed Feb 14, 2024
1 parent c45ab4c commit a2ccb06
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
7 changes: 5 additions & 2 deletions assets/mjs/wc/game-controls.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ export default class GameControls extends GameElement {
}

keyboardHandler(event) {
console.log(event.key);

let landerChange = null;
let gameChange = null;

Expand Down Expand Up @@ -110,6 +108,11 @@ export default class GameControls extends GameElement {
running: false
};
break;
case 'Enter':
gameChange = {
running: true
};
break;
}

if (landerChange) dispatchEventWithDetails('LanderStateChanged', landerChange);
Expand Down
52 changes: 44 additions & 8 deletions assets/mjs/wc/game-engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ export default class GameEngine extends GameElement {

modelLander = {};
#gameRunning;
#gameStarted;
#gameEnded;
#gameEventFrequency = 10; // Milliseconds
#gameDuration = 0; // Counts the time elapsed based on multiples of `this.#gameEventFrequency`
#gameInterval; // Placeholder for window.setInterval so that it can be cleared later.

#gameRunningStateChanged = (runningState) => {
this.#gameRunning = runningState;
console.log('running', runningState);
dispatchEventWithDetails('GameStateChange', {running: runningState});
}

#landerStateChanged = (values) => {
console.log('new lander state', values);
dispatchEventWithDetails('LanderStateChanged', values);
}

Expand All @@ -28,24 +28,41 @@ export default class GameEngine extends GameElement {
let currentItem = MODEL[keyName];
if (currentItem.affects === 'lander') this.modelLander[keyName] = currentItem.initial;
});

this.handleKeyboardInterrupts = this.handleKeyboardInterrupts.bind(this);
this.setInitialValuesAndStart = this.setInitialValuesAndStart.bind(this);
this.#gameLoop = this.#gameLoop.bind(this);
}

#gameLoop = function () {
this.#gameDuration++;

if (this.#gameDuration > 1_000) {
this.stopGame();
}
// if (this.#gameDuration > 1_000) {
// this.stopGame();
// }
}

startGame() {
this.#gameStarted = true;
this.#gameRunningStateChanged(true);
this.#gameDuration = 0;
this.#gameInterval = window.setInterval(this.#gameLoop.bind(this), this.#gameEventFrequency);
this.#gameInterval = window.setInterval(this.#gameLoop, this.#gameEventFrequency);
}

stopGame() {
pauseGame() {
this.#gameRunning = false;
this.#gameRunningStateChanged(false);
}

unpauseGame() {
this.#gameRunning = true;
this.#gameRunningStateChanged(true);
}

stopGame() {
this.#gameEnded = true;
console.log('game ended after duration', this.#gameDuration);
document.removeEventListener('keyup', this.handleKeyboardInterrupts);
window.clearInterval(this.#gameInterval);
}

Expand All @@ -54,9 +71,28 @@ export default class GameEngine extends GameElement {
this.startGame();
}

handleKeyboardInterrupts(event) {
console.log('event.key', event.key);
switch (event.key) {
case 'Enter':
if (this.#gameStarted && !this.#gameRunning) {
this.unpauseGame();
} else if (!this.#gameStarted) {
this.startGame();
}
break;
case 'Escape':
if (this.#gameStarted) {
(this.#gameRunning) ? this.pauseGame() : this.stopGame();
}
break;
}
}

connectedCallback() {
super.connectedCallback();
this.addEventListener('FormElementsAdded', this.setInitialValuesAndStart.bind(this));
this.addEventListener('FormElementsAdded', this.setInitialValuesAndStart, {once: true});
document.addEventListener('keyup', this.handleKeyboardInterrupts);
}


Expand Down
5 changes: 1 addition & 4 deletions assets/mjs/wc/lander-vehicle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ export default class LanderVehicle extends GameElement {
propertyName = '--lander_rotation';
break;
case 'thruster':
break;
case 'speed':
break;
case 'altitude':
propertyName = '--lander_thruster';
break;
}

Expand Down

0 comments on commit a2ccb06

Please sign in to comment.