💃😽 Checkpoint

./script.js:9517054/13602
This commit is contained in:
Glitch (glitch-hello-website) 2022-03-06 01:19:07 +00:00
parent e511b3bae4
commit a39a119ad8

View file

@ -37,7 +37,32 @@ function draw() {
const rect = (x, y, w, h) => ctx.fillRect(...point(x, y), w, h);
// draw background
background(60, 0.5);
color("background");
ctx.fillRect(0, 0, game.size[0], game.size[1]);
color("dots");
for (let i = 0; i < 30; i++) {
for (let j = 0; j < 40; j++) {
const spacing = 60;
const depth = 0.5;
ctx.beginPath();
// ctx.arc(
// i * spacing - (offx % (spacing / depth)) * depth,
// j * spacing - (offy % (spacing / depth)) * depth,
// 5,
// 0,
// 2 * Math.PI
// );
ctx.rect(
i * spacing - (offx % (spacing / depth)) * depth,
j * spacing - (offy % (spacing / depth)) * depth,
10,
10,
);
ctx.fill();
}
}
// draw checkpoint animation (if there is one)
if (game.anim[2] < 1) {
@ -69,40 +94,6 @@ function draw() {
rect(game.player.x, game.player.y, game.player.size, game.player.size);
game.cache.dirty = true;
// draw and cache background
function background(spacing, depth) {
const tilesize = 15;
// always use cached background if it exists
if (game.cache.background) {
for (let i = -1; i < 3; i++) {
for (let j = -1; j < 3; j++) {
ctx.putImageData(
game.cache.background,
i * spacing * tilesize - (offx % (spacing / depth)) * depth,
j * spacing * tilesize - (offy % (spacing / depth)) * depth
);
}
}
return;
}
// draw the background first time
color("background");
ctx.fillRect(0, 0, game.size[0], game.size[1]);
color("dots");
for (let i = 0; i < tilesize + 1; i++) {
for (let j = 0; j < tilesize + 1; j++) {
ctx.beginPath();
ctx.arc(i * spacing, j * spacing, 5, 0, 2 * Math.PI);
ctx.fill();
}
}
game.cache.background = ctx.getImageData(0, 0, game.size[0], game.size[1]);
}
}
function update() {
@ -118,14 +109,14 @@ function update() {
if (i.right) p.velx += speed;
// update x position
for (let i = 0; i < Math.abs(p.velx) - 1; i++) {
p.x += p.velx < 0 ? -1 : 1;
const c = collision();
if (c) {
p.x -= p.velx < 0 ? -1 : 1;
p.velx = 0;
}
for (let i = 0; i < Math.abs(p.velx) - 1; i++) {
p.x += p.velx < 0 ? -1 : 1;
const c = collision();
if (c) {
p.x -= p.velx < 0 ? -1 : 1;
p.velx = 0;
}
}
// 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;