So much stuff happened idek
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
export default {
|
||||||
|
hitbox(e) {
|
||||||
|
// This returns the exact coords on the page
|
||||||
|
let p = e.padding(e.width, e.height);
|
||||||
|
let box = {
|
||||||
|
t:this.y + p.t,
|
||||||
|
b:this.y + this.height - p.b,
|
||||||
|
l:this.x + p.l,
|
||||||
|
r:this.x + this.width - p.r
|
||||||
|
};
|
||||||
|
return box;
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
-10
@@ -1,3 +1,4 @@
|
|||||||
|
import Entity from './entity.js';
|
||||||
export class Player {
|
export class Player {
|
||||||
constructor(game, map, x, y) {
|
constructor(game, map, x, y) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
@@ -10,6 +11,10 @@ export class Player {
|
|||||||
this.speedY = 0;
|
this.speedY = 0;
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
|
this.hitbox = Entity.hitbox;
|
||||||
|
this.padding = function(w, h) {
|
||||||
|
return {t:h/3, b:0, l:1, r:1};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
update(input, audio) {
|
update(input, audio) {
|
||||||
@@ -25,10 +30,12 @@ export class Player {
|
|||||||
this.speedX += 1;
|
this.speedX += 1;
|
||||||
} else if(this.speedX > 0) {
|
} else if(this.speedX > 0) {
|
||||||
this.speedX -= 1;
|
this.speedX -= 1;
|
||||||
|
} else {
|
||||||
|
this.speedX = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Jump
|
// Jump
|
||||||
if(input.includes('ArrowUp') && this.y == floor) {
|
if(input.includes('ArrowUp') && this.y == floor && this.speedY == 0) {
|
||||||
this.speedY = -this.speed*2;
|
this.speedY = -this.speed*2;
|
||||||
audio.play("pip_jump");
|
audio.play("pip_jump");
|
||||||
}
|
}
|
||||||
@@ -92,18 +99,12 @@ export class Player {
|
|||||||
ctx.drawImage(img, this.x, this.y, this.width, this.height);
|
ctx.drawImage(img, this.x, this.y, this.width, this.height);
|
||||||
|
|
||||||
if(hitboxes) {
|
if(hitboxes) {
|
||||||
// T R B L
|
let hitbox = this.hitbox(this);
|
||||||
// 0 1 2 3
|
let padding = this.padding(this.width, this.height);
|
||||||
let hitbox = this.hitbox();
|
|
||||||
ctx.fillStyle = 'blue';
|
ctx.fillStyle = 'blue';
|
||||||
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[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y);
|
|
||||||
ctx.fillRect(this.x, this.y, this.width, this.height);
|
|
||||||
}
|
}
|
||||||
ctx.globalAlpha = 1;
|
ctx.globalAlpha = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
hitbox() {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-25
@@ -1,3 +1,4 @@
|
|||||||
|
import Entity from './entity.js';
|
||||||
export class Zombie {
|
export class Zombie {
|
||||||
constructor(game, map, x, y) {
|
constructor(game, map, x, y) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
@@ -11,16 +12,25 @@ export class Zombie {
|
|||||||
this.speedY = 0;
|
this.speedY = 0;
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
|
this.hitbox = Entity.hitbox;
|
||||||
|
this.padding = function(w, h) {
|
||||||
|
return {t:1, b:0, l:6, r:6};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
update(audio) {
|
update(audio) {
|
||||||
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);
|
||||||
|
|
||||||
//direction = Math.floor(Math.random() * 2)?-1:1;
|
let frameRand = Math.floor(Math.random() * 5) + 40;
|
||||||
|
if(this.game.frame % frameRand == 0) {
|
||||||
|
this.direction = Math.floor(Math.random() * 2)?-1:1;
|
||||||
|
}
|
||||||
|
|
||||||
//if(this.x > this.game.width-10) {
|
//if(this.x > this.game.width-10) {
|
||||||
if(this.x < 50 || this.x > 150) {
|
if(this.x < 50) {
|
||||||
this.direction = this.direction * -1;
|
this.direction = 1;
|
||||||
|
} else if(this.x > game.width - 50 - this.width) {
|
||||||
|
this.direction = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.speedX = this.direction * this.speed;
|
this.speedX = this.direction * this.speed;
|
||||||
@@ -51,30 +61,22 @@ export class Zombie {
|
|||||||
}
|
}
|
||||||
ctx.drawImage(img, this.x, this.y, this.width, this.height);
|
ctx.drawImage(img, this.x, this.y, this.width, this.height);
|
||||||
|
|
||||||
if(hitboxes) {
|
// Draw Health Bar
|
||||||
// T R B L
|
let healthPadding = 5;
|
||||||
// 0 1 2 3
|
let healthHeight = 2;
|
||||||
ctx.globalAlpha = 0.4;
|
let healthWidth = this.width/2;
|
||||||
//let hitbox = this.hitbox();
|
ctx.fillStyle = 'red';
|
||||||
|
ctx.fillRect(this.x + healthWidth/2, this.y-healthPadding, healthWidth, healthHeight);
|
||||||
ctx.fillStyle = 'green';
|
ctx.fillStyle = 'green';
|
||||||
//ctx.fillRect(hitbox[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y);
|
ctx.fillRect(this.x+healthWidth/2, this.y-healthPadding, (this.hearts/3*healthWidth), healthHeight);
|
||||||
ctx.fillRect(this.x, this.y, this.width, this.height);
|
|
||||||
|
if(hitboxes) {
|
||||||
|
let hitbox = this.hitbox(this);
|
||||||
|
let padding = this.padding(this.width, this.height);
|
||||||
|
ctx.fillStyle = 'red';
|
||||||
|
ctx.globalAlpha = 0.4;
|
||||||
|
ctx.fillRect(hitbox.l, hitbox.t, hitbox.r-this.x-padding.l, hitbox.b-this.y-padding.t);
|
||||||
}
|
}
|
||||||
ctx.globalAlpha = 1;
|
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];
|
|
||||||
}*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-12
@@ -23,10 +23,16 @@ window.addEventListener('load', function() {
|
|||||||
|
|
||||||
this.hitboxes = false;
|
this.hitboxes = false;
|
||||||
|
|
||||||
this.map = new Map(this, "start");
|
this.map = new Map(this, "arcadia");
|
||||||
this.player = new Player(this, this.map, 100, 0);
|
this.map.player = new Player(this, this.map, this.map.map.spawn.x, this.map.map.spawn.y);
|
||||||
this.ghost = new Ghost(this, this.map, 100, 0, this.player);
|
this.map.ghost = new Ghost(this, this.map, this.map.map.spawn.x, this.map.map.spawn.y, this.map.player);
|
||||||
this.zombie = new Zombie(this, this.map, 100, 0);
|
|
||||||
|
for(var i = 0;i < this.map.enemies.length;i++) {
|
||||||
|
let enemy = this.map.enemies[i];
|
||||||
|
let x = enemy.x;
|
||||||
|
let y = enemy.y;
|
||||||
|
enemy.entity = new Zombie(this, this.map, x, y);
|
||||||
|
}
|
||||||
|
|
||||||
this.ui = new UI(this, this.map);
|
this.ui = new UI(this, this.map);
|
||||||
this.audio = new AudioHandler();
|
this.audio = new AudioHandler();
|
||||||
@@ -34,20 +40,31 @@ window.addEventListener('load', function() {
|
|||||||
}
|
}
|
||||||
update() {
|
update() {
|
||||||
this.frame++;
|
this.frame++;
|
||||||
this.player.update(this.input.keys, this.audio);
|
this.map.player.update(this.input.keys, this.audio);
|
||||||
this.ghost.update(this.audio);
|
this.map.ghost.update(this.audio);
|
||||||
this.zombie.update(this.audio);
|
|
||||||
|
|
||||||
this.ui.update(this.audio);
|
// Update enemies in the current map
|
||||||
|
for(var i = 0;i < this.map.enemies.length;i++) {
|
||||||
|
let entity = this.map.enemies[i].entity;
|
||||||
|
entity.update(this.audio);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.ui.update(this);
|
||||||
}
|
}
|
||||||
draw(ctx) {
|
draw(ctx) {
|
||||||
this.clear(ctx);
|
this.clear(ctx);
|
||||||
this.map.draw(ctx);
|
this.map.draw(ctx);
|
||||||
this.ghost.draw(ctx, this.hitboxes);
|
|
||||||
this.player.draw(ctx, this.hitboxes);
|
|
||||||
this.zombie.draw(ctx, this.hitboxes);
|
|
||||||
|
|
||||||
this.ui.draw(ctx, this.player);
|
// Update enemies in the current map
|
||||||
|
for(var i = 0;i < this.map.enemies.length;i++) {
|
||||||
|
let entity = this.map.enemies[i].entity;
|
||||||
|
entity.draw(ctx, this.hitboxes);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.map.ghost.draw(ctx, this.hitboxes);
|
||||||
|
this.map.player.draw(ctx, this.hitboxes);
|
||||||
|
|
||||||
|
this.ui.draw(ctx, this.map.player);
|
||||||
}
|
}
|
||||||
clear(ctx) {
|
clear(ctx) {
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
|||||||
+4
-2
@@ -7,7 +7,8 @@ export class InputHandler {
|
|||||||
e.key === 'ArrowUp' ||
|
e.key === 'ArrowUp' ||
|
||||||
e.key === 'ArrowLeft' ||
|
e.key === 'ArrowLeft' ||
|
||||||
e.key === 'ArrowRight' ||
|
e.key === 'ArrowRight' ||
|
||||||
e.key === 'Enter'
|
e.key === 'Enter' ||
|
||||||
|
e.key === 'b'
|
||||||
) && this.keys.indexOf(e.key) === -1) {
|
) && this.keys.indexOf(e.key) === -1) {
|
||||||
this.keys.push(e.key);
|
this.keys.push(e.key);
|
||||||
}
|
}
|
||||||
@@ -18,7 +19,8 @@ export class InputHandler {
|
|||||||
e.key === 'ArrowUp' ||
|
e.key === 'ArrowUp' ||
|
||||||
e.key === 'ArrowLeft' ||
|
e.key === 'ArrowLeft' ||
|
||||||
e.key === 'ArrowRight' ||
|
e.key === 'ArrowRight' ||
|
||||||
e.key === 'Enter') {
|
e.key === 'Enter' ||
|
||||||
|
e.key === 'b') {
|
||||||
this.keys.splice(this.keys.indexOf(e.key), 1);
|
this.keys.splice(this.keys.indexOf(e.key), 1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
export default {
|
||||||
|
data: {
|
||||||
|
name: "Arcadia",
|
||||||
|
gravity: 1,
|
||||||
|
spawn: {x:100, y:0},
|
||||||
|
platforms: [
|
||||||
|
{x: 150, y: 200, w: 100, h: 10},
|
||||||
|
{x: 100, y: 220, w: 100, h: 10},
|
||||||
|
{x: 0, y: 260, w: 500, h: 10}
|
||||||
|
],
|
||||||
|
enemies: [
|
||||||
|
{entity: null,type: "zombie", x:130, y:100},
|
||||||
|
{entity: null,type: "zombie", x:140, y:50}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
-8
@@ -1,14 +1,14 @@
|
|||||||
|
import data from './arcadia.js';
|
||||||
export class Map {
|
export class Map {
|
||||||
constructor(game, mapName) {
|
constructor(game, mapName) {
|
||||||
this.game = game;
|
this.game = game;
|
||||||
this.map = mapName;
|
this.map = data.data;
|
||||||
this.gravity = 1;
|
this.player = null;
|
||||||
// x, y, w, h
|
this.ghost = null;
|
||||||
this.platforms = [
|
this.mapName = mapName;
|
||||||
{x: 150, y: 200, w: 100, h: 10},
|
this.platforms = this.map['platforms'];
|
||||||
{x: 100, y: 220, w: 100, h: 10},
|
this.enemies = this.map['enemies'];
|
||||||
{x: 0, y: game.height-10, w: game.width, h: 10}
|
this.gravity = this.map.gravity;
|
||||||
]
|
|
||||||
}
|
}
|
||||||
draw(ctx) {
|
draw(ctx) {
|
||||||
for(var i = 0;i < this.platforms.length;i++) {
|
for(var i = 0;i < this.platforms.length;i++) {
|
||||||
|
|||||||
@@ -22,11 +22,24 @@ export class UI {
|
|||||||
|
|
||||||
|
|
||||||
// Frame Counter
|
// Frame Counter
|
||||||
//ctx.font = "10px Arial";
|
if(this.game.hitboxes) {
|
||||||
//ctx.fillText(this.game.frame, 0, 10);
|
ctx.fillStyle = 'black';
|
||||||
|
ctx.font = "10px Arial";
|
||||||
|
ctx.fillText(this.game.frame, 0, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
update(game) {
|
||||||
|
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) {
|
||||||
|
this.game.hitboxes = false;
|
||||||
|
} else {
|
||||||
|
this.game.hitboxes = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
update(entity) {
|
|
||||||
this.hearts = entity.hearts;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user