1 decade ago by Nico
So I'm working on a game that will have quite a few "items" that the player can hold in an inventory and use to do certain things. After a bit of experimentation and a couple of rewrites I've come up with this:
I'm storing my items in main.js like so:
The for loop iterates through my items perfectly and wraps it into my inventory grid accordingly.
All I'm wondering is whether or not any of the infinitely more experienced devs here have any suggestions to do this more efficiently.
Also does anyone recommend moving the itemList out of main and into inventory.js?
Thanks,
-N
EntityInventory = ig.Entity.extend({ size: {x: 120, y: 180}, maxVel: {x: 0, y: 0}, type: ig.Entity.TYPE.get('inventory'), collides: ig.Entity.COLLIDES.NEVER, animSheet: new ig.AnimationSheet( 'media/inventory.png', 120, 180 ), invText: new ig.Font( 'media/04b03.font.png' ), playerImg: new ig.Image( 'media/invplayer.png' ), init: function( x, y, settings ) { this.parent( x, y , settings ); this.addAnim( 'idle', 0.2, [0] ); }, update: function() { this.moveToFront(); if(this) { this.pos.x = ig.game.screen.x + 15; this.pos.y = ig.game.screen.y + 35; } this.parent(); }, draw: function() { this.parent(); var ix = this.size.x; var iy = 117; ix = Math.floor((ix)/5); if(ig.game.inventoryWindows == 1) { this.playerImg.draw((ix * 3) - 6, 60); var itemList = ig.game.itemList; var i = 1; for( item in itemList ) { if( itemList[item].count > 0 ) { itemList[item].img.draw(ix * i, iy - 10); this.invText.draw(itemList[item].count, ix * i, iy); i++; if( i > 5){ i = 1; iy = iy + 22; } } } } }, kill: function(){ this.parent(); } });
I'm storing my items in main.js like so:
itemList: { bullets: {count: 25, img: new ig.Image('media/weapon-gun.png')}, grenades: {count: 3, img: new ig.Image('media/weapon-grenade.png')}, materials: {count: 0, img: new ig.Image('media/bcollect.png')}, }
The for loop iterates through my items perfectly and wraps it into my inventory grid accordingly.
All I'm wondering is whether or not any of the infinitely more experienced devs here have any suggestions to do this more efficiently.
Also does anyone recommend moving the itemList out of main and into inventory.js?
Thanks,
-N