Fixed some bugs with platform collision detection

This commit is contained in:
rain
2023-11-16 23:37:10 -06:00
parent 80216e5c30
commit d6bb0453c3
2 changed files with 47 additions and 21 deletions
+28 -16
View File
@@ -19,15 +19,14 @@ export class Player {
update(input, audio) { update(input, audio) {
// TODO: Make ceiling and floor based on hitbox let h = this.hitbox(this);
//let h = this.hitbox; let width = h.r-h.l;
//let width = h.r-h.l; let height = h.b-h.t;
//let height = h.b-h.t; let padding = this.padding(this.width, this.height);
let floor = this.map.floor(h.l, h.t, width, height);
//let floor = this.map.floor(h.l, h.t, width, height); let ceiling = this.map.ceiling(h.l, h.t, width, height);
//let ceiling = this.map.ceiling(h.l, h.t, h.r-h.l, h.b-h.t); //let floor = this.map.floor(this.x, this.y, this.width, this.height);
let floor = this.map.floor(this.x, this.y, this.width, this.height); //let ceiling = this.map.ceiling(this.x, this.y, this.width, this.height);
let ceiling = this.map.ceiling(this.x, this.y, this.width, this.height);
// Left / Right // Left / Right
if(input.includes('ArrowRight') && this.speedX < this.speed) { if(input.includes('ArrowRight') && this.speedX < this.speed) {
@@ -43,14 +42,14 @@ export class Player {
} }
// Jump // Jump
if(input.includes('ArrowUp') && this.y+this.height == floor && this.speedY == 0) { if(input.includes('ArrowUp') && h.b == floor && this.speedY == 0) {
this.speedY = -this.speed*2; this.speedY = -this.speed*2;
audio.play("pip_jump"); audio.play("pip_jump");
} }
// Down // Down
if(input.includes('ArrowDown')) { if(input.includes('ArrowDown')) {
if(this.y+this.height < floor) { if(h.b < floor) {
// Speed Fall // Speed Fall
this.speedY += 1; this.speedY += 1;
} else { } else {
@@ -71,19 +70,25 @@ export class Player {
this.x += this.speedX; this.x += this.speedX;
// Recalc the floor // Recalc the floor
floor = this.map.floor(this.x, this.y, this.width, this.height); //floor = this.map.floor(this.x, this.y, this.width, this.height);
h = this.hitbox(this);
width = h.r-h.l;
height = h.b-h.t;
padding = this.padding(this.width, this.height);
floor = this.map.floor(h.l, h.t, width, height);
ceiling = this.map.ceiling(h.l, h.t, width, height);
// Apply Gravity // Apply Gravity
this.speedY += this.map.gravity; this.speedY += this.map.gravity;
// if skipping the platform when falling, set to platform // if skipping the platform when falling, set to platform
if(this.y+this.height <= floor && this.y+this.height+this.speedY >= floor) { if(h.b > floor || h.b+this.speedY >= floor) {
// Land on platforms // Land on platforms
this.y = floor-this.height; this.y = floor - this.height + padding.b;
this.speedY = 0; this.speedY = 0;
} else if(this.y >= ceiling && this.y+this.speedY <= ceiling) { } else if(h.t < ceiling || h.t+this.speedY <= ceiling) {
// Hit your head on platforms // Hit your head on platforms
this.y = ceiling; this.y = ceiling - padding.t;
this.speedY = 0; this.speedY = 0;
} else { } else {
this.y += this.speedY; this.y += this.speedY;
@@ -118,6 +123,13 @@ export class Player {
ctx.globalAlpha = 0.4; ctx.globalAlpha = 0.4;
ctx.fillRect(hitbox.l, hitbox.t, hitbox.r-this.x-padding.l, hitbox.b-this.y-padding.t); ctx.fillRect(hitbox.l, hitbox.t, hitbox.r-this.x-padding.l, hitbox.b-this.y-padding.t);
ctx.fillStyle = 'green';
// Mark the Top
ctx.fillRect(hitbox.l, hitbox.t, hitbox.r-this.x-padding.l, 2);
ctx.fillStyle = 'red';
// Mark the Bottom
ctx.fillRect(hitbox.l, hitbox.b, hitbox.r-this.x-padding.l, -2);
// Draw Floor Marker // Draw Floor Marker
let floor = this.map.floor(this.x, this.y, this.width, this.height); let floor = this.map.floor(this.x, this.y, this.width, this.height);
ctx.fillStyle = 'yellow'; ctx.fillStyle = 'yellow';
+19 -5
View File
@@ -19,7 +19,12 @@ export class Zombie {
} }
update(audio) { update(audio) {
let floor = this.map.floor(this.x, this.y, this.width, this.height); let h = this.hitbox(this);
let width = h.r-h.l;
let height = h.b-h.t;
let padding = this.padding(this.width, this.height);
let floor = this.map.floor(h.l, h.t, width, height);
let ceiling = this.map.ceiling(h.l, h.t, width, height);
let frameRand = Math.floor(Math.random() * 5) + 40; let frameRand = Math.floor(Math.random() * 5) + 40;
if(this.game.frame % frameRand == 0) { if(this.game.frame % frameRand == 0) {
@@ -37,21 +42,30 @@ export class Zombie {
this.x += this.speedX; this.x += this.speedX;
// Recalc the floor // Recalc the floor
floor = this.map.floor(this.x, this.y, this.width, this.height); h = this.hitbox(this);
width = h.r-h.l;
height = h.b-h.t;
padding = this.padding(this.width, this.height);
floor = this.map.floor(h.l, h.t, width, height);
ceiling = this.map.ceiling(h.l, h.t, width, height);
// Gravity // Gravity
this.speedY += this.map.gravity; this.speedY += this.map.gravity;
// if skipping the platform when falling, set to platform // if skipping the platform when falling, set to platform
if(this.y+this.height <= floor && this.y+this.height+this.speedY >= floor) { if(h.b > floor || h.b+this.speedY >= floor) {
this.y = floor-this.height; // Land on platforms
this.y = floor - this.height + padding.b;
this.speedY = 0;
} else if(h.t < ceiling || h.t+this.speedY <= ceiling) {
// Hit your head on platforms
this.y = ceiling - padding.t;
this.speedY = 0; this.speedY = 0;
} else { } else {
this.y += this.speedY; this.y += this.speedY;
} }
} }
draw(ctx, hitboxes) { draw(ctx, hitboxes) {
let img = document.getElementById(`left_zombie`); let img = document.getElementById(`left_zombie`);
if(this.speedX < 0) { if(this.speedX < 0) {