Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

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:

    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

1 decade ago by Joncom

Was just curious about why you have...
if(this) { // <--- This line here.
    this.pos.x = ig.game.screen.x + 15;
    this.pos.y = ig.game.screen.y + 35;
} // <--- With closing bracket.

Seems redundant because if those lines are being run, it would be impossible for this to return any value other than true.

EDIT Yes, it is probably a good idea to move your itemList out of main.

1 decade ago by alexandre

Also does anyone recommend moving the itemList out of main and into inventory.js?


If it's a "smart" inventory, i.e., has methods for management, stacking of similar items, and requires non-trivial display and updates, I'd say yes.

1 decade ago by paulh

Yep move it! Probably doesnt need the overhead of being an entity also as i doesnt move or collide?

I'd like to know how you change that forman entity to a class though!
Page 1 of 1
« first « previous next › last »