//WEAPONクラスを作るときの設定値みたいなもん
function WEAPON_MAKER(name, level) {
	const atk_num_level = $gameTemp.SURVIVOR_player_data.ability_level.find((e) => e.id == "atk_num")?.level || 0;

	// アビリティとステータスの弾数を合計したもの
	var abi_num = SURVIVOR_ABILITY_DATA.atk_num[atk_num_level] || 0 + $gameTemp.SURVIVOR_player_data.atk_num || 0;
	if (name == "fire") {
		var num = [0, 1, 2, 3, 4, 5];
		this.num = num[level] + abi_num; //何個作る？
	} else if (name == "thunder") {
		var num = [0, 1, 1, 2, 2, 3];
		this.num = num[level] + abi_num;
		this.wave = 3; // num以外全部いらんかも、このMAKERのヤツ
		this.rag = 4;
		this.t = 0;
	} else if (name == "ice") {
		var num = [0, 2, 3, 4, 5, 7];
		this.num = num[level] + abi_num; //クールタイム毎に何個出す？
	} else if (name == "maryoku_gun") {
		var num = [0, 1, 2, 3, 4, 5];
		this.num = num[level] + abi_num; //クールタイム毎に何個出す？
		this.wave = 8; //同時に何個のナイフを飛ばすか
		this.rag = 4; //同時に飛ばした場合のフレーム差
		this.t = 0;
	} else if (name == "oboro") {
		var num = [0, 1, 2, 3, 4, 5];
		this.num = num[level] + abi_num;
		this.wave = num[level] + abi_num; //同時に何個のナイフを飛ばすか
		this.rag = 3; //同時に飛ばした場合のフレーム差
		this.t = 0;
	} else if (name == "houou_soutou") {
		var num = [0, 2, 2, 3, 3, 4];
		this.num = num[level] + abi_num;
		this.wave = num[level] + abi_num; //同時に何個のナイフを飛ばすか
		this.rag = 3; //同時に飛ばした場合のフレーム差
		this.t = 0;
	} else if (name == "sword") {
		var num = [0, 1, 1, 1, 1, 1];
		this.num = 1;
		this.wave = 1; //同時に何個のナイフを飛ばすか
		this.rag = 3; //同時に飛ばした場合のフレーム差
		this.t = 0;
	}
}

function WEAPON(name, id, maker, level) {
	this.name = name;
	this.id = Math.floor(Math.random() * 100000);
	this.x = 0;
	this.y = 0;
	this.disX = 0;
	this.disY = 0;
	this.opacity = 255;

	if (name == "fire") {
		this.anchorX = 0.5;
		this.anchorY = 0.5;
		this.damage = 7 + level * 2;
		this.r = 17; //炎の大きさ レベルアップで大きくなる
		this.r2 = 100 + level * 20; //軌道の大きさ
		this.kb = { f: 4, power: 10 }; //ノックバック
		this.cool = 10;
		this.img = "!Flame";
		this.img_indexX = 0;
		this.img_indexY = 0;
		this.img_width = 48;
		this.img_height = 48;
		this.num = maker.num; //総数
		this.order = id;
		this.t = 0; // tの初期値
		this.base_scale = 1;

		// this.r += 4 * this.level;
		// this.damage += 4 * this.level;

		this.update_orbit = function () {
			//グルグル回転
			this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2 + this.r2 * Math.cos(this.t / 10 + (3.14 * 2 * this.order) / this.num);
			this.y = Graphics.height / 2 + this.r2 * Math.sin(this.t / 10 + (3.14 * 2 * this.order) / this.num);
			//面白かったので残す
			//this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN)/2 + this.r2 * Math.cos(this.t/10 + 3.14*2/this.order/(this.num/2));
			//this.y = Graphics.height/2 + this.r2 * Math.sin(this.t/10 + 3.14*this.order/(this.num/2));

			//プレイヤーの移動分を補正
			this.x -= ($gameMap._parallaxX - this.disX) * 48;
			this.disX = $gameMap._parallaxX;
			this.y -= ($gameMap._parallaxY - this.disY) * 48;
			this.disY = $gameMap._parallaxY;

			this.t += 0.8 * $gameTemp.SURVIVOR_player_data.atk_speed;
		};
	} else if (name == "thunder") {
		this.anchorX = 0.5;
		this.anchorY = 0.5;
		this.damage = 10 + level * 4;
		this.r = 40 * (1 + level / 5);
		this.base_scale = 0.5 * (1 + level / 5);
		this.kb = { f: 4, power: 20 };
		this.cool = 20; // これもう必要なくね？
		this.count = 0; //攻撃した敵の数
		this.count_max = 10 + level * 5; //攻撃できる数
		this.speed = 1;
		this.rate = 310; // 何フレーム毎に出すか
		this.img = "Thunder2";
		this.img_indexX = 0;
		this.img_indexY = 0;
		this.img_width = 192;
		this.img_height = 192;
		this.dir = 2 * 3.14 * Math.random();
		this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
		this.y = Graphics.height / 2;
		this.t = maker.t;

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

		this.update_orbit = function () {
			//まっすぐ進む
			this.x += Math.cos(this.dir) * this.speed * $gameTemp.SURVIVOR_player_data.atk_speed;
			this.y += Math.sin(this.dir) * this.speed * $gameTemp.SURVIVOR_player_data.atk_speed;

			//プレイヤーの移動分を補正
			this.x -= ($gameMap._parallaxX - this.disX) * 48;
			this.disX = $gameMap._parallaxX;
			this.y -= ($gameMap._parallaxY - this.disY) * 48;
			this.disY = $gameMap._parallaxY;

			this.opacity = 255;
			if ((this.rate - 60) * $gameTemp.SURVIVOR_player_data.atk_rate < this.t % (this.rate * $gameTemp.SURVIVOR_player_data.atk_rate)) {
				if (this.t % 2 == 0) {
					this.opacity = 0;
				}
			}

			this.t++;
			if (this.rate * $gameTemp.SURVIVOR_player_data.atk_rate < this.t) {
				this.id = Math.floor(Math.random() * 100000);
				this.dir = 2 * 3.14 * Math.random();
				this.t = 0;
				this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
				this.y = Graphics.height / 2;
				this.count = 0;
				this.break = false;
			}
		};
	} else if (name == "ice") {
		this.anchorX = 0.5;
		this.anchorY = 0.8;
		this.damage = 9 + level * 3;
		this.cool = 3;
		this.r = 40; //氷の大きさ
		this.r2 = 180; //出現範囲の大きさ
		this.kb = { f: 3, power: 10 };
		this.cool_idi = 8; //次の氷が出るまで
		this.cool_all = 240; //次の氷全部リセット
		this.img = "Ice4";
		this.img_indexX = 0;
		this.img_indexY = 0;
		this.img_width = 192;
		this.img_height = 192;
		this.count = 0; //攻撃した敵の数
		this.count_max = 30; //攻撃できる数
		this.order = id;
		this.t = 0;
		this.base_scale = 0.5;

		this.update_orbit = function () {
			//残り時間60fで点滅
			this.opacity = 255;
			if ((this.cool_all - 60) * $gameTemp.SURVIVOR_player_data.atk_rate < this.t % (this.cool_all * $gameTemp.SURVIVOR_player_data.atk_rate)) {
				if (this.t % 2 == 0) {
					this.opacity = 0;
				}
			}

			//プレイヤーの移動分を補正
			this.x -= ($gameMap._parallaxX - this.disX) * 48;
			this.disX = $gameMap._parallaxX;
			this.y -= ($gameMap._parallaxY - this.disY) * 48;
			this.disY = $gameMap._parallaxY;

			// 最初
			if (this.cool_all * $gameTemp.SURVIVOR_player_data.atk_rate < this.t) {
				var rad = 3.14 * 2 * Math.random();
				var r = this.r2 * Math.random();
				this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2 + r * Math.cos(rad);
				this.y = Graphics.height / 2 + r * Math.sin(rad);
				this.break = false;
				this.count = 0;
				this.t = 0;
			}

			this.t++;
		};
	} else if (name == "maryoku_gun") {
		this.anchorX = 0.5;
		this.anchorY = 0.5;
		this.damage = 10 + level * 2;
		this.r = 50;
		this.kb = { f: 4, power: 20 };
		this.cool = 2;
		this.count = 0; //攻撃した敵の数
		this.count_max = 1 + level * 2; //攻撃できる数
		this.speed = 5;
		this.num = maker.num; //1度に発射される総数
		this.rate = 40; // 発射感覚
		this.rate2 = 4; // 単発の発射間隔
		this.rate_all = this.rate * 5; //一巡の発射感覚
		this.order = id;
		this.img = "maryoku_gun";
		this.img_indexX = 0;
		this.img_indexY = 0;
		this.img_width = 24;
		this.img_height = 24;
		this.dir = Survivor_var.player_dir;
		this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2; //キャラの中心
		this.y = Graphics.height / 2;
		this.t = Math.floor(this.order / this.num) * this.rate + (this.order % this.num) * this.rate2;
		this.base_scale = 1;
		this.break = true;

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

		this.update_orbit = function () {
			//まっすぐ進む
			this.x += Math.cos(this.dir) * this.speed * $gameTemp.SURVIVOR_player_data.atk_speed;
			this.y += Math.sin(this.dir) * this.speed * $gameTemp.SURVIVOR_player_data.atk_speed;

			//プレイヤーの移動分を補正
			this.x -= ($gameMap._parallaxX - this.disX) * 48;
			this.disX = $gameMap._parallaxX;
			this.y -= ($gameMap._parallaxY - this.disY) * 48;
			this.disY = $gameMap._parallaxY;

			this.t += 1 / $gameTemp.SURVIVOR_player_data.atk_rate;
			if (this.rate_all < this.t) {
				this.id = Math.floor(Math.random() * 100000);
				this.dir = Survivor_var.player_dir;
				this.t = 0;
				this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
				this.y = Graphics.height / 2;
				this.count = 0;
				this.break = false;
			}
		};
	} else if (name == "oboro") {
		this.anchorX = 0.427;
		this.anchorY = 0.72;
		this.damage = 22 + level * 5;
		this.r = 42.42;
		this.kb = { f: 5, power: 30 };
		this.cool = 20;
		this.count = 0; //攻撃した敵の数
		this.count_max = 3 + level * 2; //攻撃できる数
		this.speed = 1;
		this.speed_dir = 4 * Math.random() - 2; // -1 ~ +1
		this.rate = 120; // 何フレーム毎に出すか
		this.rate2 = 4; // 何フレーム毎に出すか
		this.rate_all = this.rate * 5; // 何フレーム毎に出すか
		this.kasokudo = -10;
		this.img = "Weapons4";
		this.img_indexX = 0;
		this.img_indexY = 1;
		this.img_width = 96;
		this.img_height = 64;
		this.img_indexX_list = [1];
		this.dir = Survivor_var.player_dir;
		this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2; //キャラの中心
		this.y = Graphics.height / 2;
		this.num = maker.num; //総数
		this.order = id;
		this.t = Math.floor(this.order / this.num) * this.rate + (this.order % this.num) * this.rate2;
		this.base_scale = 1;
		this.break = true;

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

		this.update_orbit = function () {
			// max速度は30まで
			if (this.kasokudo < 8) {
				this.kasokudo += 0.3;
			}

			//放物線
			this.x += this.speed * this.speed_dir * $gameTemp.SURVIVOR_player_data.atk_speed;
			this.y += this.kasokudo * this.speed * $gameTemp.SURVIVOR_player_data.atk_speed;
			this.rotation = -this.t / 3.14;

			//プレイヤーの移動分を補正
			this.x -= ($gameMap._parallaxX - this.disX) * 48;
			this.disX = $gameMap._parallaxX;
			this.y -= ($gameMap._parallaxY - this.disY) * 48;
			this.disY = $gameMap._parallaxY;

			this.t += 1 / $gameTemp.SURVIVOR_player_data.atk_rate;
			if (this.rate_all < this.t) {
				this.id = Math.floor(Math.random() * 100000);
				this.dir = Survivor_var.player_dir;
				this.t = 0;
				this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
				this.y = Graphics.height / 2;
				this.kasokudo = -10;
				this.y_total = 0;
				this.speed_dir = 2 * Math.random() - 1;
				this.count = 0;
				this.break = false;
			}
		};
	} else if (name == "houou_soutou") {
		this.anchorX = 0.427;
		this.anchorY = 0.72;
		this.damage = 12 + level * 2;
		this.r = 40.1;
		this.r2 = 0; // 回転の半径
		this.kb = { f: 5, power: 30 };
		this.cool = 20;
		this.count = 0; //攻撃した敵の数
		this.count_max = 5 + level * 3; //攻撃できる数
		this.order = id;
		this.num = maker.num; //総数
		this.speed = 0.5;
		this.rate = 200; // 何フレーム毎に出すか
		this.rate2 = 4; // 何フレーム毎に出すか
		this.rate_all = this.rate * 5; // 何フレーム毎に出すか
		this.kasokudo = -10;
		this.img = "Weapons1";
		this.img_indexX = 0;
		this.img_indexY = 1;
		this.img_width = 96;
		this.img_height = 64;
		this.img_indexX_list = [1];
		this.dir = Survivor_var.player_dir;
		this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2; //キャラの中心
		this.y = Graphics.height / 2;
		this.base_x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2; //キャラの中心
		this.base_y = Graphics.height / 2;
		this.t = Math.floor(this.order / this.num) * this.rate + (this.order % this.num) * this.rate2;
		this.base_scale = 1;
		this.disX = $gameMap._parallaxX;
		this.disY = $gameMap._parallaxY;
		this.break = true;
		this.update_orbit = function () {
			//回転
			var speed = this.speed * $gameTemp.SURVIVOR_player_data.atk_speed;
			this.x = this.base_x;
			this.x += this.r2 * Math.cos((this.t / 20) * speed + (3.14 * 2 * this.order) / this.num);

			this.y = this.base_y;
			this.y += this.r2 * Math.sin((this.t / 20) * speed + (3.14 * 2 * this.order) / this.num);
			this.rotation = -this.t / 3.14;
			this.r2 += 1 * speed;
			//プレイヤーの移動分を補正
			this.base_x -= ($gameMap._parallaxX - this.disX) * 48;
			this.disX = $gameMap._parallaxX;
			this.base_y -= ($gameMap._parallaxY - this.disY) * 48;
			this.disY = $gameMap._parallaxY;
			this.t += 1 / $gameTemp.SURVIVOR_player_data.atk_rate;
			if (this.rate_all < this.t) {
				this.id = Math.floor(Math.random() * 100000);
				this.dir = Survivor_var.player_dir;
				this.t = 0;

				this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
				this.y = Graphics.height / 2;
				this.base_x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
				this.base_y = Graphics.height / 2;

				this.y_total = 0;
				this.r2 = 0;
				this.count = 0;
				this.break = false;
			}
		};
	} else if (name == "sword") {
		this.anchorX = 0.427;
		this.anchorY = 0.72;
		this.damage = 15 + level * 15;
		this.r = 67.88;
		this.r2 = 150;
		this.kb = { f: 5, power: 20 };
		this.cool = 20;
		this.count = 0; //攻撃した敵の数
		this.count_max = 10 + level * 5; //攻撃できる数
		this.speed = 0.5;
		this.rate = 90; // 何フレーム毎に出すか
		this.rate_dispIdx = 0; // 何枚目を表示するか
		this.img = "Slash";
		this.img_indexX = 0;
		this.img_indexY = 0;
		this.img_width = 192;
		this.img_height = 192;

		this.dir = Survivor_var.player_dir || 0;
		this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2; //キャラの中心
		this.x += this.r2 * Math.cos(this.dir);
		this.y = Graphics.height / 2;
		this.y += this.r2 * Math.sin(this.dir);
		this.t = 0;
		this.base_scale = 0.5;
		this.order = 1;
		this.num = maker.num; //総数
		this.disX = $gameMap._parallaxX;
		this.disY = $gameMap._parallaxY;
		this.update_orbit = function () {
			// プレイヤーの正面に
			this.dir = this.dir || 0;
			this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
			this.x += this.r2 * Math.cos(this.dir);

			this.y = Graphics.height / 2;
			this.y += this.r2 * Math.sin(this.dir);

			this.rate_dispIdx = Math.floor(this.t / 3);
			this.t++;
			if (5 < this.t) {
				this.break = 99999;
			}
			if (this.rate < this.t) {
				this.id = Math.floor(Math.random() * 100000);
				this.dir = Survivor_var.player_dir;
				this.t = 0;

				this.count = 0;
				this.break = false; //武器のブレイク解除
			}
		};
	}
}

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

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

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

	this.data = data;

	this.setCharacterBitmap();
};
//登場するモンスターは1つの画像にまとめよう、updateはフレームだけ
Sprite_Survive_Weapon.prototype.update = function () {
	Sprite.prototype.update.call(this);
	//this.updateBitmap();
	//this.updateFrame();
	this.updateCharacterFrame();
	this.updatePosition();
	//this.updateOther();
	this.updateVisibility();
	this.updateOpacity();

	this.anchor.x = this.data.anchorX;
	this.anchor.y = this.data.anchorY;

	this.scale.x = (this.data.base_scale ? this.data.base_scale : 1) * $gameTemp.SURVIVOR_player_data.atk_range;
	this.scale.y = (this.data.base_scale ? this.data.base_scale : 1) * $gameTemp.SURVIVOR_player_data.atk_range;

	if (this.data.break) {
		this.data.del = true;
		this.visible = false;
		this.filters = [];
	}
};

Sprite_Survive_Weapon.prototype.updateOpacity = function () {
	this.opacity = this.data.opacity;
};

Sprite_Survive_Weapon.prototype.updatePosition = function () {
	this.x = this.data.x;
	this.y = this.data.y;
	this.rotation = this.data.rotation || 0;
};

Sprite_Survive_Weapon.prototype.updateBitmap = function () {
	if (this.isImageChanged()) {
		this._tilesetId = $gameMap.tilesetId();
		this._tileId = this._character.tileId();
		this._characterName = this._character.characterName();
		this._characterIndex = this._character.characterIndex();
		if (this._tileId > 0) {
			this.setTileBitmap();
		} else {
			this.setCharacterBitmap();
		}
	}
};

Sprite_Survive_Weapon.prototype.updateVisibility = function () {
	Sprite.prototype.updateVisibility.call(this);
	//if (0) {
	this.visible = true;
	//}
};

Sprite_Survive_Weapon.prototype.setCharacterBitmap = function () {
	this.bitmap = ImageManager.loadSystem(this.data.img);
};

Sprite_Survive_Weapon.prototype.updateCharacterFrame = function () {
	const pw = this.patternWidth();
	const ph = this.patternHeight();
	const sx = (this.characterBlockX() + this.characterPatternX()) * pw;
	const sy = (this.characterBlockY() + this.characterPatternY()) * ph;

	if (this._bushDepth > 0) {
		const d = this._bushDepth;
		this._upperBody.setFrame(sx, sy, pw, ph - d);
		this._lowerBody.setFrame(sx, sy + ph - d, pw, d);
		this.setFrame(sx, sy, 0, ph);
	} else {
		this.setFrame(sx, sy, pw, ph);
	}
	this.t++;
};

// 武器の画像パターンはココでやる
Sprite_Survive_Weapon.prototype.characterPatternX = function () {
	var d = this.data;
	if (d.name == "ice") {
		var m = Math.min(2, Math.floor((Math.floor(d.t) % Math.floor(d.cool_all)) / 10 - d.order));

		return m; //最大で2
	} else if (d.name == "thunder") {
		return Math.floor(Math.floor(d.t) / 4) % 3; //最大で2
		// パターンはそのまま
	} else if (d.name == "oboro" || d.name == "houou_soutou") {
		return d.img_indexX;
	} else if (d.name == "sword") {
		return d.rate_dispIdx;
	}
	return Math.floor(d.t / 10) % 2; //this._character.pattern();
};

Sprite_Survive_Weapon.prototype.characterPatternY = function () {
	var d = this.data;
	return d.img_indexY; //(this._character.direction() - 2) / 2;
};
Sprite_Survive_Weapon.prototype.patternWidth = function () {
	//return this.bitmap.width / this.data.img_size;
	return this.data.img_width;
};

Sprite_Survive_Weapon.prototype.patternHeight = function () {
	//return this.bitmap.height / 8;
	return this.data.img_height;
};
Sprite_Survive_Weapon.prototype.characterBlockX = function () {
	var d = this.data;
	const index = d.img_indexX; //this._character.characterIndex();
	if (d.name == "oboro" || d.name == "houou_soutou") {
		return 1;
	}
	return (index % 4) * 3;
};

Sprite_Survive_Weapon.prototype.characterBlockY = function () {
	const index = this.data.img_indexY; //this._character.characterIndex();
	return Math.floor(index / 4) * 4;
};
