Major rewrite, better organization with split up files. Need to redo/make hitbox detection for entities. Also now using request animation instead of set interval. Need to update platform detection to match entire platform top to bottom instead of just the top

This commit is contained in:
rain
2023-11-15 01:48:21 -06:00
parent c28a0cb87f
commit 839d6c2c20
10 changed files with 461 additions and 516 deletions
+69
View File
@@ -0,0 +1,69 @@
export class Ghost {
constructor(game, map, x, y, target) {
this.game = game;
this.map = map;
this.speed = 2;
this.width = 20;
this.height = 20;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.target = target;
this.hiding = false;
this.scared = 0;
}
update(audio) {
// 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));
if(distance < 2) {
this.hiding = true;
return;
} else if(distance > 100 && this.hiding) {
this.hiding = false;
this.scared = 20;
audio.play("ghost_squeak");
}
if(this.hiding) {
this.speedX = 0;
this.speedY = 0;
} else {
// Determine where the player will be and move there instead
let distX = this.speedX = this.target.x + (this.target.speedX / 100 * this.target.speed) - this.x;
let distY = this.speedY = this.target.y + (this.target.speedY / 100 * this.target.speed) - this.y;
this.speedX = ((distX) / 100) * this.speed;
this.speedY = ((distY) / 100) * this.speed;
}
this.x += this.speedX;
this.y += this.speedY;
}
draw(ctx, hitboxes) {
// Control the transition point between animations
let animateDist = .5;
let img = document.getElementById(`ghost`);
if(this.speedY > animateDist && (this.speedX > -animateDist && this.speedX < animateDist)) {
img = document.getElementById(`down_ghost`);
}else if(this.speedY < -animateDist && (this.speedX > -animateDist && this.speedX < animateDist)) {
img = document.getElementById(`up_ghost`);
}else if(this.speedX < -animateDist) {
img = document.getElementById(`left_ghost`);
} else if (this.speedX > animateDist) {
img = document.getElementById(`right_ghost`);
}
ctx.drawImage(img, this.x, this.y, this.width, this.height);
if(this.scared > 0) {
let img = document.getElementById(`exclaimation`);
let topCenterX = this.x+this.width/2.5;
let topCenterY = this.y-this.height/2;
ctx.drawImage(img, topCenterX, topCenterY, this.width/3, this.height/3);
this.scared -= 1;
}
}
}
+109
View File
@@ -0,0 +1,109 @@
export class Player {
constructor(game, map, x, y) {
this.game = game;
this.map = map;
this.hearts = 3;
this.speed = 6;
this.width = 30;
this.height = 30;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
}
update(input, audio) {
let floor = this.map.floor(this.x, this.y, this.width, this.height);
// Left / Right
if(input.includes('ArrowRight') && this.speedX < this.speed) {
this.speedX += 1;
} else if(input.includes('ArrowLeft') && this.speedX > -this.speed) {
this.speedX -= 1;
} else if(this.speedX < 0) {
this.speedX += 1;
} else if(this.speedX > 0) {
this.speedX -= 1;
}
// Jump
if(input.includes('ArrowUp') && this.y == floor) {
this.speedY = -this.speed*2;
audio.play("pip_jump");
}
// Down
if(input.includes('ArrowDown')) {
if(this.y < floor) {
// Speed Fall
this.speedY += 1;
} else {
if(this.height != 15) {
this.y += 15;
audio.play("pip_duck");
}
this.height = 15;
}
} else {
if(this.height == 15) {
this.y -= 15;
}
this.height = 30;
}
// Apply final movement Calculations
this.x += this.speedX;
// Recalc the floor
floor = this.map.floor(this.x, this.y, this.width, this.height);
// Apply Gravity
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;
this.speedY = 0;
} else {
this.y += this.speedY;
}
}
draw(ctx, hitboxes) {
let img = document.getElementById("player");
if(this.speedY >= -(this.speed/2) && this.speedY < 0) {
img = document.getElementById("up_player");
}else if(this.speedY <= this.speed && this.speedY > (this.speed/2)) {
img = document.getElementById("down_player");
}else if(this.speedX < 0) {
img = document.getElementById("left_player");
} else if (this.speedX > 0) {
img = document.getElementById("right_player");
}
// Check if taking damage
// Decay iframes
/*if(this.iframes > 0) {
if(game.frameNo % 5) {
ctx.globalAlpha = 0.4;
}
this.iframes -= 1;
}*/
ctx.drawImage(img, this.x, this.y, this.width, this.height);
if(hitboxes) {
// T R B L
// 0 1 2 3
let hitbox = this.hitbox();
ctx.fillStyle = 'blue';
ctx.globalAlpha = 0.4;
//ctx.fillRect(hitbox[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y);
ctx.fillRect(this.x, this.y, this.width, this.height);
}
ctx.globalAlpha = 1;
}
hitbox() {
}
}
+80
View File
@@ -0,0 +1,80 @@
export class Zombie {
constructor(game, map, x, y) {
this.game = game;
this.map = map;
this.hearts = 3;
this.speed = 1;
this.direction = 1;
this.width = 30;
this.height = 30;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
}
update(audio) {
let floor = this.map.floor(this.x, this.y, this.width, this.height);
//direction = Math.floor(Math.random() * 2)?-1:1;
//if(this.x > this.game.width-10) {
if(this.x < 50 || this.x > 150) {
this.direction = this.direction * -1;
}
this.speedX = this.direction * this.speed;
this.x += this.speedX;
// Recalc the floor
floor = this.map.floor(this.x, this.y, this.width, this.height);
// Gravity
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;
this.speedY = 0;
} else {
this.y += this.speedY;
}
}
draw(ctx, hitboxes) {
let img = document.getElementById(`left_zombie`);
if(this.speedX < 0) {
img = document.getElementById(`left_zombie`);
} else {
img = document.getElementById(`right_zombie`);
}
ctx.drawImage(img, this.x, this.y, this.width, this.height);
if(hitboxes) {
// T R B L
// 0 1 2 3
ctx.globalAlpha = 0.4;
//let hitbox = this.hitbox();
ctx.fillStyle = 'green';
//ctx.fillRect(hitbox[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y);
ctx.fillRect(this.x, this.y, this.width, this.height);
}
ctx.globalAlpha = 1;
}
/*hitbox() {
let paddingTop = (this.height * 0.05);
let paddingBottom = (0) - paddingTop;
let paddingLeft = (this.width * 0.20);
let paddingRight = (0) - paddingLeft*2;
let top = this.y + paddingTop;
let right = this.x + this.width + paddingRight;
let bottom = this.y + this.height + paddingBottom;
let left = this.x + paddingLeft;
return [top, right, bottom, left];
}*/
}