//=============================================================================
// O_BattleSpriteActor.js
//=============================================================================
/*:ja
 * @plugindesc 戦闘中 アクター画像表示
 * @author 
 *
 * @help 
 */
 

	
(function() {
		  
//***** Sprite_Actor の中身を　Sprite_Heroineに入れ替えて実装
	
	Spriteset_Battle.prototype.createActors = function() {
		var index=0;
		this._actorSprites = [];
		this._actorSprites[index] = new Sprite_Heroine($gameParty.members()[index]);
		this._battleField.addChild(this._actorSprites[index]);
	};
	
	//Game_Actorのみサイドビューをオンにしとく
	Game_Actor.prototype.isSpriteVisible = function() {
		return true;
	};

	Game_Actor.prototype.performAction = function(action) {
		Game_Battler.prototype.performAction.call(this, action);
		if (action.isAttack()) {
			this.performAttack();
		} else if (action.isGuard()) {
			this.requestMotion('guard');
			
		} else if (action.isMagicSkill()) {
			//ここでスキル選別している
			switch(action.item().id){
			case 21://目眩まし
			case 22://魔法封じ
				this.requestMotion('spell');
				break;
				
			case 23://アーマーブレイク
			case 24://ソードブレイク
				this.requestMotion('swing');
				break;
				
			case 25://攻撃の構え
				this.requestMotion('wait');
				break;
				
			case 26://防御の構え
				this.requestMotion('guard');
				break;
				
			case 30://熊殺し
			case 31://渾身斬り
			case 32://全力斬り
				this.requestMotion('swing');
				break;
				
			case 37://瞑想
			case 38://心頭滅却
				this.requestMotion('spell');
				break;
				
			default:
				this.requestMotion('spell');
				break;
			}
		} else if (action.isSkill()) {
			this.requestMotion('skill');
		} else if (action.isItem()) {
			this.requestMotion('item');
		}
	};
	
	Game_Actor.prototype.isChanting = function() {
		if (this.isWaiting()) {
			return this._actions.some(function(action) {
				switch(action.item().id){
				case 21://目眩まし
				case 22://魔法封じ
				case 37://瞑想
				case 38://心頭滅却
					return true;
				default:
					return false;
				}
			});
		}
		
		//※※目眩ましと魔法封じは詠唱扱いにならない
		//actor._actionState の中身が waitingになっていないため
		
		return false;
	};
	

//-----------------------------------------------------------------------------
// Sprite_Heroine
//
// ヒロインの動きを表示する

	function Sprite_Heroine() {
		this.initialize.apply(this, arguments);
	}

	Sprite_Heroine.prototype = Object.create(Sprite_Battler.prototype);
	Sprite_Heroine.prototype.constructor = Sprite_Heroine;

//****** loop  0:1連表示 , 1:ループ表示, 2:１マス目のみ表示
	Sprite_Heroine.MOTIONS = {
		walk:     { index: 0,  loop: 2 },
		wait:     { index: 0,  loop: 2 },
		chant:    { index: 2,  loop: 2 },
		guard:    { index: 3,  loop: 2 },
		damage:   { index: 4,  loop: 2 },
		evade:    { index: 5,  loop: 2 },
		thrust:   { index: 7,  loop: 0 },
		swing:    { index: 7,  loop: 0 },
		missile:  { index: 8,  loop: 0 },
		skill:    { index: 9,  loop: 2 },
		spell:    { index: 10, loop: 2 },
		item:	  { index: 11, loop: 0 },
		escape:   { index: 12, loop: 2 },
		victory:  { index: 13, loop: 1 },
		dying:    { index: 14, loop: 2 },
		abnormal: { index: 15, loop: 2 },
		sleep:    { index: 16, loop: 2 },
		dead:     { index: 17, loop: 2 }
	};

	Sprite_Heroine.prototype.initialize = function(battler) {
		this._imgWidth=400;
		Sprite_Battler.prototype.initialize.call(this, battler);
		
		
		//this.moveToStartPosition();
	};

	Sprite_Heroine.prototype.initMembers = function() {
		Sprite_Battler.prototype.initMembers.call(this);
		this._motion = null;
		this._motionCount = 0;
		this._pattern = 0;
		//this.createShadowSprite();
		//this.createWeaponSprite();
		this.createMainSprite();
		this.createStateSprite();
	};

	Sprite_Heroine.prototype.createMainSprite = function() {
		this._mainSprite = new Sprite_Base();
		this._mainSprite.anchor.x = 0.5;
		this._mainSprite.anchor.y = 1;
		this._mainSprite.bitmap = ImageManager.loadSystem('main_battle');
		this.addChild(this._mainSprite);
		this._effectTarget = this._mainSprite;
	};
	


	Sprite_Heroine.prototype.createStateSprite = function() {
		this._stateSprite = new Sprite_StateOverlay();
		this.addChild(this._stateSprite);
	};

	Sprite_Heroine.prototype.setBattler = function(battler) {
		Sprite_Battler.prototype.setBattler.call(this, battler);
		if (battler !== this._actor) {
			this._actor = battler;

			if (battler) {
				this.setActorHome();
			}
			this.startEntryMotion();
			this._stateSprite.setup(battler);
		}
	};

	Sprite_Heroine.prototype.moveToStartPosition = function() {
		this.startMove(0, 0, 0);
	};
	
	//****表示位置
	Sprite_Heroine.prototype.setActorHome = function() {
		this.setHome(Graphics.boxWidth-(this._imgWidth)+130, Graphics.boxHeight-80);
	};

	Sprite_Heroine.prototype.update = function() {
		Sprite_Battler.prototype.update.call(this);
		//this.updateShadow();
		if (this._actor) {
			this.updateMotion();
		}
	};
	
	/*
	Sprite_Heroine.prototype.updateShadow = function() {
		this._shadowSprite.visible = !!this._actor;
	};*/
	
	Sprite_Heroine.prototype.updateMain = function() {
		Sprite_Battler.prototype.updateMain.call(this);
		if (this._actor.isSpriteVisible() && !this.isMoving()) {
			this.updateTargetPosition();
		}
	};
	
	Sprite_Heroine.prototype.setupMotion = function() {
		if (this._actor.isMotionRequested()) {
			this.startMotion(this._actor.motionType());
			this._actor.clearMotion();
		}
	};
	/*
	Sprite_Heroine.prototype.setupWeaponAnimation = function() {
		if (this._actor.isWeaponAnimationRequested()) {
			this._weaponSprite.setup(this._actor.weaponImageId());
			this._actor.clearWeaponAnimation();
		}
	};*/
	
	Sprite_Heroine.prototype.startMotion = function(motionType) {
		var newMotion = Sprite_Heroine.MOTIONS[motionType];
		if (this._motion !== newMotion) {
			this._motion = newMotion;
			this._motionCount = 0;
			this._pattern = 0;
		}
	};
	
	//基本的にキャラの位置は変更しない？
	Sprite_Heroine.prototype.updateTargetPosition = function() {
		if (this._actor.isInputting() || this._actor.isActing()) {
			this.stepForward();
		} else if (this._actor.canMove() && BattleManager.isEscaped()) {
			this.retreat();
		} else if (!this.inHomePosition()) {
			this.stepBack();
		}
	};
	
	
	Sprite_Heroine.prototype.updateFrame = function() {
		Sprite_Battler.prototype.updateFrame.call(this);
		var bitmap = this._mainSprite.bitmap;
		if (bitmap) {
			var motionIndex = this._motion ? this._motion.index : 0;
			var pattern = this._pattern < 3 ? this._pattern : 1;
			var cw = bitmap.width / 9;
			var ch = bitmap.height / 6;
			var cx = Math.floor(motionIndex / 6) * 3 + pattern;
			var cy = motionIndex % 6;
			this._mainSprite.setFrame(cx * cw, cy * ch, cw, ch);
		}
	};
	
	Sprite_Heroine.prototype.updateMove = function() {
		var bitmap = this._mainSprite.bitmap;
		if (!bitmap || bitmap.isReady()) {
			Sprite_Battler.prototype.updateMove.call(this);
		}
	};
	
	Sprite_Heroine.prototype.updateMotion = function() {
		this.setupMotion();
		//this.setupWeaponAnimation();
		if (this._actor.isMotionRefreshRequested()) {
			this.refreshMotion();
			this._actor.clearMotion();
		}
		this.updateMotionCount();
	};
	
	Sprite_Heroine.prototype.updateMotionCount = function() {
		if (this._motion && ++this._motionCount >= this.motionSpeed()) {
			if (this._motion.loop==1) {
				this._pattern = (this._pattern + 1) % 4;
			} else if (this._motion.loop==0 && this._pattern < 2) {
				this._pattern++;
			} else if (this._motion.loop==2) {
			   
			} else {
				this.refreshMotion();
			}
			this._motionCount = 0;
		}
	};
	
	Sprite_Heroine.prototype.motionSpeed = function() {
		return 12;
	};
	
	Sprite_Heroine.prototype.refreshMotion = function() {
		var actor = this._actor;
		var motionGuard = Sprite_Heroine.MOTIONS['guard'];
		if (actor) {
			if (this._motion === motionGuard && !BattleManager.isInputting()) {
					return;
			}
			var stateMotion = actor.stateMotionIndex();
			if (actor.isInputting() || actor.isActing()) {
				//this.startMotion('walk');//前進・後退
				this.startMotion('wait');//移動なし
			} else if (stateMotion === 3) {
				this.startMotion('dead');//死亡
			} else if (stateMotion === 2) {
				this.startMotion('sleep');//睡眠
				
			} else if (actor.isChanting()) {
				
				this.startMotion('chant');//詠唱
				
			} else if (actor.isGuard() || actor.isGuardWaiting()) {
				this.startMotion('guard');//防御
			} else if (stateMotion === 1) {
				this.startMotion('abnormal');//瀕死
			} else if (actor.isDying()) {
				this.startMotion('dying');
			} else if (actor.isUndecided()) {//未定
				this.startMotion('walk');
			} else {
				this.startMotion('wait');
			}
		}
	};
	
	Sprite_Heroine.prototype.startEntryMotion = function() {
		/*
		if (this._actor && this._actor.canMove()) {
			this.startMotion('walk');
			this.startMove(0, 0, 0);
		} else if (!this.isMoving()) {
			this.refreshMotion();
			this.startMove(0, 0, 0);
		}
		*/
		
	};
	//***** 前進
	Sprite_Heroine.prototype.stepForward = function() {
		//this.startMove(0, 0, 0);
	};
	//***** 後退
	Sprite_Heroine.prototype.stepBack = function() {
		//this.startMove(0, 0, 0);
	};
	//***** 逃亡
	Sprite_Heroine.prototype.retreat = function() {
		this.startMove(this._imgWidth * 2, 0, 60);
	};
	
	
	
	Sprite_Heroine.prototype.onMoveEnd = function() {
		Sprite_Battler.prototype.onMoveEnd.call(this);
		if (!BattleManager.isBattleEnd()) {
			this.refreshMotion();
		}
	};
	
	//ダメージテキストの表示位置
	Sprite_Heroine.prototype.damageOffsetX = function() {
		return 80;//-32;
	};
	Sprite_Heroine.prototype.damageOffsetY = function() {
		return -50;
	};
	
})();
	
	/*
		var _BattleManager_startBattle=BattleManager.startBattle;
		BattleManager.startBattle = function() {
			
			//viewActorPicInBattle();
			_BattleManager_startBattle.call(this);
		
		}
		
		
		ImageManager.loadBattleActor = function(filename, hue) {
			return this.loadBitmap('img/battle_actor/', filename, hue, true);
		};
		
		function viewActorPicInBattle(){
			var tmp_img;
			//tmp_img=ImageManager.loadBattleActor("main_01");
			
			//$gameScreen.showPicture(150,"battle_main_02",0,550,-50,100,100,255,0);
		};
	*/