/*:
-------------------------------------------------------------------------------
@title Battle Action Exp
@author Hime --> HimeWorks ([url]http://himeworks.com[/url])
@version 1.1
@date Feb 25, 2016
@filename HIME_BattleExpPopup.js
@url 
 
If you enjoy my work, consider supporting me on Patreon!
 
* [url]https://www.patreon.com/himeworks[/url]
 
If you have any questions or concerns, you can contact me at any of
the following sites:
 
* Main Website: [url]http://himeworks.com[/url]
* Facebook: [url]https://www.facebook.com/himeworkscom/[/url]
* Twitter: [url]https://twitter.com/HimeWorks[/url]
* Youtube: [url]https://www.youtube.com/c/HimeWorks[/url]
* Tumblr: [url]http://himeworks.tumblr.com/[/url]
 
-------------------------------------------------------------------------------
@plugindesc v1.1 - Gain Exp after performing an action, instead of at the end
of battle.
@help 
-------------------------------------------------------------------------------
== Description ==
 
Example plugin that demonstrates how you could show an exp popup over the
battler's head.
 
Requires Battle Action Exp plugin to be installed.
 
== Terms of Use ==
 
- Free for use in non-commercial projects with credits
- Free for use in commercial projects, but it would be nice to let me know
- Please provide credits to HimeWorks
 
== Change Log ==
 
1.0 - Feb 25, 2016
 * initial release
 
== Usage ==
 
Plug and play
 
-------------------------------------------------------------------------------
 
-------------------------------------------------------------------------------
 */ 
var Imported = Imported || {} ;
var TH = TH || {};
Imported.TH_BattleExpPopup = 1;
TH.BattleExpPopup = TH.BattleExpPopup || {};
 
(function ($) {
 
  var TH_GameActionResult_clear = Game_ActionResult.prototype.clear;
  Game_ActionResult.prototype.clear = function() {
    TH_GameActionResult_clear.call(this);
    this.expPopupChecked = false;
  };
 
  var TH_SpriteBattler_initMembers = Sprite_Battler.prototype.initMembers;
  Sprite_Battler.prototype.initMembers = function() {
    TH_SpriteBattler_initMembers.call(this);
    this._expPopups = [];
  }
 
  var TH_SpriteBattler_update = Sprite_Battler.prototype.update;
  Sprite_Battler.prototype.update = function() {
    TH_SpriteBattler_update.call(this);
    if (this._battler && this._battler.isSpriteVisible()) {
      this.updateExpPopup();
    }
  }
 
  Sprite_Battler.prototype.setupExpPopup = function() {
    var result = this._battler.result();
    if (!result.expPopupChecked && result.gainedExp > 0) {
      result.expPopupChecked = true;
      var spr = new Sprite_ExpPopup();
      spr.setup(this._battler);
      spr.x = this.x + this.damageOffsetX() - 20;
      spr.y = this.y + this.damageOffsetY() - 140;
      this._expPopups.push(spr);
      this.parent.addChild(spr);
    }    
  };
 
  Sprite_Battler.prototype.updateExpPopup = function() {
    this.setupExpPopup();
    if (this._expPopups.length > 0) {
      for (var i = 0; i < this._expPopups.length; i++) {
        this._expPopups[i].update();
      }
      if (!this._expPopups[0].isPlaying()) {
        this.parent.removeChild(this._expPopups[0]);
        this._expPopups.shift();
      }
    }
  };
 
  function Sprite_ExpPopup() {
    this.initialize.apply(this, arguments);
  }
  Sprite_ExpPopup.prototype = Object.create(Sprite_Base.prototype);
  Sprite_ExpPopup.prototype.constructor = Sprite_ExpPopup;
 
  Sprite_ExpPopup.prototype.initialize = function() {
    Sprite_Base.prototype.initialize.call(this);
    this._duration = 90;
  };
 
  Sprite_ExpPopup.prototype.setup = function(battler) {
    this._battler = battler;
    this.bitmap = new Bitmap(100, 200);
    this.bitmap.textColor = "rgb(0, 255, 0)";
    this.bitmap.fontSize = 20;
    this.bitmap.drawText(battler.result().gainedExp + " exp", 0, 0, 100, 200, 'center');
  };
 
  Sprite_ExpPopup.prototype.update = function() {
    if (this._duration > 30) {
      this.y -= 0.5;
    }
    if (this._duration > 0) {
      this._duration -= 1
    }
  };
 
  Sprite_ExpPopup.prototype.isPlaying = function() {
    return this._duration > 0;
  };
})(TH.BattleExpPopup);