//=============================================================================
// O_BattleViewEnamyStatus.js
//=============================================================================

/*:ja
 * @plugindesc 戦闘中 ヒロインHP・MPステータス表示
 * @author 
 *
 * @help 
 */
 
(function() {
	
//***** ステータスウィンドウを　Window_BattleStatusEX と差し替え
	Scene_Battle.prototype.createStatusWindow = function() {
		this._statusWindow = new Window_BattleStatusEX();
		this.addWindow(this._statusWindow);
	};

//*****Window_BattleStatusEX
//*****  Window_BattleStatus の拡張クラス
	function Window_BattleStatusEX() {
		this.initialize.apply(this, arguments);
	}

	Window_BattleStatusEX.prototype = Object.create(Window_Base.prototype);
	Window_BattleStatusEX.prototype.constructor = Window_BattleStatusEX;
	
	Window_BattleStatusEX.prototype.initialize = function() {
		Window_Base.prototype.initialize.call(this, 0, 0, 1, 1);
		
		this._fadeTime=20;
		
		this._requestRefresh = false;
		//this.contentsOpacity = 0;
		this.opacity = 0;//window枠 非表示
		this._actor = $gameParty.members()[0];
		
		this._img_gauge_base = ImageManager.loadSystem('sys_gauge_actor_bace');
		this._img_gauge_HP = ImageManager.loadSystem('sys_gauge_actor_hp_blue');
		this._img_gauge_HP_red = ImageManager.loadSystem('sys_gauge_actor_hp_red');
		
		//this._img_gauge_MP = ImageManager.loadSystem('sys_gauge_actor_mp');
		
		this._img_gauge_base_TP  = ImageManager.loadSystem('sys_gauge_actor_bace_TP');
		this._img_gauge_TP		  = ImageManager.loadSystem('sys_gauge_actor_tp');
		
		this._gauge_padding_left=28;
		this._gauge_padding_top=5;
		this._gauge_padding_bottom=28;
		
		this._MPgauge_padding_top=35;
		
		this._hp=this._view_hp=this._actor.hp;
		this._mp=this._view_mp=this._actor.mp;
		this._tp=this._view_tp=this._actor._tp;
		this._mhp=this._actor.mhp;
		this._mmp=this._actor.mmp;
		this._mtp=100;
		
		this._status=this.getStatusStr();//状態変更チェック用
		this._name_line_height=32;// =アイコンの高さ
		
		
		
	};
	
	Window_BattleStatusEX.prototype.update = function() {
		Window_Base.prototype.update.call(this);
		

		this.setWindowSize();
		this.setViewHp();
		//this.setViewMp();//MPの描画更新
		this.setViewTp();
		this.checkStatus();

			
		if(this._flg_redraw){
			this._flg_redraw=false;
			this.contents.clear();
			
			this.drawNameAndStatus();
			this.drawGauge();
			
		}
	};
	
	Window_BattleStatusEX.prototype.setWindowSize = function() {
		var width  = this._img_gauge_base.width+this._gauge_padding_left+50;
		var height = this._name_line_height+70+this._gauge_padding_top+this._gauge_padding_bottom;
		if(width != this.width || height != this.height){
			this.x=Graphics.boxWidth-width;
			this.y=Graphics.boxHeight-height;
			this.width = width;
			this.height = height;
			this.createContents();
			this._flg_redraw = true;
		}
	};
	
	//表示中HPの値を更新 現在HPまでループ
	Window_BattleStatusEX.prototype.setViewHp = function() {
		if(this._hp != this._actor.hp){
			this.diff_hp= (this._hp-this._actor.hp)/this._fadeTime;//１回の増減値
			this.loop_limit_hp=this._fadeTime+1;
			this._hp = this._actor.hp;
		}
		
		if(this._hp!=this._view_hp){
			this._view_hp-=this.diff_hp;
			this._flg_redraw = true;
			this.loop_limit_hp--;
			if(this.diff_hp>=0){//減算処理
				if(this._hp>this._view_hp){
					this.loop_limit_hp=0;
				}
			}else{//加算処理
				if(this._hp<this._view_hp){
					this.loop_limit_hp=0;
				}
			}
		}
		
		//一定回数以上はループしない
		if(this.loop_limit_hp==0 && this._flg_redraw){
			this._hp=this._view_hp=this._actor.hp;
		}
	};
	
	//表示中MPの値を更新 現在MPまでループ
	Window_BattleStatusEX.prototype.setViewMp = function() {
		if(this._mp != this._actor.mp){
			this.diff_mp= (this._mp-this._actor.mp)/this._fadeTime;//１回の増減値
			this.loop_limit_mp=this._fadeTime+1;
			this._mp = this._actor.mp;
			
		}
		
		if(this._mp!=this._view_mp){
			this._view_mp-=this.diff_mp;
			this._flg_redraw = true;
			this.loop_limit_mp--;
			if(this.diff_mp>=0){//減算処理
				if(this._mp>this._view_mp){
					this.loop_limit_mp=0;
				}
			}else{//加算処理
				if(this._mp<this._view_mp){
					this.loop_limit_mp=0;
				}
			}
		}
		
		//一定回数以上はループしない
		if(this.loop_limit_mp==0 && this._flg_redraw){
			this._mp=this._view_mp=this._actor.mp;
		}
	};
	
	Window_BattleStatusEX.prototype.setViewTp = function() {
		if(this._tp != this._actor._tp){
			this.diff_tp= (this._tp-this._actor._tp)/this._fadeTime;//１回の増減値
			this.loop_limit_tp=this._fadeTime+1;
			this._tp = this._actor._tp;
			
		}
		
		if(this._tp!=this._view_tp){
			this._view_tp-=this.diff_tp;
			this._flg_redraw = true;
			this.loop_limit_tp--;
			if(this.diff_tp>=0){//減算処理
				if(this._tp>this._view_tp){
					this.loop_limit_tp=0;
				}
			}else{//加算処理
				if(this._tp<this._view_tp){
					this.loop_limit_tp=0;
				}
			}
		}
		
		//一定回数以上はループしない
		if(this.loop_limit_tp==0 && this._flg_redraw){
			this._tp=this._view_tp=this._actor._tp;
		}
	};
	
	//状態に変更が無いかチェック あれば表示更新
	Window_BattleStatusEX.prototype.checkStatus=function(){
		var buff=this.getStatusStr();//this._actor.allIcons().join(',');
		if(this._status != buff){
			this._status=buff;
			this._flg_redraw = true;
		}
	};
	
	
	//名前とステータスの描画
	Window_BattleStatusEX.prototype.drawNameAndStatus = function(){
		var width=this.textWidth(this._actor.name());
		this.drawText(this._actor.name(), 0, 0, width);
		
		this.drawActorIcons(this._actor, width+5, 0, this.width - width);
		
		//LV描画
		var text_lv = 'Lv '+this._actor._level;
		width = this.textWidth(text_lv);
		this.drawText(text_lv, 418 - width, 0, width);
	};
	
	//HP・MP・TPゲージの描画
	Window_BattleStatusEX.prototype.drawGauge = function(){
		var dx=this._gauge_padding_left;
		var dy=this._gauge_padding_top+this._name_line_height;
		//this._name_line_height
		this.contents.blt(this._img_gauge_base
						  ,0,0
						  ,this._img_gauge_base.width,this._img_gauge_base.height
						  ,dx,dy
						  );
		/*　MP
		this.contents.blt(this._img_gauge_base
						  ,0,0
						  ,this._img_gauge_base.width,this._img_gauge_base.height
						  ,dx,dy+this._MPgauge_padding_top
						  );
		*/
		
		//***TP
		this.contents.blt(this._img_gauge_base_TP
						  ,0,0
						  ,this._img_gauge_base_TP.width,this._img_gauge_base_TP.height
						  ,dx,dy+this._MPgauge_padding_top
						  );
		
		var drawWidth = this.calcDrawWidth(this._img_gauge_HP.width,this._view_hp,this._mhp);
		
		if (this._view_hp < this._mhp / 4){
			this.contents.blt(this._img_gauge_HP_red
							  ,0,0
							  ,drawWidth,this._img_gauge_HP_red.height
							  ,dx+3,dy+3
							  );
		}else{
			this.contents.blt(this._img_gauge_HP
							  ,0,0
							  ,drawWidth,this._img_gauge_HP.height
							  ,dx+3,dy+3
							  );	
		}
		
		/*　MP
		drawWidth= this.calcDrawWidth(this._img_gauge_MP.width,this._view_mp,this._mmp);
		this.contents.blt(this._img_gauge_MP
						  ,0,0
						  ,drawWidth,this._img_gauge_MP.height
						  ,dx+2,dy+this._MPgauge_padding_top+3
						  );
		*/
		
		//*** TP
		drawWidth= this.calcDrawWidth(this._img_gauge_TP.width,this._view_tp,this._mtp);
		this.contents.blt(this._img_gauge_TP
						  ,0,0
						  ,drawWidth,this._img_gauge_TP.height
						  ,dx+3,dy+this._MPgauge_padding_top+3
						  );
		var value_x=this._img_gauge_base.width/2;
		
		dy-=4;//描画位置をちょっと上に上げる
		
		this.drawText('HP', 0, dy);	
		
		//　MP
		//this.drawText('MP', 0, dy+this._MPgauge_padding_top);
		//*** TP
		this.drawText('TP', 0, dy+this._MPgauge_padding_top);
		
		this.changeTextColor(this.hpColor(this._actor));
		this.drawText(Math.round(this._view_hp)+'/'+this._mhp, value_x, dy);
		
		/*　MP
		this.changeTextColor(this.mpColor(this._actor));
		this.drawText(Math.round(this._view_mp)+'/'+this._mmp, value_x, dy+this._MPgauge_padding_top);
		*/
		//****TP
		value_x=this._img_gauge_TP.width/2;
		
		this.changeTextColor(this.mpColor(this._actor));
		this.drawText(Math.round(this._view_tp)+'/'+this._mtp, value_x, dy+this._MPgauge_padding_top);
				
		this.resetTextColor();
	};
	
	
	
	Window_BattleStatusEX.prototype.mpColor = function(actor) {
		if (this._view_mp<=0) {
			return this.deathColor();
		} else if (this._view_mp < this._mmp / 4) {
			return this.crisisColor();
		} else {
			return this.normalColor();
		}
	};

	Window_BattleStatusEX.prototype.calcDrawWidth = function(maxWidth,value,maxValue){
		var drawWidth = Math.round(maxWidth*value/maxValue);
		if(drawWidth>maxWidth){
			drawWidth=maxWidth;
		}
		return drawWidth;	
	};
	

	Window_BattleStatusEX.prototype.drawItem = function(index) {
		var actor = $gameParty.battleMembers()[index];
		this.drawBasicArea(this.basicAreaRect(index), actor);
		this.drawGaugeArea(this.gaugeAreaRect(index), actor);
		this.drawActorBustup(0);
	};
	
	Window_BattleStatusEX.prototype.getStatusStr = function() {
		return this._actor._states.join(',')+','+this._actor._buffs.join(',');

	};
	
	/*
	Window_BattleStatusEX.prototype.updateOpacity = function() {
		if(this.isShowWindow()) {
			this.contentsOpacity += 32;//表示状態
		}else{
			this.contentsOpacity -= 32;//非表示化
		}
	};*/
	
	
	Window_BattleStatusEX.prototype.select = function() {};
	Window_BattleStatusEX.prototype.deselect = function() {};
	Window_BattleStatusEX.prototype.refresh=function(){};


})();