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
+21 -20
View File
@@ -1,32 +1,33 @@
:root {
--fg: #ffffff;
--bg: #000000;
--accent: #d3d3d3;
}
body, html { body, html {
color: white; color: var(--fg);
background: black; background: var(--bg);
} }
canvas { canvas {
width: auto; border:1px solid var(--accent);
height: 90%; background: rgb(92,88,164);
background: linear-gradient(180deg, rgba(92,88,164,1) 30%, rgba(156,90,188,1) 100%);
image-rendering: pixelated; image-rendering: pixelated;
display: flex; position: absolute;
margin: 0 auto; top: 50%;
left: 50%;
border:1px solid #d3d3d3; transform: translate(-50%, -50%);
background: rgb(92,88,164); width: 90%;
background: linear-gradient(180deg, rgba(92,88,164,1) 30%, rgba(156,90,188,1) 100%); height: auto;
} }
h1,h2,h3 { h1,h2,h3 {
text-align: center; text-align: center;
} }
button { .sprites {
display: flex; display: none;
padding: 5px;
margin: 10px auto;
}
.sprite {
display: none;
} }
+21 -19
View File
@@ -5,33 +5,35 @@
<link rel="icon" type="image/png" href="img/pip.png"> <link rel="icon" type="image/png" href="img/pip.png">
<link rel="stylesheet" href="css/game.css"> <link rel="stylesheet" href="css/game.css">
</head> </head>
<body onload="startGame()"> <body>
<h1>The Spooky Adventures of Pip</h1> <h1>The Spooky Adventures of Pip</h1>
<div id="game"> <div id="game-container">
<script type="text/javascript" src="js/game.js"></script> <canvas id="game"></canvas>
</div> </div>
<!--<button id="start" onclick="startGame();">Restart Game</button>-->
<img class="sprite" id="exclaimation" src="img/exclaimation.png"> <div class="sprites">
<img id="exclaimation" src="img/exclaimation.png">
<img class="sprite" id="heart" src="img/heart.png"> <img id="heart" src="img/heart.png">
<img class="sprite" id="player" src="img/pip.png"> <img id="player" src="img/pip.png">
<img class="sprite" id="down_player" src="img/pip_down.png"> <img id="down_player" src="img/pip_down.png">
<img class="sprite" id="up_player" src="img/pip_up.png"> <img id="up_player" src="img/pip_up.png">
<img class="sprite" id="right_player" src="img/pip_right.png"> <img id="right_player" src="img/pip_right.png">
<img class="sprite" id="left_player" src="img/pip_left.png"> <img id="left_player" src="img/pip_left.png">
<img class="sprite" id="ghost" src="img/ghost.png"> <img id="ghost" src="img/ghost.png">
<img class="sprite" id="down_ghost" src="img/ghost_down.png"> <img id="down_ghost" src="img/ghost_down.png">
<img class="sprite" id="up_ghost" src="img/ghost_up.png"> <img id="up_ghost" src="img/ghost_up.png">
<img class="sprite" id="right_ghost" src="img/ghost_right.png"> <img id="right_ghost" src="img/ghost_right.png">
<img class="sprite" id="left_ghost" src="img/ghost_left.png"> <img id="left_ghost" src="img/ghost_left.png">
<img class="sprite" id="right_zombie" src="img/zombie_right.png"> <img id="right_zombie" src="img/zombie_right.png">
<img class="sprite" id="left_zombie" src="img/zombie_left.png"> <img id="left_zombie" src="img/zombie_left.png">
<img class="sprite" id="mushroom01" src="img/mushroom-01.png"> <img id="mushroom01" src="img/mushroom-01.png">
</div>
<script type="module" src="js/game.js"></script>
</body> </body>
</html> </html>
+16
View File
@@ -0,0 +1,16 @@
export class AudioHandler {
constructor(game, map) {
this.game = game;
this.map = map;
}
async play(sound) {
//document.getElementById(sound).play();
let audio = new Audio(`sfx/${sound}.wav`);
audio.play();
return audio;
}
async stop(audio) {
//document.getElementById(sound).pause();
audio.pause();
}
}
+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];
}*/
}
+54 -477
View File
@@ -1,492 +1,69 @@
var myObstacles = []; // Entities
var enemies = []; import { Player } from './entities/player.js';
var myScore; import { Zombie } from './entities/zombie.js';
var ghost; import { Ghost } from './entities/ghost.js';
var pip;
var canvasWidth = 480; // Game Functions
var canvasHeight = 270; import { UI } from './ui.js';
var pWidth = 30; import { InputHandler } from './input.js';
var pHeight = 30; import { AudioHandler } from './audio.js';
import { Map } from './maps/maps.js';
var ghostS = 20; window.addEventListener('load', function() {
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
canvas.width = 480;
canvas.height = 270;
var drawHitboxes = false; class Game {
constructor(width, height) {
this.width = width;
this.height = height;
this.frame = 0;
function startGame() { this.hitboxes = false;
pip = new player(pWidth, pHeight, 0 + pWidth*2, canvasHeight - pHeight-30);
ghost = new fairy(ghostS, ghostS, 50, 50, pip);
enemies.push( new zombie(30, 30, 20, 100) );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, pip, "ghost") );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[0], "ghost") );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[1], "ghost") );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[2], "ghost") );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[3], "ghost") );
overlay = new interface(pip); this.map = new Map(this, "start");
this.player = new Player(this, this.map, 100, 0);
this.ghost = new Ghost(this, this.map, 100, 0, this.player);
this.zombie = new Zombie(this, this.map, 100, 0);
// Add sound effects and music this.ui = new UI(this, this.map);
jump = new sound("sfx/pip_jump.wav"); this.audio = new AudioHandler();
hurt = new sound("sfx/pip_hurt.wav"); this.input = new InputHandler();
heal = new sound("sfx/pip_heal.wav");
duck = new sound("sfx/pip_duck.wav");
squeak = new sound("sfx/ghost_squeak.wav");
death = new sound("sfx/death.wav");
game.start();
}
var game = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 270;
this.context = this.canvas.getContext("2d");
document.getElementById("game").insertBefore(this.canvas, document.getElementById("game").childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGame, 20);
this.keydown = function (e) {
game.keys = (game.keys || []);
game.keys[e.keyCode] = true;
// Enable Hitboxes
if (game.keys && game.keys[66]) {
if(drawHitboxes == false) {
drawHitboxes = true;
} else {
drawHitboxes = false;
}
}
};
this.keyup = function (e) {
game.keys[e.keyCode] = false;
};
window.addEventListener('keydown', this.keydown);
window.addEventListener('keyup', this.keyup);
},
end : function() {
//this.clear();
window.clearInterval(this.interval);
death.play();
let deathMessage = "YOU DIED";
ctx.font = "24px serif";
ctx.textAlign = "center";
ctx.fillStyle = 'black';
let padding = 20;
let width = ctx.measureText(deathMessage).width+padding*2;
let height = ctx.measureText(deathMessage).height;
ctx.fillRect(game.canvas.width/2-width/2, game.canvas.height/2-(24+padding/2), width, parseInt(ctx.font, padding));
ctx.fillStyle = "orange";
ctx.fillText(deathMessage, this.canvas.width/2, this.canvas.height/2);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
} }
} update() {
this.frame++;
this.player.update(this.input.keys, this.audio);
this.ghost.update(this.audio);
this.zombie.update(this.audio);
function player(width, height, x, y) { this.ui.update(this.audio);
this.hearts = 3;
this.speed = 12;
this.iframes = 80;
this.ducking = false;
this.score = 0;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 1;
this.update = function() {
ctx = game.context;
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 draw(ctx) {
// Decay iframes this.clear(ctx);
if(this.iframes > 0) { this.map.draw(ctx);
if(game.frameNo % 5) { this.ghost.draw(ctx, this.hitboxes);
ctx.globalAlpha = 0.4; this.player.draw(ctx, this.hitboxes);
} this.zombie.draw(ctx, this.hitboxes);
this.iframes -= 1;
this.ui.draw(ctx, this.player);
} }
ctx.drawImage(img, this.x, this.y, this.width, this.height); clear(ctx) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if(drawHitboxes) {
// 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.globalAlpha = 1;
}
this.move = function() {
var bottom = game.canvas.height - this.height;
// Jump
if (game.keys && game.keys[38]) {
if(pip.y >= bottom) {
this.speedY = -this.speed;
jump.play();
}
}
// Duck
if (game.keys && game.keys[40] || this.ducking) {
if(pip.y < bottom) {
pip.speedY = this.speed;
} else {
if (pip.height != pHeight / 2) {
duck.play();
}
// Ducking position
pip.height = pHeight / 2;
pip.y += pHeight / 2;
}
} else {
pip.height = pHeight;
pip.ducking = false;
}
// Right
if (game.keys && game.keys[39]) {
pip.speedX = this.speed / 2;
} else {
// Slow Down
if(pip.speedX > 0) {
pip.speedX -= this.speed/8;
}
}
// Left
if (game.keys && game.keys[37]) {
pip.speedX = 0 - this.speed / 2;
} else {
// Slow Down
if(pip.speedX < 0) {
pip.speedX += this.speed/8;
}
} }
} }
this.hitbox = function() { const game = new Game(canvas.width, canvas.height);
let paddingTop = (this.height * 0.45);
let paddingBottom = (0) - paddingTop;
let paddingLeft = (this.width * 0.00);
let paddingRight = (0) - paddingLeft*2;
let top = this.y + paddingTop; const fps = 60;
let right = this.x + this.width + paddingRight; function animate() {
let bottom = this.y + this.height + paddingBottom; game.update(ctx);
let left = this.x + paddingLeft; game.draw(ctx);
return [top, right, bottom, left];
setTimeout(() => {
requestAnimationFrame(animate);
}, 1000 / fps);
} }
animate();
this.newPos = function() { });
// Calc Y
this.speedY += this.gravity;
this.y += this.speedY;
this.hitBottom();
// Calc X
this.x += this.speedX;
}
this.hitBottom = function() {
var bottom = game.canvas.height - this.height;
if(this.y > bottom){
this.y = bottom;
this.speedY = 0;
}
}
this.hurt = function() {
// Do the hurt player things
// Check if immune
if(this.iframes > 0) {
return;
}
this.iframes = 40;
if(this.hearts > 0) {
this.hearts -= 1;
hurt.play();
return;
}
game.end();
}
this.heal = function() {
// Detect if ya hit sometin
if(this.hearts < 3) {
this.hearts += 1;
heal.play();
}
}
}
function fairy(width, height, x, y, target) {
this.speed = 4;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.target = target;
this.hiding = false;
this.scared = 0;
this.gravity = 0;
this.update = function() {
ctx = game.context;
// 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;
}
}
this.move = function() {
// if the ghost is hiding, only move after player gets a certain distance away
var 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;
squeak.play();
}
if(this.hiding) {
this.speedX = 0;
this.speedY = 0;
} else {
// Determine where the player will be and move there instead
var distX = this.speedX = this.target.x + (this.target.speedX / 100 * this.target.speed) - this.x;
var 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.newPos = function() {
// Calc Y
this.y += this.speedY;
// Calc X
this.x += this.speedX;
}
}
function zombie(width, height, x, y) {
this.hearts = 3;
this.speed = 1;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 1;
this.update = function() {
ctx = game.context;
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(drawHitboxes) {
// 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.globalAlpha = 1;
}
this.move = function() {
let bottom = game.canvas.height - this.height;
let direction = 0;
if(game.frameNo % 40 == 1) {
direction = Math.floor(Math.random() * 2)?-1:1;
} else if(this.x >= game.canvas.width-10) {
// Move Move Left
direction = -1;
} else if(this.x <= 0+10) {
// Move Move Right
direction = 1;
} else {
// Ruin the equation to keep the same state
direction = this.speedX/this.speed;
}
this.speedX = direction * this.speed;
if(this.y < bottom) {
this.speedY += this.gravity;
}
this.hitBottom();
}
this.hitBottom = function() {
var bottom = game.canvas.height - this.height;
if(this.y > bottom){
this.y = bottom;
this.speedY = 0;
}
}
this.hitbox = function() {
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];
}
this.newPos = function() {
// Calc X
this.x += this.speedX;
this.y += this.speedY;
}
}
function interface(pip) {
this.hearts = pip.hearts;
this.width = 15;
this.height = 15;
this.padding = 5;
this.x = canvasWidth;
this.y = 0;
this.update = function() {
ctx = game.context;
let img = document.getElementById("heart");
this.hearts = pip.hearts;
for(var i = 1;i<=this.hearts;i++){
let offset = i*(this.padding+this.width);
ctx.drawImage(img, this.x - offset, this.y+this.padding, this.width, this.height);
}
}
}
function updateGame() {
game.clear();
game.frameNo += 1;
//if (game.keys && game.keys[37]) {pip.speedX = -10; }
//if (game.keys && game.keys[39]) {pip.speedX = 10; }
for(var i = enemies.length-1;i>=0;i--){
enemies[i].move();
enemies[i].newPos();
enemies[i].update();
if(collision.collide(pip.hitbox(), enemies[i].hitbox()) || collision.inside(pip.hitbox(), enemies[i].hitbox())) {
pip.hurt();
}
}
ghost.move();
ghost.newPos();
pip.move();
pip.newPos();
ghost.update();
pip.update();
overlay.update();
}
function sound(src) {
this.sound = document.createElement("audio");
this.sound.src = src;
this.sound.setAttribute("preload", "auto");
this.sound.setAttribute("controls", "none");
this.sound.style.display = "none";
document.body.appendChild(this.sound);
this.play = function(){
this.sound.play();
}
this.stop = function(){
this.sound.pause();
}
}
var collision = {
// T R B L
// 0 1 2 3
collide: function (el1, el2) {
// Every case where there isn't a collision possible
return !(
el1[0] > el2[2] ||
el1[1] < el2[3] ||
el1[2] < el2[0] ||
el1[3] > el2[1]
);
},
inside: function (el1, el2) {
// Every condition to be inside of each other
return (
((el2[0] <= el1[0]) && (el1[0] <= el2[2])) &&
((el2[0] <= el1[2]) && (el1[2] <= el2[2])) &&
((el2[3] <= el1[3]) && (el1[3] <= el2[1])) &&
((el2[3] <= el1[1]) && (el1[1] <= el2[1]))
);
}
};
+26
View File
@@ -0,0 +1,26 @@
export class InputHandler {
constructor() {
this.keys = [];
window.addEventListener('keydown', e => {
if((e.key === 'ArrowDown' ||
e.key === 'ArrowUp' ||
e.key === 'ArrowLeft' ||
e.key === 'ArrowRight' ||
e.key === 'Enter'
) && this.keys.indexOf(e.key) === -1) {
this.keys.push(e.key);
}
});
window.addEventListener('keyup', e => {
if(e.key === 'ArrowDown' ||
e.key === 'ArrowUp' ||
e.key === 'ArrowLeft' ||
e.key === 'ArrowRight' ||
e.key === 'Enter') {
this.keys.splice(this.keys.indexOf(e.key), 1);
}
});
}
}
+33
View File
@@ -0,0 +1,33 @@
export class Map {
constructor(game, mapName) {
this.game = game;
this.map = mapName;
this.gravity = 1;
// x, y, w, h
this.platforms = [
{x: 150, y: 200, w: 100, h: 10},
{x: 100, y: 220, w: 100, h: 10},
{x: 0, y: game.height-10, w: game.width, h: 10}
]
}
draw(ctx) {
for(var i = 0;i < this.platforms.length;i++) {
let p = this.platforms[i];
ctx.fillStyle = 'black';
ctx.fillRect(p.x, p.y, p.w, p.h);
}
}
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;
}
}
}
return closestPlatform;
}
}
+32
View File
@@ -0,0 +1,32 @@
export class UI {
constructor(game, map) {
this.game = game;
this.map = map;
this.hearts = 3;
this.width = 15;
this.height = 15;
this.padding = 5;
this.x = this.game.width;
this.y = 0;
}
draw(ctx, entity) {
let img = document.getElementById("heart");
this.hearts = entity.hearts;
for(var i = 1;i<=this.hearts;i++){
let offset = i*(this.padding+this.width);
ctx.drawImage(img, this.x - offset, this.y+this.padding, this.width, this.height);
}
// Frame Counter
//ctx.font = "10px Arial";
//ctx.fillText(this.game.frame, 0, 10);
}
update(entity) {
this.hearts = entity.hearts;
}
}