{"size": 2305, "ext": "js", "max_stars_repo_path": "js/entities/HUD.js", "max_stars_repo_name": "Paulloz/gbjam", "max_stars_repo_stars_event_min_datetime": "2018-11-30T16:32:12.000Z", "max_stars_repo_stars_event_max_datetime": "2020-11-19T15:15:58.000Z", "max_issues_repo_path": "js/entities/HUD.js", "max_issues_repo_name": "Paulloz/gbjam", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "js/entities/HUD.js", "max_forks_repo_name": "Paulloz/gbjam", "max_forks_repo_forks_event_min_datetime": "2018-11-30T16:32:14.000Z", "max_forks_repo_forks_event_max_datetime": "2018-11-30T16:32:14.000Z"}
{"max_stars_count": 6, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 23.7628865979, "max_line_length": 104, "alphanum_fraction": 0.5457700651}
/**
 * a HUD container and child items
 */

game.HUD = game.HUD || {};
game.HUD.Container = me.Container.extend({

    /**
     * constructor
     */
    init: function() {
        // call the constructor
        this._super(me.Container, 'init');
        this.name = 'HUD';

        // persistent across level change
        this.isPersistent = true;

        // make sure we use screen coordinates
        this.floating = true;

        // make sure our object always draws first
        this.z = 90;

        // add all piglets
        this.maxPiglets = me.game.world.getChildByName("piglet").length;
        this.piglets = this.maxPiglets;

        for (var i = this.maxPiglets - 1; i >= 0; --i) {
          this.addChild(new game.HUD.PigletItem(1 + ((11 + 1) * i) , 1));
        }
    },

    /**
     *  When a piglet is picked up, call this function to remove
     *  it from the HUD and count it in the score
     */
    pigletRescued: function() {
        // remove a piglet
        --this.piglets;

        // if enough piglets
        if (this.piglets >= 0) {
          // remove a piglet from the screen
          this.getChildAt(this.piglets).rescue();
        }

        // if no more piglets
        if (this.piglets <= 0) {
            // A WINNER IS YOU
            me.state.current().win();
        }
    }
});


/**
* a piglet hud indicator telling the number of piglets still alive
*/
game.HUD.PigletItem = me.AnimationSheet.extend({

    /**
    * constructor
    */
    init: function(x, y) {
        // call the parent constructor
        this._super(me.AnimationSheet, 'init', [x, y, {image: "HUD", framewidth: 11, frameheight: 11}]);

        // animations
        this.addAnimation('notrescued', [0], 100);
        this.addAnimation('rescued', [1], 100);

        // kidnap the piglet
        this.kidnap();
    },

    /**
    * update function
    */
    update : function (dt) {
        // we don't do anything for the moment here
        //TODO : disable rendering when saved
        return false;
    },

    rescue: function() {
        if(!this.isCurrentAnimation('rescued'))
            this.setCurrentAnimation('rescued');
    },

    kidnap: function() {
        if(!this.isCurrentAnimation('notrescued'))
            this.setCurrentAnimation('notrescued');
    }

});
