//MOBクラスに直接書くよりコッチの方が見やすい
//基本ステータス
const mob_id_list = [
	"ossan",
	"ossan_mura",
	"ossan_gold",
	"ossan_red",
	"ossan_hakase",
	"ossan_hage",
	"ossan_battle",
	"ossan_long",
	"ossan_back",
	"ossan_majime",
	"monster_ghost",
	"monster_bone",
	"monster_orc",
	"monster_imp",
	"monster_flower_r",
	"monster_flower_b",
	"monster_slime_b",
	"monster_slime_g",
	"monster_succubus_r",
	"monster_succubus_g",
	"bat",
	"medama",
	"bee",
	"jelly",
	"fishman",
	"scorpion",
	"spider"
];
const mobData = {
	ossan: {
		name: "オッサン",
		img: "ossan_sao",
		img_index: 0,
		maxHp: 20,
		hp: 20,
		exp: 1,
		speed: 0.7,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	ossan_mura: {
		name: "オッサン紫",
		img: "ossan_sao",
		img_index: 1,
		maxHp: 30,
		hp: 30,
		exp: 2,
		speed: 1.5,
		weight: 5,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	ossan_gold: {
		name: "オッサン金",
		img: "ossan_sao",
		img_index: 2,
		maxHp: 150,
		hp: 150,
		exp: 10,
		speed: 1,
		weight: 30,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	ossan_red: {
		name: "オッサン赤",
		img: "ossan_sao",
		img_index: 3,
		maxHp: 50,
		hp: 50,
		exp: 2,
		speed: 2,
		weight: 10,
		damage: 2,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	ossan_hakase: {
		name: "オッサンハカセ",
		img: "ossan_minna",
		img_index: 0,
		maxHp: 50,
		hp: 50,
		exp: 2,
		speed: 1,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	ossan_hage: {
		name: "オッサンハゲ",
		img: "ossan_minna",
		img_index: 0,
		maxHp: 50,
		hp: 50,
		exp: 2,
		speed: 1,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	ossan_battle: {
		name: "オッサンバトル",
		img: "ossan_minna",
		img_index: 2,
		maxHp: 70,
		hp: 70,
		exp: 3,
		speed: 1.5,
		weight: 10,
		damage: 3,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	ossan_long: {
		name: "オッサンロン毛",
		img: "ossan_minna",
		img_index: 3,
		maxHp: 50,
		hp: 50,
		exp: 2,
		speed: 1,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	ossan_back: {
		name: "オッサンオールバック",
		img: "ossan_minna",
		img_index: 4,
		maxHp: 50,
		hp: 50,
		exp: 2,
		speed: 1,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	ossan_majime: {
		name: "オッサン真面目",
		img: "ossan_minna",
		img_index: 5,
		maxHp: 50,
		hp: 50,
		exp: 2,
		speed: 1,
		weight: 10,
		damage: 2,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_ghost: {
		name: "モンスターゴースト",
		img: "Monster1",
		img_index: 0,
		maxHp: 20,
		hp: 20,
		exp: 1,
		speed: 2,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_bone: {
		name: "モンスター骨",
		img: "Monster1",
		img_index: 1,
		maxHp: 30,
		hp: 30,
		exp: 1,
		speed: 0.7,
		weight: 10,
		damage: 3,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_orc: {
		name: "モンスターオーク",
		img: "Monster1",
		img_index: 2,
		maxHp: 100,
		hp: 100,
		exp: 5,
		speed: 0.5,
		weight: 10,
		damage: 3,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_imp: {
		name: "モンスターインプ",
		img: "Monster1",
		img_index: 3,
		maxHp: 30,
		hp: 30,
		exp: 1,
		speed: 2,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_flower_r: {
		name: "花赤",
		img: "Monster2",
		img_index: 0,
		maxHp: 30,
		hp: 30,
		exp: 1,
		speed: 0.8,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_flower_b: {
		name: "花青",
		img: "05_Enemy02",
		img_index: 2,
		maxHp: 80,
		hp: 80,
		exp: 4,
		speed: 0.8,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_slime_b: {
		name: "スライム青",
		img: "Monster2",
		img_index: 2,
		maxHp: 10,
		hp: 10,
		exp: 1,
		speed: 0.5,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_slime_g: {
		name: "スライム緑",
		img: "Monster2",
		img_index: 6,
		maxHp: 30,
		hp: 30,
		exp: 2,
		speed: 0.5,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_succubus_r: {
		name: "サキュバス赤",
		img: "Monster2",
		img_index: 3,
		maxHp: 100,
		hp: 100,
		exp: 8,
		speed: 1.3,
		weight: 10,
		damage: 3,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	monster_succubus_g: {
		name: "サキュバス緑",
		img: "Monster2",
		img_index: 7,
		maxHp: 100,
		hp: 100,
		exp: 10,
		speed: 1.3,
		weight: 10,
		damage: 4,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	bat: {
		name: "コウモリ",
		img: "Monster3",
		img_index: 0,
		maxHp: 5,
		hp: 5,
		exp: 1,
		speed: 2,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	medama: {
		name: "目玉",
		img: "Monster3",
		img_index: 1,
		maxHp: 120,
		hp: 120,
		exp: 7,
		speed: 0.8,
		weight: 10,
		damage: 2,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	bee: {
		name: "ハチ",
		img: "Monster3",
		img_index: 2,
		maxHp: 10,
		hp: 10,
		exp: 1,
		speed: 2,
		weight: 10,
		damage: 1,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	jelly: {
		name: "海月",
		img: "Monster3",
		img_index: 3,
		maxHp: 20,
		hp: 20,
		exp: 2,
		speed: 1,
		weight: 10,
		damage: 2,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	fishman: {
		name: "サハギン",
		img: "Monster3",
		img_index: 5,
		maxHp: 100,
		hp: 100,
		exp: 9,
		speed: 1,
		weight: 10,
		damage: 3,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},

	scorpion: {
		name: "サソリ",
		img: "Monster3",
		img_index: 6,
		maxHp: 15,
		hp: 15,
		exp: 1,
		speed: 1.5,
		weight: 10,
		damage: 2,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	},
	spider: {
		name: "クモ",
		img: "Monster3",
		img_index: 7,
		maxHp: 20,
		hp: 20,
		exp: 1,
		speed: 2,
		weight: 10,
		damage: 2,
		r: 20,
		dm_cool: [], //こいつが攻撃する時のクールタイム
		appear_cool: 0
	}
};
// MOBがやられた時の流れ
// resetMobされる
// Spriteはそのまま
function MOB(name, id) {
	this.resetMob(name, id);
}

MOB.prototype.resetMob = function (name, id) {
	this.cool = []; //やられた時のクールタイム {id:id , duration:フレーム数}が入る

	this.name = name;
	this.id = id;

	//まず中央に配置
	this.x = (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2;
	this.y = Graphics.height / 2;
	var key = Math.random() - 0.5;
	//左右のタテ直線ラインに配置
	if (Math.random() < 0.5) {
		this.x += Math.sign(key) * ((Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) / 2) * (Math.random() + 1);
		this.y += Graphics.height * (Math.random() - 0.5);
	} else {
		this.y += Math.sign(key) * (Graphics.height / 2) * (Math.random() + 1);
		this.x += (Graphics.width - SURVIVOR_CONST.RIGHT_MARGIN) * (Math.random() - 0.5);
	}
	//その時のディスプレイ座標を記録
	this.dispX = $gameMap._parallaxX;
	this.dispY = $gameMap._parallaxY;

	this.kb = null;
	this.death = false;
	this.del = false;
	this.dm = {
		visible: false,
		dm: 0,
		duration: 0
	};

	//クラスよりこっちの方が見やすい
	var keys = Object.keys(mobData[name]);
	for (var i = 0, len = keys.length; i < len; i++) {
		this[keys[i]] = mobData[name][keys[i]];
	}

	this.reset = true;
};

MOB.prototype.setDamage = function (dm) {
	this.hp -= dm;
	this.dm.visible = true;
	this.dm.dm = dm;
	this.dm.duration = SURVIVOR_CONST.DAMAGE_DURATION;
};

//MobのSprite
function Sprite_Mob() {
	this.initialize(...arguments);
}

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

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

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

	this.data = data;
	this.t = 0;
	this.setCharacterBitmap();

	var sp = new Sprite_CBRDamage(this.data);
	this._damage = sp;
	this.addChild(sp);
};

//登場するモンスターは1つの画像にまとめよう、updateはフレームだけ
Sprite_Mob.prototype.update = function () {
	Sprite.prototype.update.call(this);

	if (this.data.del) {
		return;
	} else {
		this.visible = true;
	}

	this.updateCharacterFrame();
	this.updatePosition();

	//死んだらダメージもポジションもアップデートしない
	if (this.data.death) {
		//初期
		if (!this.data.deathFilter) {
			var filter = new PIXI.filters.breakFilter();
			filter.uniforms.key1 = 1; //粒々用乱数、ノイズの大きさ用  0.1だと少ない 0.9だと多い
			filter.uniforms.pxSize = Graphics.boxWidth / 1; //粒子のサイズ
			this._CBR_breakFilter = filter;
			if (!this.filters) {
				this.filters = [];
			}
			this.filters.push(filter);
			this.data.deathFilter = true;
		}

		var filter = this._CBR_breakFilter;
		filter.uniforms.key1 = this.data.deathCount / SURVIVOR_CONST.DEATH_COUNT; //粒々用乱数、ノイズの大きさ用

		//死んだ完了フラグが立ってたらdelフラグON
		if (this.data.deathCount <= 0) {
			this.data.del = true;
			this.visible = false;
			this.filters = [];
		} else {
			this.data.deathCount--;
		}
	}
	if (this.data.reset) {
		this.bitmap = ImageManager.loadCharacter(this.data.img);
		this.updateCharacterFrame();
		this.data.reset = false;
	}
};

Sprite_Mob.prototype.updatePosition = function () {
	this.x = this.data.x;
	this.y = this.data.y;
};

Sprite_Mob.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_Mob.prototype.setCharacterBitmap = function () {
	this.bitmap = ImageManager.loadCharacter(this.data.img);
};

Sprite_Mob.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;

	this.setFrame(sx, sy, pw, ph);
	this.t++;
};

Sprite_Mob.prototype.characterPatternX = function () {
	return Math.floor(Math.abs(((this.t / 10) % 5.99) - 5.99 / 2)) + (this.data.img_index % 4) * 3; //this._character.pattern();
};

Sprite_Mob.prototype.characterPatternY = function () {
	return Math.floor(this.data.img_index / 4);
};
Sprite_Mob.prototype.patternWidth = function () {
	return this.bitmap.width / 12;
};

Sprite_Mob.prototype.patternHeight = function () {
	return this.bitmap.height / 8;
};
Sprite_Mob.prototype.characterBlockX = function () {
	const index = 0; //this._character.characterIndex();
	return (index % 4) * 3;
};

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

//Mobのダメージ
function Sprite_CBRDamage() {
	this.initialize(...arguments);
}

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

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

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

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

	this.create();
};

Sprite_CBRDamage.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_CBRDamage.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_CBRDamage.prototype.update = function () {
	Sprite.prototype.update.call(this);

	var d = this.data.dm;
	if (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;
	}
};
