Skip to content

Commit 0cf7d35

Browse files
committed
refactoring button state checking to use getters
1 parent e0fe596 commit 0cf7d35

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

source/game.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function weightedChoice(weights) {
6161
*
6262
* TODO:
6363
* work on tutorial pane.
64+
* make progress slider use <div> instead of <progress>.
6465
* create a field that is a scalar of Game.highSpeed based on the difficulty slider
6566
*
6667
* Cookies for: name, high-score(score, misses), version.
@@ -97,7 +98,7 @@ class Game {
9798
cell.appendChild(fInner);
9899
}
99100
}
100-
// TODO @ below: update numTracks.
101+
// Initialize background music with all 12 tracks:
101102
this.backgroundMusic = new BackgroundMusic(12);
102103

103104
// Create players:
@@ -113,6 +114,7 @@ class Game {
113114
// Setup invariants and then start the game:
114115
for (let enemy in Game.enemies) { this[enemy] = new Pos(); }
115116
this.restart();
117+
document.body.onkeydown = () => this.movePlayer(event);
116118
}
117119

118120
/* Creates restart and pause buttons,
@@ -554,11 +556,11 @@ class Game {
554556
const obtained = Math.pow(scores + (this.misses), 1-curveDown);
555557

556558
// Scalar multiple of the default number of targets:
557-
const slowness = 25 * (20 * 20 / Game.targetThinness);
559+
const slowness = 25 * Game.defaultNumTargets;
558560
const exp = Math.pow(obtained / slowness, 2);
559561
const speed = (Game.highSpeed - Game.lowSpeed) *
560562
(1 - Math.pow(2, -exp)) + Game.lowSpeed;
561-
563+
console.log(speed);
562564
return speed;
563565
}
564566

@@ -622,11 +624,11 @@ class Game {
622624
// Handle if a method is requesting to
623625
// force the game to a certain state:
624626
if (force == 'pause') {
625-
this.pauseButton.pauseOn = false;
627+
this.paused = false;
626628
this.togglePause();
627629
return;
628630
} else if (force == 'unpause') {
629-
this.pauseButton.pauseOn = true;
631+
this.paused = true;
630632
this.togglePause();
631633
return;
632634
}
@@ -638,25 +640,20 @@ class Game {
638640
clearTimeout(that.runnerCancel);
639641

640642
// The player pressed the pause button:
641-
if (!this.pauseButton.pauseOn) {
642-
this.pauseButton.innerHTML = 'unpause';
643+
if (!this.paused) {
643644
this.backgroundMusic.pause();
644645
document.body.style.filter = 'var(--pauseFilter)';
645-
//document.body.onkeydown = () => {};
646-
647646
// The user pressed the un-pause button:
648647
} else {
649-
this.pauseButton.innerHTML = 'pause';
650-
if (!document.getElementById('muteButton').muteOn) {
648+
if (!this.muted) {
651649
this.backgroundMusic.play();
652650
}
653651
document.body.style.filter = '';
654-
document.body.onkeydown = () => this.movePlayer(event);
655652
this.chaserCancel = setTimeout(that.moveChaser.bind(that), 1000);
656653
this.nommerCancel = setTimeout(that.moveNommer.bind(that), 1000);
657654
this.runnerCancel = setTimeout(that.moveRunner.bind(that), 1000);
658655
}
659-
this.pauseButton.pauseOn = !this.pauseButton.pauseOn;
656+
this.paused = !this.paused;
660657
}
661658

662659
/* Moves the chaser to player.pos and kills player.
@@ -702,14 +699,31 @@ class Game {
702699
tileAt(pos) { return this.grid[pos.y * this.width + pos.x]; }
703700
isCharacter(tile) { return !(tile.key in this.language); }
704701

705-
get misses() {return parseInt(this.misses_.innerHTML);}
702+
get misses() {
703+
return parseInt(this.misses_.innerHTML);
704+
}
706705
set misses(val) {
707706
this.misses_.innerHTML = val;
708707
this.updateTrackLevel();
709708
}
709+
get paused() {
710+
return this.pauseButton.pauseOn;
711+
}
712+
set paused(onFlag) {
713+
if (onFlag) {
714+
this.pauseButton.innerHTML = 'unpause';
715+
} else {
716+
this.pauseButton.innerHTML = 'pause';
717+
}
718+
this.pauseButton.pauseOn = onFlag;
719+
}
720+
get muted() {
721+
return document.getElementById('muteButton').muteOn;
722+
}
710723
}
711724
// Game base settings:
712725
Game.targetThinness = 72;
726+
Game.defaultNumTargets = 20 * 20 / Game.targetThinness;
713727
Game.enemies = {
714728
'chaser': ':>',
715729
'nommer': ':O',

source/options_menu.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function makeOptionsMenu(game, parentElement) {
2323

2424
'10. Wouldn\'t it be nice if there was a way' +
2525
' to cut down your misses? (spoiler: there is)',
26-
'11. If you catch (go right beside) the runner,' + // 50 chars wide.
26+
'11. If you catch (go right beside) the runner,' +
2727
' your misses will be cut down by a third.',
2828

2929
'12. If you find the game too easy, press the' +

source/sounds.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,4 @@ class BackgroundMusic {
139139

140140
}
141141
}
142-
BackgroundMusic.fullTrackProgress = 0.25;
142+
BackgroundMusic.fullTrackProgress = 0.40;

style.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ body {
6969
flex-basis: 40vh;
7070
}
7171
.tutorial > div {
72-
width: 100%;
72+
width: 85%;
7373
display: none;
7474
text-overflow: clip;
7575
}

0 commit comments

Comments
 (0)