Added ceiling to jump calculations, increased the features available when displaying devtools and fixed the toggle glitch

This commit is contained in:
rain
2023-11-16 21:36:27 -06:00
parent 1eef69f514
commit 8013ba262a
6 changed files with 114 additions and 14 deletions
+9
View File
@@ -14,6 +14,15 @@ export class Ghost {
this.scared = 0;
}
update(audio) {
// This is in case the ghost is tracking a single point instead of another entity
if(this.target.speedX === undefined) {
this.target.speedX = 0;
} if(this.target.speedY === undefined) {
this.target.speedY = 0;
} if(this.target.speed === undefined) {
this.target.speed = 0;
}
// if the ghost is hiding, only move after player gets a certain distance away
let distance = Math.sqrt(Math.pow(this.x - this.target.x, 2) + Math.pow(this.y - this.target.y, 2));
+26 -4
View File
@@ -19,7 +19,15 @@ export class Player {
update(input, audio) {
// TODO: Make ceiling and floor based on hitbox
//let h = this.hitbox;
//let width = h.r-h.l;
//let height = h.b-h.t;
//let floor = this.map.floor(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 ceiling = this.map.ceiling(this.x, this.y, this.width, this.height);
// Left / Right
if(input.includes('ArrowRight') && this.speedX < this.speed) {
@@ -35,14 +43,14 @@ export class Player {
}
// Jump
if(input.includes('ArrowUp') && this.y == floor && this.speedY == 0) {
if(input.includes('ArrowUp') && this.y+this.height == floor && this.speedY == 0) {
this.speedY = -this.speed*2;
audio.play("pip_jump");
}
// Down
if(input.includes('ArrowDown')) {
if(this.y < floor) {
if(this.y+this.height < floor) {
// Speed Fall
this.speedY += 1;
} else {
@@ -69,8 +77,13 @@ export class Player {
this.speedY += this.map.gravity;
// if skipping the platform when falling, set to platform
if(this.y <= floor && this.y+this.speedY >= floor) {
this.y = floor;
if(this.y+this.height <= floor && this.y+this.height+this.speedY >= floor) {
// Land on platforms
this.y = floor-this.height;
this.speedY = 0;
} else if(this.y >= ceiling && this.y+this.speedY <= ceiling) {
// Hit your head on platforms
this.y = ceiling;
this.speedY = 0;
} else {
this.y += this.speedY;
@@ -104,6 +117,15 @@ export class Player {
ctx.fillStyle = 'blue';
ctx.globalAlpha = 0.4;
ctx.fillRect(hitbox.l, hitbox.t, hitbox.r-this.x-padding.l, hitbox.b-this.y-padding.t);
// Draw Floor Marker
let floor = this.map.floor(this.x, this.y, this.width, this.height);
ctx.fillStyle = 'yellow';
ctx.fillRect(this.x, floor, this.width, 10);
// Draw Ceiling Marker
let ceiling = this.map.ceiling(this.x, this.y, this.width, this.height);
ctx.fillRect(this.x, ceiling, this.width, 10);
}
ctx.globalAlpha = 1;
}
+2 -2
View File
@@ -43,8 +43,8 @@ export class Zombie {
this.speedY += this.map.gravity;
// if skipping the platform when falling, set to platform
if(this.y <= floor && this.y+this.speedY >= floor) {
this.y = floor;
if(this.y+this.height <= floor && this.y+this.height+this.speedY >= floor) {
this.y = floor-this.height;
this.speedY = 0;
} else {
this.y += this.speedY;
+25 -2
View File
@@ -23,7 +23,28 @@ window.addEventListener('load', function() {
this.hitboxes = false;
this.map = new Map(this, "arcadia");
this.map = new Map(this, 'arcadia');
this.map.player = new Player(this, this.map, this.map.map.spawn.x, this.map.map.spawn.y);
this.map.ghost = new Ghost(this, this.map, this.map.map.spawn.x, this.map.map.spawn.y, this.map.player);
// Make the ghost stay at a certain point on the map example...
//this.map.ghost = new Ghost(this, this.map, this.map.map.spawn.x, this.map.map.spawn.y, {x:300, y:150});
for(var i = 0;i < this.map.enemies.length;i++) {
let enemy = this.map.enemies[i];
let x = enemy.x;
let y = enemy.y;
if(enemy.type === 'zombie') {
enemy.entity = new Zombie(this, this.map, x, y);
}
}
this.ui = new UI(this, this.map);
this.audio = new AudioHandler();
this.input = new InputHandler();
}
loadMap(mapName) {
this.map = new Map(this, mapName);
this.map.player = new Player(this, this.map, this.map.map.spawn.x, this.map.map.spawn.y);
this.map.ghost = new Ghost(this, this.map, this.map.map.spawn.x, this.map.map.spawn.y, this.map.player);
@@ -31,14 +52,16 @@ window.addEventListener('load', function() {
let enemy = this.map.enemies[i];
let x = enemy.x;
let y = enemy.y;
if(enemy.type === 'zombie') {
enemy.entity = new Zombie(this, this.map, x, y);
}
}
this.ui = new UI(this, this.map);
this.audio = new AudioHandler();
this.input = new InputHandler();
}
update() {
update(ctx) {
this.frame++;
this.map.player.update(this.input.keys, this.audio);
this.map.ghost.update(this.audio);
+44 -2
View File
@@ -17,17 +17,59 @@ export class Map {
ctx.fillRect(p.x, p.y, p.w, p.h);
}
}
// TODO: Make ceiling and floor based on hitboxes
floor(x, y, w, h) {
let closestPlatform = game.height;
for(var i = 0;i < this.platforms.length;i++) {
let p = this.platforms[i];
if(y+h <= p.y && x < p.x+p.w && x+w > p.x) {
if(closestPlatform > p.y - h) {
closestPlatform = p.y - h;
if(closestPlatform > p.y) {
closestPlatform = p.y;
}
}
}
return closestPlatform;
}
ceiling(x, y, w, h) {
let closestPlatform = 0;
for(var i = 0;i < this.platforms.length;i++) {
let p = this.platforms[i];
if(y >= p.y+p.h && x < p.x+p.w && x+w > p.x) {
if(closestPlatform < p.y + p.h) {
closestPlatform = p.y + p.h;
}
}
}
return closestPlatform;
}
/*
floor(x, y, w, h) {
let closestPlatform = game.height;
for(var i = 0;i < this.platforms.length;i++) {
let p = this.platforms[i];
if(y+h <= p.y && x < p.x+p.w && x+w > p.x) {
if(closestPlatform > p.y) {
closestPlatform = p.y;
}
}
}
return closestPlatform;
}
ceiling(x, y, w, h) {
let closestPlatform = 0;
for(var i = 0;i < this.platforms.length;i++) {
let p = this.platforms[i];
if(y >= p.y+p.h && x < p.x+p.w && x+w > p.x) {
if(closestPlatform < p.y + p.h) {
closestPlatform = p.y + p.h;
}
}
}
return closestPlatform;
}
*/
}
+7 -3
View File
@@ -8,6 +8,7 @@ export class UI {
this.padding = 5;
this.x = this.game.width;
this.y = 0;
this.devtools = false; // Used to toggle state on hitboxes
}
draw(ctx, entity) {
@@ -32,14 +33,17 @@ export class UI {
this.game = game;
this.hearts = this.game.map.player.hearts;
// TODO: Adjust this later so players can long press 'b' with the same effects
let keys = this.game.input.keys;
if(keys.includes('b')) {
if(this.game.hitboxes) {
if(this.game.hitboxes && !this.devtools) {
this.game.hitboxes = false;
} else {
this.devtools = true;
} else if(!this.game.hitboxes && !this.devtools) {
this.game.hitboxes = true;
this.devtools = true;
}
} else {
this.devtools = false;
}
}
}