Added some more assets, started working on fixing hitboxes

This commit is contained in:
rain
2023-11-14 18:07:00 -06:00
parent 0514198c00
commit c28a0cb87f
8 changed files with 22 additions and 5 deletions
Binary file not shown.

Before

Width:  |  Height:  |  Size: 724 B

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 837 B

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.8 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.7 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

+2
View File
@@ -31,5 +31,7 @@
<img class="sprite" id="right_zombie" src="img/zombie_right.png"> <img class="sprite" id="right_zombie" src="img/zombie_right.png">
<img class="sprite" id="left_zombie" src="img/zombie_left.png"> <img class="sprite" id="left_zombie" src="img/zombie_left.png">
<img class="sprite" id="mushroom01" src="img/mushroom-01.png">
</body> </body>
</html> </html>
+20 -5
View File
@@ -11,12 +11,12 @@ var pHeight = 30;
var ghostS = 20; var ghostS = 20;
var drawHitboxes = true; var drawHitboxes = false;
function startGame() { function startGame() {
pip = new player(pWidth, pHeight, 0 + pWidth*2, canvasHeight - pHeight-30); pip = new player(pWidth, pHeight, 0 + pWidth*2, canvasHeight - pHeight-30);
ghost = new fairy(ghostS, ghostS, 50, 50, pip); ghost = new fairy(ghostS, ghostS, 50, 50, pip);
enemies.push( new zombie(30, 30, 100, 100) ); 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, pip, "ghost") );
//enemies.push( new enemy(ghostS, ghostS, 50, 50, enemies[0], "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[1], "ghost") );
@@ -50,6 +50,15 @@ var game = {
this.keydown = function (e) { this.keydown = function (e) {
game.keys = (game.keys || []); game.keys = (game.keys || []);
game.keys[e.keyCode] = true; 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) { this.keyup = function (e) {
game.keys[e.keyCode] = false; game.keys[e.keyCode] = false;
@@ -115,16 +124,16 @@ function player(width, height, x, y) {
} }
this.iframes -= 1; this.iframes -= 1;
} }
ctx.drawImage(img, this.x, this.y, this.width, this.height);
if(drawHitboxes) { if(drawHitboxes) {
// T R B L // T R B L
// 0 1 2 3 // 0 1 2 3
let hitbox = this.hitbox(); let hitbox = this.hitbox();
ctx.fillStyle = 'blue'; ctx.fillStyle = 'blue';
ctx.globalAlpha = 0.4;
ctx.fillRect(hitbox[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y); ctx.fillRect(hitbox[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y);
} }
ctx.drawImage(img, this.x, this.y, this.width, this.height);
ctx.globalAlpha = 1; ctx.globalAlpha = 1;
} }
@@ -326,17 +335,19 @@ function zombie(width, height, x, y) {
} else { } else {
img = document.getElementById(`right_zombie`); img = document.getElementById(`right_zombie`);
} }
ctx.drawImage(img, this.x, this.y, this.width, this.height);
if(drawHitboxes) { if(drawHitboxes) {
// T R B L // T R B L
// 0 1 2 3 // 0 1 2 3
ctx.globalAlpha = 0.4;
let hitbox = this.hitbox(); let hitbox = this.hitbox();
ctx.fillStyle = 'green'; ctx.fillStyle = 'green';
ctx.fillRect(hitbox[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y); ctx.fillRect(hitbox[3], hitbox[0], hitbox[1]-this.x, hitbox[2]-this.y);
} }
ctx.globalAlpha = 1;
ctx.drawImage(img, this.x, this.y, this.width, this.height);
} }
this.move = function() { this.move = function() {
@@ -457,7 +468,10 @@ function sound(src) {
} }
var collision = { var collision = {
// T R B L
// 0 1 2 3
collide: function (el1, el2) { collide: function (el1, el2) {
// Every case where there isn't a collision possible
return !( return !(
el1[0] > el2[2] || el1[0] > el2[2] ||
el1[1] < el2[3] || el1[1] < el2[3] ||
@@ -467,6 +481,7 @@ var collision = {
}, },
inside: function (el1, el2) { inside: function (el1, el2) {
// Every condition to be inside of each other
return ( return (
((el2[0] <= el1[0]) && (el1[0] <= el2[2])) && ((el2[0] <= el1[0]) && (el1[0] <= el2[2])) &&
((el2[0] <= el1[2]) && (el1[2] <= el2[2])) && ((el2[0] <= el1[2]) && (el1[2] <= el2[2])) &&