Lint code

This commit is contained in:
2024-10-05 13:25:07 +02:00
parent 0a817c4379
commit ee696c6292
3 changed files with 78 additions and 78 deletions
+46 -46
View File
@@ -13,21 +13,21 @@ let seconds_elapsed = 0;
let sounds = new Map([
["reveal", [
new Audio("./res/sfx/reveal_1.wav"),
new Audio("./res/sfx/reveal_2.wav"),
new Audio("./res/sfx/reveal_3.wav"),
new Audio("./res/sfx/reveal_4.wav")
new Audio("./res/sfx/reveal_1.wav"),
new Audio("./res/sfx/reveal_2.wav"),
new Audio("./res/sfx/reveal_3.wav"),
new Audio("./res/sfx/reveal_4.wav")
]],
["flag", [
new Audio("./res/sfx/flag_1.wav"),
new Audio("./res/sfx/flag_2.wav"),
new Audio("./res/sfx/flag_3.wav")
new Audio("./res/sfx/flag_1.wav"),
new Audio("./res/sfx/flag_2.wav"),
new Audio("./res/sfx/flag_3.wav")
]],
["unflag", [
new Audio("./res/sfx/unflag_1.wav"),
new Audio("./res/sfx/unflag_2.wav"),
new Audio("./res/sfx/unflag_3.wav"),
new Audio("./res/sfx/unflag_4.wav")
new Audio("./res/sfx/unflag_1.wav"),
new Audio("./res/sfx/unflag_2.wav"),
new Audio("./res/sfx/unflag_3.wav"),
new Audio("./res/sfx/unflag_4.wav")
]],
["game_over", []],
["game_win", []]
@@ -58,10 +58,10 @@ function play_sound(sound_name) {
function get_dom_bb_proper(element) {
let r = element.getBoundingClientRect();
return {
w: r.right - r.left,
h: r.bottom - r.top,
x: element.offsetLeft - element.scrollLeft,
y: element.offsetTop - element.scrollTop
w: r.right - r.left,
h: r.bottom - r.top,
x: element.offsetLeft - element.scrollLeft,
y: element.offsetTop - element.scrollTop
};
}
@@ -70,15 +70,15 @@ function reveal_cell_cosmetic(cell) {
let bb = get_dom_bb_proper(cell);
// Some wood particles
for (let i = 0; i < 16; ++i) {
let x = bb.x + bb.w / 2;
let y = bb.y + bb.h / 2;
spawn_particle(x, y, (Math.random() * 2 - 1) / 4, -Math.random() / 2, 0,
(Math.random() * 2 - 1) / 8, graphics_wood_particle,
graphics_wood_particle.width, graphics_wood_particle.height);
let x = bb.x + bb.w / 2;
let y = bb.y + bb.h / 2;
spawn_particle(x, y, (Math.random() * 2 - 1) / 4, -Math.random() / 2, 0,
(Math.random() * 2 - 1) / 8, graphics_wood_particle,
graphics_wood_particle.width, graphics_wood_particle.height);
}
// Pop the coffin lid off.
spawn_particle(bb.x, bb.y, (Math.random() * 2 - 1) / 16, -0.3, 0,
(Math.random() * 2 - 1) / 16, graphics_unknown, bb.w, bb.h);
(Math.random() * 2 - 1) / 16, graphics_unknown, bb.w, bb.h);
}
function flag_cell_cosmetic(cell) {
@@ -92,18 +92,18 @@ function unflag_cell_cosmetic(cell) {
let bb = get_dom_bb_proper(cell);
// Spawn dirt particles and make the tombstone fall off.
spawn_particle(bb.x, bb.y, (Math.random() * 2 - 1) / 24, -0.2, 0,
(Math.random() * 2 - 1) / 24, graphics_flagged, bb.w, bb.h);
(Math.random() * 2 - 1) / 24, graphics_flagged, bb.w, bb.h);
}
// Shows the game won/over popup.
function show_popup(victory) {
document.body.classList.remove("popup-victory", "popup-failure");
if (victory) {
document.body.classList.add("popup-victory");
$("#message").innerText = "You've won!"
document.body.classList.add("popup-victory");
$("#message").innerText = "You've won!"
} else {
document.body.classList.add("popup-failure");
$("#message").innerText = "You've lost!"
document.body.classList.add("popup-failure");
$("#message").innerText = "You've lost!"
}
}
@@ -112,10 +112,10 @@ function show_popup(victory) {
// Game state saving.
window.addEventListener("beforeunload", function() {
if (mines_placed && !game_finished) {
// We need to save: the minefield, the elapsed time. These also
// get prefixed by the current level.
localStorage.setItem(current_level + "-minefield_html", $("#minefield").innerHTML);
localStorage.setItem(current_level + "-seconds_elapsed", seconds_elapsed);
// We need to save: the minefield, the elapsed time. These also
// get prefixed by the current level.
localStorage.setItem(current_level + "-minefield_html", $("#minefield").innerHTML);
localStorage.setItem(current_level + "-seconds_elapsed", seconds_elapsed);
} else if (document.body.classList.contains("game-view")) {
localStorage.removeItem(current_level + "-minefield_html");
localStorage.removeItem(current_level + "-seconds_elapsed");
@@ -152,7 +152,7 @@ function restart_game() {
stop_timing();
seconds_elapsed = 0;
document.body.classList.remove("main-menu", "popup-victory", "popup-failure");
document.body.classList.add ("game-view");
document.body.classList.add("game-view");
$("#time").innerText = get_seconds_readable(seconds_elapsed);
}
@@ -165,7 +165,7 @@ function start_or_continue_game(level) {
let minefield_html = localStorage.getItem(current_level + "-minefield_html");
$("#level").innerText = current_level;
if (minefield_html == null) {
restart_game();
restart_game();
} else {
// If we have a minefield in localStorage, load it back into
// the DOM.
@@ -219,17 +219,17 @@ function cell_onclick() {
reveal_mines(this);
}
} else if (this.classList.contains("flagged")) {
unflag_cell_cosmetic(this);
unflag_cell_cosmetic(this);
this.classList.remove("flagged");
}
}
function cell_oncontextmenu() {
if (this.classList.contains("unknown") && !this.classList.contains("flagged")) {
flag_cell_cosmetic(this);
flag_cell_cosmetic(this);
this.classList.add("flagged");
} else if (this.classList.contains("flagged")) {
unflag_cell_cosmetic(this);
unflag_cell_cosmetic(this);
this.classList.remove("flagged");
}
check_victory();
@@ -275,26 +275,26 @@ function reveal_mines(cell) {
let n_mines = count_neighbouring_mines(cell);
if (cell.classList.contains("unknown")) {
reveal_cell_cosmetic(cell);
cell.classList.remove("unknown");
cell.classList.add("known");
reveal_cell_cosmetic(cell);
cell.classList.remove("unknown");
cell.classList.add("known");
}
// Check for victory.
if (!game_finished) {
check_victory();
check_victory();
}
if (n_mines == 0) {
get_neighbours(cell).forEach(function(c) {
if (c.classList.contains("unknown")) {
window.setTimeout(function () {
reveal_mines(c);
}, 100);
window.setTimeout(function() {
reveal_mines(c);
}, 100);
// reveal_mines(c);
}
if (c.classList.contains("flagged")) {
unflag_cell_cosmetic(c);
unflag_cell_cosmetic(c);
c.classList.remove("flagged");
}
});
@@ -318,7 +318,7 @@ function check_victory() {
stop_timing();
// Set the maximum level to be one higher, meaning the player unlocked the next level.
if (current_level == maximum_level) {
localStorage.setItem("maximum_level", maximum_level + 1);
localStorage.setItem("maximum_level", maximum_level + 1);
}
show_popup(true);
// Save a high score.
@@ -346,4 +346,4 @@ function lose_game() {
cell.innerText = "";
}
});
}
}
+1 -1
View File
@@ -32,4 +32,4 @@ for (let i = maximum_level; i > 0; --i) {
start_or_continue_game(i);
};
$("#button-list").appendChild(button);
}
}
+31 -31
View File
@@ -15,42 +15,42 @@ function update_particle_canvas_size() {
function spawn_particle(x, y, vel_x, vel_y, rot, vel_rot, img, w, h) {
particles.add({
x: x,
y: y,
vel_x: vel_x,
vel_y: vel_y,
rot: rot,
vel_rot: vel_rot,
img: img,
w: w,
h: h
x: x,
y: y,
vel_x: vel_x,
vel_y: vel_y,
rot: rot,
vel_rot: vel_rot,
img: img,
w: w,
h: h
});
}
function update_particles(delta_time) {
particle_ctx.clearRect(0, 0, particle_canvas.width, particle_canvas.height);
for (particle of particles) {
// Delete off-screen particles, more or less. For now, it's
// designed so that particles go down with the force of
// gravity, so we just check if the particle goes below the
// screen.
if (particle.y > particle_canvas.height) {
particles.delete(particle);
continue;
}
// Update the position and rotation of the particle.
particle.x += particle.vel_x * delta_time;
particle.y += particle.vel_y * delta_time;
particle.rot += particle.vel_rot * delta_time;
// Update the velocity to take into account "gravity".
particle.vel_y += 0.001 * delta_time;
// Render particle.
particle_ctx.save();
particle_ctx.translate(particle.x + particle.w / 2, particle.y + particle.h / 2);
particle_ctx.rotate(particle.rot * Math.PI / 180);
particle_ctx.drawImage(particle.img, -particle.w / 2, -particle.h / 2, particle.w, particle.h)
particle_ctx.restore();
// Delete off-screen particles, more or less. For now, it's
// designed so that particles go down with the force of
// gravity, so we just check if the particle goes below the
// screen.
if (particle.y > particle_canvas.height) {
particles.delete(particle);
continue;
}
// Update the position and rotation of the particle.
particle.x += particle.vel_x * delta_time;
particle.y += particle.vel_y * delta_time;
particle.rot += particle.vel_rot * delta_time;
// Update the velocity to take into account "gravity".
particle.vel_y += 0.001 * delta_time;
// Render particle.
particle_ctx.save();
particle_ctx.translate(particle.x + particle.w / 2, particle.y + particle.h / 2);
particle_ctx.rotate(particle.rot * Math.PI / 180);
particle_ctx.drawImage(particle.img, -particle.w / 2, -particle.h / 2, particle.w, particle.h)
particle_ctx.restore();
}
}
@@ -59,4 +59,4 @@ function particle_loop(timestamp) {
update_particles(timestamp - last_time);
last_time = timestamp;
window.requestAnimationFrame(particle_loop);
}
}