-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
112 lines (100 loc) · 2.89 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { globals } from "./globals.js";
import { Grid } from "./grid.js";
import { randomLevel, findLegalPaths } from "./levelGenerator.js";
import { Player } from "./player.js";
/**
* Inits the global vars
*/
function init() {
globals.BOARD_INSTANCE = document.querySelector(".game-board");
alert("generating levels, this could take a minute!");
findLegalPaths(
globals.START_COORDINATE,
globals.BOARD_WIDTH,
globals.BOARD_HEIGHT,
[globals.START_COORDINATE]
);
}
class Game {
constructor() {
this.grid = new Grid(
globals.BOARD_WIDTH,
globals.BOARD_HEIGHT,
globals.BOARD_INSTANCE
);
this.player = new Player(globals.START_COORDINATE);
this.score = 0;
this.scoreDisplay = document.querySelector(".score");
this.updateScore();
}
updateScore() {
this.scoreDisplay.innerHTML = "Score: " + this.score;
}
calculateScore() {
const spareLinks = this.player.visitedLinks.length - this.solution.length;
this.score += globals.SCORE_FOR_BEST_PATH / (spareLinks + 1);
const timeUsed = Date.now() - this.levelStartTime;
if (timeUsed < globals.TIME_BEFORE_FALLOFF) {
this.score += globals.SCORE_FOR_TIME;
} else {
this.score += Math.floor(
globals.SCORE_FOR_TIME /
Math.max(1, (timeUsed - globals.TIME_BEFORE_FALLOFF) / 1000)
);
}
}
onKeyPress(event) {
if (!this.player.canMove()) {
return;
}
const key = event.key.toLowerCase();
const legalMoves = this.grid.getLegalMoves(this.player.head);
for (const move in legalMoves) {
if (key === move) {
this.player.move(legalMoves[move]);
this.player.draw(this.grid);
if (this.player.visited.length === this.grid.width * this.grid.height) {
this.calculateScore();
this.updateScore();
this.newLevel(false);
}
break;
}
}
}
reset() {
this.player.clear(this.grid);
this.player.draw(this.grid);
this.levelStartTime = Date.now();
}
newLevel(deduct) {
if (deduct && globals.SHOULD_DEDUCT_FOR_NEW_LEVEL) {
if (this.score >= globals.COST_FOR_NEW_LEVEL) {
this.score -= globals.COST_FOR_NEW_LEVEL;
this.updateScore();
}
}
this.level = randomLevel(globals.BOARD_WIDTH, globals.BOARD_HEIGHT);
this.path = this.level[0];
this.solution = this.level[1];
this.grid.clearPath();
this.grid.setPath(this.path);
this.player.clear(this.grid);
this.player.draw(this.grid);
this.levelStartTime = Date.now();
}
}
window.onload = () => {
init();
const game = new Game();
game.newLevel();
document.querySelector(".next-level-button").addEventListener("click", () => {
game.newLevel(true);
});
document.querySelector(".reset-button").addEventListener("click", () => {
game.reset();
});
window.addEventListener("keypress", (event) => {
game.onKeyPress(event);
});
};