From c07d60efd5ffebb7bfc72502d8a5132e8a3391b3 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Sun, 7 Feb 2021 11:27:11 -0500 Subject: [PATCH] Catbug, Engine, Pickup: use null instead of empty object, cleaner initialization for non-function properties --- Catbug.js | 18 ++++++++++-------- Engine.js | 20 +++----------------- Pickup.js | 20 +++++++++++--------- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/Catbug.js b/Catbug.js index 0ff3a6d..96cabf7 100644 --- a/Catbug.js +++ b/Catbug.js @@ -1,11 +1,13 @@ -var catbug = {}; -catbug.x = 45; -catbug.y = 90; -catbug.vX = 0; -catbug.vY = 0; -catbug.frame = 0; -catbug.HP = 3; -catbug.maxHP = 3; +var catbug = { + x: 45, + y: 90, + vX: 0, + vY: 0, + frame: 0, + HP: 3, + maxHP: 3 +}; + catbug.move = function() { this.x += this.vX; diff --git a/Engine.js b/Engine.js index 4694c57..884aaca 100755 --- a/Engine.js +++ b/Engine.js @@ -100,7 +100,7 @@ renderer.drawPickups = function() var i; for (i = 0; i < 10; i++) { - if (!isEmptyObject(stuff[i])){ + if (stuff[i] != null){ screen.drawImage(stuff[i].sprite, stuff[i].x, stuff[i].y);} } } @@ -136,20 +136,6 @@ controller.left = false; controller.space = false; controller.q = false; -function isEmptyObject(obj) -{ - var name; - for (name in obj) - { - if (obj.hasOwnProperty(name)) - { - return false; - } - } - return true; -} - - function scoreSummary() { gameState.frame = thisFrame; @@ -191,7 +177,7 @@ function resetGame() catbug.vY = 0; gameState.points = 0; gameState.threshold = 10; - stuff = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]; + stuff = new Array(10); } function loop() @@ -346,4 +332,4 @@ function releaseHandler(e) default: break; } -} \ No newline at end of file +} diff --git a/Pickup.js b/Pickup.js index fd17ad6..66ba0b0 100644 --- a/Pickup.js +++ b/Pickup.js @@ -43,25 +43,27 @@ function managePickups() else genSpeed = Math.floor(Math.random()*10); for (i = 0; i < 10; i++) { - if (!isEmptyObject(stuff[i])) + if (stuff[i] != null) { movePickup(stuff[i]); if (stuff[i].x < -32) { - stuff[i] = {}; + stuff[i] = null; catbug.HP--; } else { - var box = {}; - box.x = stuff[i].x; - box.y = stuff[i].y; - box.w = 36; - box.h = 36; + box = { + x: stuff[i].x, + y: stuff[i].y, + w: 36, + h: 36 + } + if (catbug.isInRect(box)) { getPickup(stuff[i]); - stuff[i] = {}; + stuff[i] = null; } } } @@ -96,4 +98,4 @@ function getPickup(self) catbug.HP = catbug.maxHP; } -} \ No newline at end of file +}