🚊🛫 Checkpoint
./script.js:9517054/10573
This commit is contained in:
parent
8e0a25325b
commit
ecefe02aa2
1 changed files with 37 additions and 29 deletions
66
script.js
66
script.js
|
@ -105,38 +105,44 @@ function update() {
|
|||
// reset jump if they're falling or already jumping (there's a small grace period though)
|
||||
if (Math.abs(p.vely) > 3) p.canJump = false;
|
||||
|
||||
// update y position
|
||||
const collided = move("y", p.vely, p.vely < 0 ? -1 : 1);
|
||||
if (collided) {
|
||||
if (p.vely >= 0) p.canJump = true;
|
||||
if (p.vely >= 0) {
|
||||
// if they fell onto a platform then let them jump
|
||||
p.canJump = true;
|
||||
|
||||
// if they hit a checkpoint
|
||||
if (collided[4] === "checkpoint" || collided[4] === "final") {
|
||||
for (let part of game.parts.filter((i) => i[4] === "active")) {
|
||||
part[4] = "checkpoint";
|
||||
// if they hit a checkpoint
|
||||
if (collided[4] === "checkpoint" || collided[4] === "final") {
|
||||
// unset the other checkpoints
|
||||
for (let part of game.parts.filter((i) => i[4] === "active")) {
|
||||
part[4] = "checkpoint";
|
||||
}
|
||||
|
||||
const x = collided[0] + collided[2] / 2;
|
||||
const y = collided[1] + collided[3] / 2;
|
||||
|
||||
// play the checkpoint animation
|
||||
game.anim = [x, y, 0];
|
||||
game.player.savex = x - game.player.size / 2;
|
||||
game.player.savey = y - collided[3] / 2 - game.player.size;
|
||||
|
||||
// they won!
|
||||
if (collided[4] === "final") {
|
||||
alert("ayo you win");
|
||||
|
||||
// reset the inputs, `alert()` does weird things
|
||||
input.up = false;
|
||||
input.down = false;
|
||||
input.left = false;
|
||||
input.right = false;
|
||||
}
|
||||
|
||||
// don't spam animation; change platform color
|
||||
collided[4] = collided[4] === "final" ? "win" : "active";
|
||||
}
|
||||
|
||||
const x = collided[0] + collided[2] / 2;
|
||||
const y = collided[1] + collided[3] / 2;
|
||||
|
||||
// play the checkpoint animation
|
||||
game.anim = [x, y, 0];
|
||||
game.player.savex = x - game.player.size / 2;
|
||||
game.player.savey = y - collided[3] / 2 - game.player.size;
|
||||
|
||||
// they won!
|
||||
if (collided[4] === "final") {
|
||||
alert("ayo you win");
|
||||
|
||||
// reset the inputs, `alert()` does weird things
|
||||
input.up = false;
|
||||
input.down = false;
|
||||
input.left = false;
|
||||
input.right = false;
|
||||
}
|
||||
|
||||
// don't spam animation; change platform color
|
||||
collided[4] = collided[4] === "final" ? "win" : "active";
|
||||
}
|
||||
|
||||
p.vely = 0;
|
||||
}
|
||||
|
||||
|
@ -151,6 +157,7 @@ function update() {
|
|||
c[0] = (c[0] * 9 + p.x) / 10;
|
||||
c[1] = (c[1] * 9 + p.y) / 10;
|
||||
|
||||
// move, and return the platform the player collided with if any
|
||||
function move(key, amt, speed) {
|
||||
for (let i = 0; i < Math.abs(amt) - 1; i++) {
|
||||
p[key] += speed;
|
||||
|
@ -160,9 +167,10 @@ function update() {
|
|||
return c;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
// check if there was a collision
|
||||
function collision() {
|
||||
const s = game.player.size;
|
||||
for (let part of game.parts) {
|
||||
|
@ -177,6 +185,7 @@ function update() {
|
|||
}
|
||||
}
|
||||
|
||||
// handle input
|
||||
function input(e) {
|
||||
const i = game.input;
|
||||
const down = e.type === "keydown";
|
||||
|
@ -195,7 +204,6 @@ function input(e) {
|
|||
}
|
||||
|
||||
// run the game!
|
||||
|
||||
resize();
|
||||
draw();
|
||||
setInterval(update, 1000 / 60);
|
||||
|
|
Loading…
Reference in a new issue