//=============================================================================
// Character Still Pose
// by ProGM
// Last Updated: 2015.09.21
//=============================================================================

/*:
 * @plugindesc Allows more than 3 Frames
 * @author Shaz
 *
 * @help This plugin does not provide plugin commands.
 *
 * Add [D L R U] to your character sheet name (with $ prefix) to specify haw
 * many frames in each direction (Down, Left, Right, Up).
 * Spritesheets with this added to the file name will use a looping frame
 * animation rather than the back-and-forth default animation.
 *
 * The first frame should be the idle/still pose.
 *
 * eg. $Ralph [8 8 8 8].png
 *     is a character sheet consisting of 4 rows with 8 frames per row.
 *     Animation will go from frame 1 to 8 then start at 1 again.
 */

(function() {

  ImageManager.hasStillPose = function(filename) {
    var stillPose = filename.match(/\$$/);
    return stillPose;
  };

  var _Game_CharacterBase_initMembers = Game_CharacterBase.prototype.initMembers;
  Game_CharacterBase.initMembers = function() {
    _Game_CharacterBase_initMembers.call(this);
    this._hasStillPose = false;
  };

  var _Game_CharacterBase_updatePattern = Game_CharacterBase.prototype.updatePattern;
  Game_CharacterBase.prototype.updatePattern = function() {
    if (!this.hasStepAnime() && this._stopCount > 0) {
      this.resetPattern();
    } else {
      _Game_CharacterBase_updatePattern.call(this);
      this._pattern = (this._hasStillPose && this._pattern === 0) ? 1 : this._pattern;
    }
  };

  // var _Game_CharacterBase_pattern = Game_CharacterBase.prototype.pattern;
  // Game_CharacterBase.prototype.pattern = function() {
  //   if (!this._isMultiFrame) {
  //     return _Game_CharacterBase_pattern.call(this);
  //   } else if (this._hasStillPose) {
  //     return this._pattern < this._frames[this._direction / 2 - 1] ? this._pattern : 1;
  //   } else {
  //     return this._pattern < this._frames[this._direction / 2 - 1] ? this._pattern : 0;
  //   }
  // };

  var _Game_CharacterBase_isOriginalPattern = Game_CharacterBase.prototype.isOriginalPattern;
  Game_CharacterBase.prototype.isOriginalPattern = function() {
    if (!this._isMultiFrame) {
      return _Game_CharacterBase_isOriginalPattern.call(this);
    } else {
      return this.pattern() === 0;
    }
  };

  var _Game_CharacterBase_resetPattern = Game_CharacterBase.prototype.resetPattern;
  Game_CharacterBase.prototype.resetPattern = function() {
    if (!this._hasStillPose) {
      _Game_CharacterBase_resetPattern.call(this);
    } else {
      this.setPattern(0);
    }
  };

  var _Game_CharacterBase_setImage = Game_CharacterBase.prototype.setImage;
  Game_CharacterBase.prototype.setImage = function(characterName, characterIndex) {
    _Game_CharacterBase_setImage.call(this, characterName, characterIndex);
    this._hasStillPose = ImageManager.hasStillPose(characterName);
  };

  var _Game_CharacterBase_setTileImage = Game_CharacterBase.prototype.setTileImage;
  Game_CharacterBase.prototype.setTileImage = function(tileId) {
    _Game_CharacterBase_setTileImage.call(this, tileId);
    this._hasStillPose = false;
  };

  Game_CharacterBase.prototype.hasStillPose = function() {
    return this._hasStillPose;
  };

})();