Catbug, Engine, Pickup: use null instead of empty object, cleaner initialization for non-function properties

This commit is contained in:
Iris Lightshard 2021-02-07 11:27:11 -05:00
parent 7eff511ff1
commit c07d60efd5
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
3 changed files with 24 additions and 34 deletions

View file

@ -1,11 +1,13 @@
var catbug = {}; var catbug = {
catbug.x = 45; x: 45,
catbug.y = 90; y: 90,
catbug.vX = 0; vX: 0,
catbug.vY = 0; vY: 0,
catbug.frame = 0; frame: 0,
catbug.HP = 3; HP: 3,
catbug.maxHP = 3; maxHP: 3
};
catbug.move = function() catbug.move = function()
{ {
this.x += this.vX; this.x += this.vX;

View file

@ -100,7 +100,7 @@ renderer.drawPickups = function()
var i; var i;
for (i = 0; i < 10; 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);} screen.drawImage(stuff[i].sprite, stuff[i].x, stuff[i].y);}
} }
} }
@ -136,20 +136,6 @@ controller.left = false;
controller.space = false; controller.space = false;
controller.q = false; controller.q = false;
function isEmptyObject(obj)
{
var name;
for (name in obj)
{
if (obj.hasOwnProperty(name))
{
return false;
}
}
return true;
}
function scoreSummary() function scoreSummary()
{ {
gameState.frame = thisFrame; gameState.frame = thisFrame;
@ -191,7 +177,7 @@ function resetGame()
catbug.vY = 0; catbug.vY = 0;
gameState.points = 0; gameState.points = 0;
gameState.threshold = 10; gameState.threshold = 10;
stuff = [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]; stuff = new Array(10);
} }
function loop() function loop()
@ -346,4 +332,4 @@ function releaseHandler(e)
default: default:
break; break;
} }
} }

View file

@ -43,25 +43,27 @@ function managePickups()
else genSpeed = Math.floor(Math.random()*10); else genSpeed = Math.floor(Math.random()*10);
for (i = 0; i < 10; i++) for (i = 0; i < 10; i++)
{ {
if (!isEmptyObject(stuff[i])) if (stuff[i] != null)
{ {
movePickup(stuff[i]); movePickup(stuff[i]);
if (stuff[i].x < -32) if (stuff[i].x < -32)
{ {
stuff[i] = {}; stuff[i] = null;
catbug.HP--; catbug.HP--;
} }
else else
{ {
var box = {}; box = {
box.x = stuff[i].x; x: stuff[i].x,
box.y = stuff[i].y; y: stuff[i].y,
box.w = 36; w: 36,
box.h = 36; h: 36
}
if (catbug.isInRect(box)) if (catbug.isInRect(box))
{ {
getPickup(stuff[i]); getPickup(stuff[i]);
stuff[i] = {}; stuff[i] = null;
} }
} }
} }
@ -96,4 +98,4 @@ function getPickup(self)
catbug.HP = catbug.maxHP; catbug.HP = catbug.maxHP;
} }
} }