/*:
* @plugindesc Displays your health in the upper-left corner of the screen, Legend of Zelda-style.
* @author Ben Hendel-Doying
*
* @param Actor ID
* @desc The ID of the actor whose health should be tracked. You can change this mid-game by calling $zeldaHealth.setActorId(X)
* Default: 1
* @default 1
*
* @param Full Heart Icon
* @desc Which icon (as if using \I tag) to use for a full heart in the health bar.
* Default: 1
* @default 1
*
* @param Empty Heart Icon
* @desc Which icon to use for an empty heart in the health bar.
* Default: 2
* @default 2
*
* @param Damage Sounds
* @desc A comma-separated list of sounds. A random sound from the list will be played when the player receives damage.
* Default:
* @default
*
* @param Damage Sound Volume
* @desc From 0 to 100.
* Default: 80
* @default 80
*
* @help
* Compatibility:
* * ALIASES Scene_Map..createAllWindows
* * ALIASES Game_Battler..gainHp
* * ALIASES Game_Battler..levelUp
* * ALIASES Game_Battler..levelDown
* * ALIASES Game_Actor..changeEquip
* * ALIASES Game_Actor..forceChangeEquip
*/

var BenMakesGames = window.BenMakesGames || {};
BenMakesGames.ZeldaHealth = {};
BenMakesGames.ZeldaHealth.Parameters = PluginManager.parameters('ZeldaHealth');
BenMakesGames.ZeldaHealth.ActorID = !isNaN(parseInt(BenMakesGames.ZeldaHealth.Parameters)) ? parseInt(BenMakesGames.ZeldaHealth.Parameters) : 1;
BenMakesGames.ZeldaHealth.FullHeartIcon = !isNaN(parseInt(BenMakesGames.ZeldaHealth.Parameters)) ? parseInt(BenMakesGames.ZeldaHealth.Parameters) : 1;
BenMakesGames.ZeldaHealth.EmptyHeartIcon = !isNaN(parseInt(BenMakesGames.ZeldaHealth.Parameters)) ? parseInt(BenMakesGames.ZeldaHealth.Parameters) : 2;
BenMakesGames.ZeldaHealth.DamageSounds = (BenMakesGames.ZeldaHealth.Parameters || '').split(',');
BenMakesGames.ZeldaHealth.DamageSoundVolume = !isNaN(parseInt(BenMakesGames.ZeldaHealth.Parameters)) ? parseInt(BenMakesGames.ZeldaHealth.Parameters) : 80;

if(BenMakesGames.ZeldaHealth.DamageSoundVolume < 0 || BenMakesGames.ZeldaHealth.DamageSoundVolume > 100)
BenMakesGames.ZeldaHealth.DamageSoundVolume = 80;

var $zeldaHealth;

(function() {

var actorID = BenMakesGames.ZeldaHealth.ActorID;

var ZeldaHealth_Scene_Map_createAllWindows = Scene_Map.prototype.createAllWindows;

Scene_Map.prototype.createAllWindows = function() {
this.createZeldaHealthDisplay();
ZeldaHealth_Scene_Map_createAllWindows.call(this);
};

Scene_Map.prototype.createZeldaHealthDisplay = function()
{
$zeldaHealth = new Window_ZeldaHealth(6, 6);
this._zeldaHealthWindow = $zeldaHealth;
this.addWindow(this._zeldaHealthWindow);
};

function Window_ZeldaHealth() {
this.initialize.apply(this, arguments);
this.opacity = 0;
}

Window_ZeldaHealth.prototype = Object.create(Window_Base.prototype);
Window_ZeldaHealth.prototype.constructor = Window_ZeldaHealth;

Window_ZeldaHealth.prototype.initialize = function(x, y) {
Window_Base.prototype.initialize.call(this, x, y, this.windowWidth(), this.windowHeight());
this.refresh();
};

Window_ZeldaHealth.prototype.standardPadding = function() {
return 0;
};

Window_ZeldaHealth.prototype.textPadding = function() {
return 0;
};

Window_ZeldaHealth.prototype.windowWidth = function() {
return $gameActors.actor(actorID).mhp * Window_Base._iconWidth;
};

Window_ZeldaHealth.prototype.windowHeight = function() {
return Window_Base._iconHeight;
};

Window_ZeldaHealth.prototype.refresh = function() {
this.contents.clear();

var actor = $gameActors.actor(actorID);

for(var i = 0; i < actor.mhp; i++)
{
if(i < actor.hp)
this.drawIcon(BenMakesGames.ZeldaHealth.FullHeartIcon, i * Window_Base._iconWidth, 0);
else
this.drawIcon(BenMakesGames.ZeldaHealth.EmptyHeartIcon, i * Window_Base._iconWidth, 0);
}
};

Window_ZeldaHealth.prototype.open = function() {
this.refresh();
Window_Base.prototype.open.call(this);
};

Window_ZeldaHealth.prototype.setActorId = function(id)
{
actorID = id;
this.move(this.x, this.y, this.windowWidth(), this._height);
this.createContents();
this.refresh();
};

var ZeldaHealth_Game_Battler_gainHp = Game_Battler.prototype.gainHp;

Game_Battler.prototype.gainHp = function(value) {
ZeldaHealth_Game_Battler_gainHp.call(this, value);

if(BenMakesGames.ZeldaHealth.DamageSoundVolume > 0 && BenMakesGames.ZeldaHealth.DamageSounds.length > 0) {
var sound = BenMakesGames.ZeldaHealth.DamageSounds;
console.log(sound);
AudioManager.playSe({ name: sound, volume: BenMakesGames.ZeldaHealth.DamageSoundVolume, pitch: 100 });
}

if(typeof $zeldaHealth != 'undefined')
$zeldaHealth.refresh();
};

var ZeldaHealth_Game_Battler_levelUp = Game_Battler.prototype.levelUp;
var ZeldaHealth_Game_Battler_levelDown = Game_Battler.prototype.levelDown;

Game_Battler.prototype.levelUp = function()
{
ZeldaHealth_Game_Battler_levelUp.call(this);

if(typeof $zeldaHealth != 'undefined')
$zeldaHealth.setActorId(actorID); // forces redraw & resize (in case max HP changed)
};

Game_Battler.prototype.levelDown = function()
{
ZeldaHealth_Game_Battler_levelDown.call(this);

if(typeof $zeldaHealth != 'undefined')
$zeldaHealth.setActorId(actorID); // forces redraw & resize (in case max HP changed)
};

var ZeldaHealth_Game_Actor_changeEquip = Game_Actor.prototype.changeEquip;
var ZeldaHealth_Game_Actor_forceChangeEquip = Game_Actor.prototype.forceChangeEquip;

Game_Actor.prototype.changeEquip = function(slotId, item) {
ZeldaHealth_Game_Actor_changeEquip.call(this, slotId, item);

if(typeof $zeldaHealth != 'undefined')
$zeldaHealth.setActorId(actorID); // forces redraw & resize (in case max HP changed)
};

Game_Actor.prototype.forceChangeEquip = function(slotId, item) {
ZeldaHealth_Game_Actor_forceChangeEquip.call(this, slotId, item);

if(typeof $zeldaHealth != 'undefined')
$zeldaHealth.setActorId(actorID); // forces redraw & resize (in case max HP changed)
};

})();