summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDerek Stevens <nilix@nilfm.cc>2021-02-07 11:27:11 -0500
committerDerek Stevens <nilix@nilfm.cc>2021-02-07 11:27:11 -0500
commitc07d60efd5ffebb7bfc72502d8a5132e8a3391b3 (patch)
tree9ce38a92e91974f64108ba2be6354969aeb6255b
parent7eff511ff184230419a6fd94c8aafdc8716d5df3 (diff)
Catbug, Engine, Pickup: use null instead of empty object, cleaner initialization for non-function properties
-rw-r--r--Catbug.js18
-rwxr-xr-xEngine.js20
-rw-r--r--Pickup.js20
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
+}