var sviv_Sprite_Character_initialize = Sprite_Character.prototype.initialize;
Sprite_Character.prototype.initialize = function (character) {
	sviv_Sprite_Character_initialize.call(this, character);

	if (SceneManager._scene.constructor.name === "Scene_Survive_Battle_Map") {
		if (this._character.constructor.name == "Game_Player") {
			var sp = new Sprite_Gauge_H();
			sp.y = -70;
			this.addChild(sp);

			// 最大5まで重なって出る
			for (var i = 0; i < SURVIVOR_CONST.PLAYER_DAMAGE_SPRITES; i++) {
				var sp = new Sprite_CBRPlayerDamage(i);
				this._damage = sp;
				this.addChild(sp);
			}
		}
	}
};

//HPゲージ
function Sprite_Gauge_H() {
	this.initialize(...arguments);
}

Sprite_Gauge_H.prototype = Object.create(Sprite.prototype);
Sprite_Gauge_H.prototype.constructor = Sprite_Gauge_H;

Sprite_Gauge_H.prototype.initialize = function () {
	Sprite.prototype.initialize.call(this);

	this.createBitmap();
	this.anchor.x = 0.5;
	this.anchor.y = 0;

	this.setup();
};

Sprite_Gauge_H.prototype.createBitmap = function () {
	const width = this.bitmapWidth();
	const height = this.bitmapHeight();
	this.bitmap = new Bitmap(width, height);
};

Sprite_Gauge_H.prototype.bitmapWidth = function () {
	return 96;
};

Sprite_Gauge_H.prototype.bitmapHeight = function () {
	return 32;
};

Sprite_Gauge_H.prototype.gaugeHeight = function () {
	return 12;
};

Sprite_Gauge_H.prototype.gaugeX = function () {
	return 0;
};

Sprite_Gauge_H.prototype.setup = function () {
	this._value = null;
	this._maxValue = null;
	this.updateBitmap();
};

Sprite_Gauge_H.prototype.update = function () {
	Sprite.prototype.update.call(this);
	this.updateBitmap();
};

Sprite_Gauge_H.prototype.updateBitmap = function () {
	if (this._value != this.currentValue()) {
		this._value = this.currentValue();
		this._maxValue = this.currentMaxValue();

		this.redraw();
	}
};

Sprite_Gauge_H.prototype.currentValue = function () {
	return $gameTemp.SURVIVOR_player_data.nowhp;
};
Sprite_Gauge_H.prototype.currentMaxValue = function () {
	return $gameTemp.SURVIVOR_player_data.maxhp;
};
Sprite_Gauge_H.prototype.gaugeBackColor = function () {
	return ColorManager.gaugeBackColor();
};

Sprite_Gauge_H.prototype.gaugeColor1 = function () {
	return ColorManager.hpGaugeColor1();
};

Sprite_Gauge_H.prototype.redraw = function () {
	this.bitmap.clear();
	const currentValue = this.currentValue();
	if (!isNaN(currentValue)) {
		this.drawGauge();
	}
};

Sprite_Gauge_H.prototype.drawGauge = function () {
	const gaugewidth = this.bitmapWidth();
	const gaugeHeight = this.gaugeHeight();
	this.drawGaugeRect(0, 0, gaugewidth, gaugeHeight);
};

Sprite_Gauge_H.prototype.drawGaugeRect = function (x, y, width, height) {
	const rate = this.gaugeRate();
	const fillW = Math.floor((width - 2) * rate);
	const fillH = height - 2;
	const color0 = this.gaugeBackColor();
	const color1 = this.gaugeColor1();
	this.bitmap.fillRect(x, y, width, height, color0);
	this.bitmap.gradientFillRect(x + 1, y + 1, fillW, fillH, color1, color1);
};

Sprite_Gauge_H.prototype.gaugeRate = function () {
	const value = this._value;
	const maxValue = this._maxValue;
	return maxValue > 0 ? value / maxValue : 0;
};

//経験値ゲージ
//HPゲージ
function Sprite_Gauge_K() {
	this.initialize(...arguments);
}

Sprite_Gauge_K.prototype = Object.create(Sprite.prototype);
Sprite_Gauge_K.prototype.constructor = Sprite_Gauge_K;

Sprite_Gauge_K.prototype.initialize = function () {
	Sprite.prototype.initialize.call(this);

	this.createBitmap();
	this.anchor.x = 0.5;
	this.anchor.y = 0;

	this.setup();
};

Sprite_Gauge_K.prototype.createBitmap = function () {
	const width = this.bitmapWidth();
	const height = this.bitmapHeight();
	this.bitmap = new Bitmap(width, height);
};

Sprite_Gauge_K.prototype.bitmapWidth = function () {
	return Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN - 40;
};

Sprite_Gauge_K.prototype.bitmapHeight = function () {
	return 48;
};

Sprite_Gauge_K.prototype.gaugeHeight = function () {
	return 24;
};

Sprite_Gauge_K.prototype.gaugeX = function () {
	return 0;
};

Sprite_Gauge_K.prototype.setup = function () {
	this._value = null;
	this._maxValue = null;
	this.updateBitmap();
};

Sprite_Gauge_K.prototype.update = function () {
	Sprite.prototype.update.call(this);
	this.updateBitmap();
};

Sprite_Gauge_K.prototype.updateBitmap = function () {
	if (this._value != this.currentValue()) {
		this._value = this.currentValue();
		this._maxValue = this.currentMaxValue();

		this.redraw();
	}
};

Sprite_Gauge_K.prototype.currentValue = function () {
	// return this.currentMaxValue() - $gameActors.actor(1).nextRequiredExp();
	return $gameTemp.SURVIVOR_player_data.now_exp;
};
Sprite_Gauge_K.prototype.currentMaxValue = function () {
	// var ac = $gameActors.actor(1);
	// return ac.expForLevel(ac._level + 1) - ac.expForLevel(ac._level);
	return $gameTemp.SURVIVOR_next_exp($gameTemp.SURVIVOR_player_data.level);
};
Sprite_Gauge_K.prototype.gaugeBackColor = function () {
	return ColorManager.gaugeBackColor();
};

Sprite_Gauge_K.prototype.gaugeColor1 = function () {
	return "#a32424ff";
};
Sprite_Gauge_K.prototype.gaugeColor2 = function () {
	return "#f73839";
};

Sprite_Gauge_K.prototype.redraw = function () {
	this.bitmap.clear();
	const currentValue = this.currentValue();
	if (!isNaN(currentValue)) {
		this.drawGauge();
	}
};

Sprite_Gauge_K.prototype.drawGauge = function () {
	const gaugewidth = this.bitmapWidth();
	const gaugeHeight = this.gaugeHeight();
	this.drawGaugeRect(0, 0, gaugewidth, gaugeHeight);
};

Sprite_Gauge_K.prototype.drawGaugeRect = function (x, y, width, height) {
	const rate = this.gaugeRate();
	const fillW = Math.floor((width - 2) * rate);
	const fillH = height - 2;
	const color0 = this.gaugeBackColor();
	const color1 = this.gaugeColor1();
	const color2 = this.gaugeColor2();
	this.bitmap.fillRect(x, y, width, height, color0);
	this.bitmap.gradientFillRect(x + 1, y + 1, fillW, fillH, color1, color2);
};

Sprite_Gauge_K.prototype.gaugeRate = function () {
	const value = this._value;
	const maxValue = this._maxValue;
	return maxValue > 0 ? value / maxValue : 0;
};

// #################### 経験値 ######################

function Sprite_Keiken() {
	this.initialize(...arguments);
}

Sprite_Keiken.prototype = Object.create(Sprite.prototype);
Sprite_Keiken.prototype.constructor = Sprite_Keiken;

Sprite_Keiken.prototype.initialize = function () {
	Sprite.prototype.initialize.call(this);

	this.anchor.x = 0.5;
	this.anchor.y = 0.5;

	this.scale.x = 0.3;
	this.scale.y = 0.3;
	this.speed = 0;

	this.disX = $gameMap._parallaxX;
	this.disY = $gameMap._parallaxY;

	this.setCharacterBitmap();
};
//場所移動するだけでいいや
Sprite_Keiken.prototype.update = function () {
	if (this.death) {
		return;
	}
	Sprite.prototype.update.call(this);

	this.x -= ($gameMap._parallaxX - this.disX) * 48;
	this.disX = $gameMap._parallaxX;
	this.y -= ($gameMap._parallaxY - this.disY) * 48;
	this.disY = $gameMap._parallaxY;

	if (!this.speed) {
		var kyori = (this.x - Graphics.boxWidth / 2) * (this.x - Graphics.boxWidth / 2);
		kyori += (this.y - Graphics.boxHeight / 2) * (this.y - Graphics.boxHeight / 2);
		kyori = Math.sqrt(kyori);
		//プレイヤーが接触したら
		if (kyori < $gameTemp.SURVIVOR_player_data.collect_range) {
			this.speed = -8;
		}
	} else {
		//atan2使うと微妙に遅いからx^2 + y^2 = r^2 、y=kxを使ってspeed(移動させたい距離r)として動かしたいxとyの大きさを導く
		var sa_x = this.x - Graphics.boxWidth / 2;
		var sa_y = this.y - Graphics.boxHeight / 2;
		if (this.speed < 0) {
			sa_x *= -1;
			sa_y *= -1;
		}
		var k = sa_y / sa_x;

		var x = Math.sqrt((this.speed * this.speed) / (1 + k * k));

		//xを基に導くけど2乗してるから+-どっちかわからん
		if (0 < sa_x) {
			x = -x;
		}

		var y = k * x;

		//xかyどちらかが中央超えたら削除
		if (sa_x * (sa_x + x) < 0 || sa_y * (sa_y + y) < 0) {
			//this.visible = false;
			this.death = true;
			this.speed = 0;
			survivorState.keikenDel.push(this.spriteId);
			if (!survivorState.se_cool) {
				AudioManager.playSe({ name: "Item1", volume: 20, pitch: 150, pan: 0 });
				survivorState.se_cool = 5;
			}
		}
		this.x += x;
		this.y += y;
		this.speed += 0.2;
	}
};

Sprite_Keiken.prototype.setCharacterBitmap = function () {
	this.bitmap = ImageManager.loadCharacter("!Door2");
	this.setFrame(48 * 3, 48 * 2, 48, 48);
};

function Sprite_CBRPlayerDamage() {
	this.initialize(...arguments);
}

Sprite_CBRPlayerDamage.prototype = Object.create(Sprite.prototype);
Sprite_CBRPlayerDamage.prototype.constructor = Sprite_CBRPlayerDamage;

Sprite_CBRPlayerDamage.prototype.initialize = function (index) {
	Sprite.prototype.initialize.call(this);

	this.index = index;
	this.anchor.x = 0.5;
	this.visible = false;

	this._damages = [];
	this.duration = 0;

	this.create();
};

Sprite_CBRPlayerDamage.prototype.create = function () {
	//まぁ最大で3桁でしょ
	for (var i = 0; i < 3; i++) {
		var sp = new Sprite();
		sp.bitmap = ImageManager.loadSystem("Damage");
		sp.x = 12 * i;
		sp.scale.x = 1;
		sp.scale.y = 1;
		this._damages.push(sp);
		this.addChild(sp);
	}
};

Sprite_CBRPlayerDamage.prototype.setFrame = function (dm) {
	if (dm < 10) {
		this.x = -12 * 2.5;
		this._damages[0].visible = false;
		this._damages[1].visible = false;

		this._damages[2].setFrame(12 * dm, 0, 12, 16);
	} else if (dm < 100) {
		this.x = -12 * 2;
		var temp = Math.floor(dm / 10);
		this._damages[0].visible = false;
		this._damages[1].visible = true;

		this._damages[1].setFrame(12 * temp, 0, 12, 16);
		this._damages[2].setFrame(12 * Math.floor(dm - temp * 10), 0, 12, 16);
	} else {
		this.x = -12 * 1.5;
		var temp = Math.floor(dm / 100);
		this._damages[0].setFrame(12 * temp, 0, 12, 16);
		dm -= temp * 100;
		var temp = Math.floor(dm / 10);
		this._damages[1].setFrame(12 * temp, 0, 12, 16);
		this._damages[2].setFrame(12 * Math.floor(dm - temp * 10), 0, 12, 16);
	}
};

Sprite_CBRPlayerDamage.prototype.update = function () {
	Sprite.prototype.update.call(this);

	// var d = $gameTemp.SURVIVOR_player_data.disp_damages[this.index];
	// if (d && d.visible) {
	// 	//最初の時
	// 	if (d.duration == SURVIVOR_CONST.DAMAGE_DURATION) {
	// 		this.setFrame(d.dm);
	// 		this.visible = true;
	// 	}

	// 	this.y = -Math.sin((3.14 / SURVIVOR_CONST.DAMAGE_DURATION) * (SURVIVOR_CONST.DAMAGE_DURATION - d.duration)) * SURVIVOR_CONST.DAMAGE_DURATION;
	// 	d.duration--;
	// 	if (d.duration < 0) {
	// 		this.visible = false;
	// 	}
	// } else {
	this.visible = false;
	// }
};
