I'm actually doing the same thing, an inventory system. What I ended up doing is creating a new Inventory class and defining every item that I'll have in the game in its init function. I have two arrays for current possessions that the player has - a "weapons" array and an "artifacts" array. When a player picks up an artifact, for example (by touching the artifact entity with the player entity), the "addArtifact()" function below runs.
ig.module(
'game.director.inventory'
)
.requires(
'impact.impact'
)
.defines(function(){"use strict";
ig.Inventory = ig.Class.extend({
weaponsArr: new Array(),
artifactsArr: new Array(),
artifact64pxImage: new ig.Image( 'media/artifacts/64pxArtifact.png' ),
init: function(){
// FEMALE
this.coreFemaleWeapon = {name: 'Laser Pellets', damage: 1, speed: 400, entity: EntityLaserpellet};
// MALE
this.coreMaleWeapon = {name: 'Laser Orbs', damage: 3, speed: -600};
// AI
this.coreAiWeapon = {name: 'Laser Bolts', damage: 2, speed: -600};
// GENERIC
this.normalGrenade = {name: 'Grenade', damage: 5, speed: -500};
this.freezeGrenade = {name: 'Freeze Grenade', damage: 3, speed: -500, condition: 'Freezes Opponent'};
this.tractorBeam = {name: 'Tractor Beam', damage: 0, speed: 600, condition: 'Collects Artifacts'};
// ARTIFACTS
this.artifact64px = {name: 'Tiny Artifact', entity: EntityArtifact64, kind: 'artifact', image: this.artifact64pxImage};
this.allItems = [this.coreFemaleWeapon, this.coreMaleWeapon, this.coreAiWeapon, this.normalGrenade, this.freezeGrenade, this.tractorBeam, this.artifact64px];
},
setupInventory: function(kind) {
console.log('setting up inventory: ' + kind);
switch (kind) {
case 'female':
this.weaponsArr = [{item: this.coreFemaleWeapon, quantity: 999},
{item: this.normalGrenade, quantity: 3},
{item: this.freezeGrenade, quantity: 3},
{item: this.tractorBeam, quantity: 999}];
break;
}
},
addWeapon: function(item) {
for (var i = 0; i < this.allItems.length; i++) {
if (this.allItems[i].name == item.name) {
this.weaponsArr.push({item: this.allItems[i], quantity: 1});
item.kill();
console.log(this.weaponsArr);
break;
}
}
},
addArtifact: function(item) {
for (var i = 0; i < this.allItems.length; i++) {
if (this.allItems[i].name == item.name) {
this.artifactsArr.push({item: this.allItems[i], quantity: 1});
item.kill();
console.log(this.artifactsArr);
break;
}
}
}
});
});
Not sure if this is the best way of doing this and I'm just now starting to get into the upgrade system for the player character (so that they can upgrade their character based on the objects they've collected), so this may all need to change down the line...