/*:
-------------------------------------------------------------------------------
@title Battle Action Exp
@author Hime --> HimeWorks (http://himeworks.com)
@version 1.1
@date Feb 25, 2016
@filename HIME_BattleExpPopup.js
@url 

If you enjoy my work, consider supporting me on Patreon!

* https://www.patreon.com/himeworks

If you have any questions or concerns, you can contact me at any of
the following sites:

* Main Website: http://himeworks.com
* Facebook: https://www.facebook.com/himeworkscom/
* Twitter: https://twitter.com/HimeWorks
* Youtube: https://www.youtube.com/c/HimeWorks
* Tumblr: http://himeworks.tumblr.com/

-------------------------------------------------------------------------------
@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);