var CBR = CBR || {};

CBR.fish = {
	move: function (type) {
		switch (type) {
			case 1: //イワシ
				if ($gameMap.mapId() == 120) {
					return {
						x: 48 * 11 + Math.random() * 48 * 4,
						y: 48 * 15.2 + Math.random() * 48 * 7.5,
						size: 0.2,
						id: 1 //強さとか釣り難易度
					};
				} else {
					return {
						x: 48 * 4 + Math.random() * 48 * 20,
						y: 48 * 13 + Math.random() * 48 * 9,
						size: 0.2,
						id: 1 //強さとか釣り難易度
					};
				}
			case 2: //アジ
				if ($gameMap.mapId() == 120) {
					return {
						x: 48 * 11 + Math.random() * 48 * 4,
						y: 48 * 15.2 + Math.random() * 48 * 7.5,
						size: 0.2,
						id: 1
					};
				} else {
					return {
						x: 48 * 4 + Math.random() * 48 * 20,
						y: 48 * 13 + Math.random() * 48 * 9,
						size: 0.2,
						id: 1
					};
				}
			case 3: //サンマ
				if ($gameMap.mapId() == 120) {
					return {
						x: 48 * 11 + Math.random() * 48 * 4,
						y: 48 * 15.2 + Math.random() * 48 * 7.5,
						size: 0.2,
						id: 2
					};
				} else {
					return {
						x: 48 * 4 + Math.random() * 48 * 20,
						y: 48 * 13 + Math.random() * 48 * 9,
						size: 0.2,
						id: 2
					};
				}
			case 4: //サバ
				if ($gameMap.mapId() == 120) {
					return {
						x: 48 * 11 + Math.random() * 48 * 4,
						y: 48 * 15.2 + Math.random() * 48 * 7.5,
						size: 0.2,
						id: 2
					};
				} else {
					return {
						x: 48 * 4 + Math.random() * 48 * 20,
						y: 48 * 13 + Math.random() * 48 * 9,
						size: 0.2,
						id: 2
					};
				}
			case 5: //カレイ
				if ($gameMap.mapId() == 120) {
					return {
						x: 48 * 11 + Math.random() * 48 * 4,
						y: 48 * 15.2 + Math.random() * 48 * 7.5,
						size: 0.2,
						id: 3
					};
				} else {
					return {
						x: 48 * 4 + Math.random() * 48 * 20,
						y: 48 * 13 + Math.random() * 48 * 9,
						size: 0.2,
						id: 3
					};
				}
			case 6: //スズキ
				if ($gameMap.mapId() == 120) {
					return {
						x: 48 * 11 + Math.random() * 48 * 4,
						y: 48 * 15.2 + Math.random() * 48 * 7.5,
						size: 0.2,
						id: 5
					};
				} else {
					return {
						x: 48 * 4 + Math.random() * 48 * 20,
						y: 48 * 13 + Math.random() * 48 * 9,
						size: 0.2,
						id: 5
					};
				}
			case 7: //ヒラメ
				if ($gameMap.mapId() == 120) {
					return {
						x: 48 * 11 + Math.random() * 48 * 4,
						y: 48 * 15.2 + Math.random() * 48 * 7.5,
						size: 0.2,
						id: 5
					};
				} else {
					return {
						x: 48 * 4 + Math.random() * 48 * 20,
						y: 48 * 13 + Math.random() * 48 * 9,
						size: 0.2,
						id: 5
					};
				}
			case 8: //タイ
				if ($gameMap.mapId() == 120) {
					return {
						x: 48 * 11 + Math.random() * 48 * 4,
						y: 48 * 15.2 + Math.random() * 48 * 7.5,
						size: 0.2,
						id: 4
					};
				} else {
					return {
						x: 48 * 4 + Math.random() * 48 * 20,
						y: 48 * 13 + Math.random() * 48 * 9,
						size: 0.2,
						id: 4
					};
				}
		}
	}
};

//################ 釣りプラグイン ######################
//魚を設定
Game_Event.prototype.setImage = function (characterName, characterIndex) {
	this._tileId = 0;
	this._characterName = characterName;
	this._characterIndex = characterIndex;
	this._isObjectCharacter = ImageManager.isObjectCharacter(characterName);

	//初期設定
	if (characterName == "!$_fish" || characterName == "!$_tako") {
		this.CBR_fish = {};
		this.CBR_fish.speed = 120 + 60 * Math.random(); //120～180
		this.CBR_fish.rotation = 3.14 * 2 * Math.random();
		this.CBR_fish.nowRotation = this.CBR_fish.rotation;
		this.CBR_fish.targetRotation = this.CBR_fish.rotation;
		this.CBR_fish.duration = 0;

		this.CBR_fish.type = Number($dataMap.events[this._eventId].meta.type);
		this.CBR_fish.fishId = CBR.fish.move(this.CBR_fish.type).id;

		var p = CBR.fish.move(this.CBR_fish.type);
		this.CBR_fish.scale = p.size * (1 + Math.random() * 0.2);
		this.CBR_fish.nowX = p.x;
		this.CBR_fish.nowY = p.y;
		this.CBR_fish.targetX = this.CBR_fish.nowX; //目的地
		this.CBR_fish.targetY = this.CBR_fish.nowY;
	}
};

Sprite_Character.prototype.initialize = function (character) {
	Sprite.prototype.initialize.call(this);
	this.initMembers();
	this.setCharacter(character);

	if (this._character.CBR_fish) {
		this.scale.x = this._character.CBR_fish.scale;
		this.scale.y = this._character.CBR_fish.scale;
	}
};

//魚を動かす
Game_Event.prototype.update = function () {
	Game_Character.prototype.update.call(this);
	this.checkEventTriggerAuto();
	this.updateParallel();

	if (this.CBR_fish) {
		var d = this.CBR_fish;
		var uki = $gameMap._events[10];
		//食いつき中とバトル中は動かさない
		if (($gameSwitches.value(190) || $gameSwitches.value(191) || $gameSwitches.value(192)) && $gameVariables.value(166) == this._eventId) {
			return;
		}

		//新しく移動するよーって時
		if (d.speed < d.duration) {
			d.duration = 0;

			d.nowX = d.targetX;
			d.nowY = d.targetY;

			var p = CBR.fish.move(d.type);
			d.targetX = p.x; //目的地
			d.targetY = p.y;

			d.nowRotation = d.rotation;
			d.targetRotation = Math.atan2(d.targetY - d.nowY, d.targetX - d.nowX) - 3.14 / 2;

			var saX = d.nowX - uki._x * 48;
			var saY = d.nowY - uki._y * 48;
			var rang = Math.sqrt(saX * saX + saY * saY);
			//他の奴が食いついてない
			if ($gameSwitches.value(189) && !$gameSwitches.value(190)) {
				// map120だけ範囲制限
				if ($gameMap.mapId() != 120 || ($gameMap.mapId() == 120 && 11.5 < uki._x && uki._x < 15.5)) {
					//半径内かつ20%以上の確率
					if (rang < 100 && 0.7 < Math.random()) {
						//フィーッシュ！食いついた！
						$gameSwitches.setValue(189, false);
						$gameSwitches.setValue(190, true);
						$gameVariables.setValue(162, 100); //temp変数近付きカウント
						$gameVariables.setValue(166, this._eventId); //temp変数近付きカウント
					}
				}
			}
		}

		if (d.duration < d.speed / 3) {
			this._x = d.nowX + (d.targetX - d.nowX) * Math.sin((((3.14 / 2) * d.duration) / d.speed) * 3);
			this._y = d.nowY + (d.targetY - d.nowY) * Math.sin((((3.14 / 2) * d.duration) / d.speed) * 3);
		} else {
			this._x = d.targetX;
			this._y = d.targetY;
		}

		if (d.duration < d.speed / 9) {
			this.CBR_fish.rotation = d.nowRotation + (((d.targetRotation - d.nowRotation) * d.duration) / d.speed) * 9;
		}

		this._x /= 48;
		this._y /= 48;
		this._realX = this._x;
		this._realY = this._y;

		var saX = this._x - uki._x;
		var saY = this._y - uki._y;
		var rang = Math.sqrt(saX * saX + saY * saY);

		if (d.speed / 4 < d.duration) {
			// map120だけ範囲制限
			if ($gameMap.mapId() != 120 || ($gameMap.mapId() == 120 && 11.5 < uki._x && uki._x < 15.5)) {
				//魚にフキダシを表示
				if ($gameSwitches.value(189)) {
					if (rang * 48 < 100 && !this._balloonPlaying) {
						$gameTemp.requestBalloon(this, 1);
					}
				}
			}
		}

		d.duration++;
	}
};

//魚を回転させたり動かす
Sprite_Character.prototype.updatePosition = function () {
	if (this._character.CBR_fish) {
		this.rotation = this._character.CBR_fish.rotation;
	}
	this.x = this._character.screenX();
	this.y = this._character.screenY();
	this.z = this._character.screenZ();
};

//目的地の移動を中止する
Scene_Map.prototype.updateDestination = function () {
	if (this.isMapTouchOk() && !$gameSwitches.value(185)) {
		this.processMapTouch();
	} else {
		$gameTemp.clearDestination();
		this._touchCount = 0;
	}
};

//矢印中色々と禁止
/*
var Scene_Map___update = Scene_Map.prototype.update;
Scene_Map.prototype.update = function() {
    Scene_Message.prototype.update.call(this);
    this.updateDestination();
    this.updateMenuButton();
    this.updateMapNameWindow();
    this.updateMainMultiply();
	//釣り最中はメニュー開けないように
	if(!$gameSwitches.value(88)){
		if (this.isSceneChangeOk()) {
			this.updateScene();
		} else if (SceneManager.isNextScene(Scene_Battle)) {
			this.updateEncounterEffect();
		}
	}
    this.updateWaitCount();
};
*/

/*
document.addEventListener('mousemove', function(event) {
    // キャンバス上のマウス座標を取得
    const x = Graphics.pageToCanvasX(event.pageX);
    const y = Graphics.pageToCanvasY(event.pageY);

    // ログに表示する（開発ツールのコンソールで確認できます）
    console.log(`マウス座標: X: ${x}, Y: ${y}`);
});
*/

//矢印を移動させる
Scene_Map.prototype.updateMain = function () {
	$gameMap.update(this.isActive());
	if ($gameSwitches.value(185)) {
		var saoPower = 0.2;

		// 装備によって投げるパワーを変える
		if ($gameActors._data[1]._equips[0]._itemId == 2) {
			saoPower *= 2.5;
		}
		//################# 方向決める ####################
		if ($gameSwitches.value(186)) {
			//console.log(TouchInput._y);//中心 638 377

			var ang = $gameVariables.value(163);
			if (Input.isPressed("left")) {
				ang += 2;
				if (180 < ang) {
					ang = 180;
				}
				$gameVariables.setValue(163, ang); //ゲージの方向
				$gameScreen.picture(9)._angle = ang - 90;
			} else if (Input.isPressed("right")) {
				ang -= 2;
				if (ang < 0) {
					ang = 0;
				}
				$gameVariables.setValue(163, ang); //ゲージの方向
				$gameScreen.picture(9)._angle = ang - 90;
			} else if (Input.isTriggered("ok") || TouchInput.isTriggered()) {
				$gameSwitches.setValue(186, false);
				$gameSwitches.setValue(187, true); //ゲージの強さスタート
				$gameScreen.erasePicture(9);
				$gameScreen.showPicture(10, "_fish_guage_b", 0, 456, 10, 100, 100, 255, 0);
				$gameScreen.showPicture(11, "_fish_guage_a", 0, 456, 10, 100, 100, 255, 0);
				$gameScreen.picture(10).CBR_clip = { x: 0, y: 0, w: 0, h: 134 };
				AudioManager.playSe({ name: "Raise3", volume: 70, pitch: 100, pan: 0 });
				$gameScreen.erasePicture(14);
			} else if (TouchInput.isHovered()) {
				var mouseX = TouchInput._x - 638;
				var mouseY = TouchInput._y - 377;
				ang = (Math.atan2(-mouseX, mouseY) * 180) / 3.14 + 90;
				if (ang < 0) {
					ang = 0;
				} else if (180 < ang) {
					ang = 180;
				}

				$gameVariables.setValue(163, ang); //ゲージの方向
				$gameScreen.picture(9)._angle = ang - 90;
			}

			//################ 投げるパワー決める #################
		} else if ($gameSwitches.value(187)) {
			//10-363 = 363
			var power = $gameVariables.value(164);
			if (363 < power) {
				power = 10;
				AudioManager.playSe({ name: "Raise3", volume: 70, pitch: 100, pan: 0 });
			} else {
				power += 8;
			}
			$gameVariables.setValue(164, power); //ゲージの
			$gameScreen.picture(10).CBR_clip = { x: 0, y: 0, w: power, h: 134 };
			//投げる
			if (Input.isTriggered("ok") || TouchInput.isTriggered()) {
				$gameVariables.setValue(164, $gameVariables.value(164) * saoPower); //ボロの釣り竿は0.25倍

				$gameSwitches.setValue(193, true);
				$gameSwitches.setValue(187, false);
				$gameSwitches.setValue(188, true);
				$gameMap._events[10].setTransparent(false);

				var pic_name = $gameParty.members()[0].name() == "Shirayuki" ? `sira_${$gameVariables.value(204)}` : `yozu_${$gameVariables.value(205)}`;
				$gameScreen.showPicture(14, `turi_cutin_${pic_name}`, 0, 0, 0, 100, 100, 255, 0);

				$gameScreen.showPicture(12, "dummy", 0, 0, 0, 100, 100, 255, 0);
				AudioManager.playSe({ name: "Jump2", volume: 90, pitch: 100, pan: 0 });
			}
			//############# 投げてる最中 ##################
		} else if ($gameSwitches.value(188)) {
			var rad = ($gameVariables.value(163) / 180) * 3.14; //0-3.14
			var power = $gameVariables.value(164) / 363; //0~1
			var maxR = 2500;
			var homeX = $gamePlayer._x * 48;
			var homeY = $gamePlayer._y * 48;
			var t = $gameVariables.value(165);
			var castTime = 200;

			t += 4;

			if (t == 72) {
				$gameScreen.movePicture(14, 0, 200, 0, 100, 100, 0, 0, 30);
			}

			if (castTime < t) {
				t = castTime;
				$gameSwitches.setValue(188, false);
				$gameSwitches.setValue(189, true);
				$gameScreen.erasePicture(10);
				$gameScreen.erasePicture(11);
				$gameScreen.erasePicture(2);
			}

			var screenX = homeX + (maxR * power * Math.cos(rad) * t) / castTime;
			var screenY = homeY + (maxR * power * Math.sin(rad) * t) / castTime;
			screenX /= 48;
			screenY /= 48;

			$gameMap._events[10]._realX = screenX;
			$gameMap._events[10]._x = screenX;
			$gameMap._events[10]._realY = screenY;
			$gameMap._events[10]._y = screenY;

			if (screenX < 13) {
				screenX = 0;
			} else if ($dataMap.width - 14 < screenX) {
				screenX = $dataMap.width - 27;
			} else {
				screenX -= 13;
			}
			if (screenY < 7) {
				screenY = 0;
			} else if ($dataMap.height - 8 < screenY) {
				screenY = $dataMap.height - 15;
			} else {
				screenY -= 7;
			}

			$gameMap._displayX = screenX;
			$gameMap._displayY = screenY; //見える範囲の一番上のY座標

			//糸
			var b = SceneManager._scene._spriteset._pictureContainer.children[11].bitmap;
			if (b && b._loadingState == "loaded") {
				b._context = b._context || b.canvas.getContext("2d");
				b.clear();
				var c = b._context;
				c.beginPath();

				c.strokeStyle = "rgba(255,255,255,1)";
				c.lineWidth = 1;
				c.moveTo($gamePlayer.screenX(), $gamePlayer.screenY());
				c.lineTo($gameMap._events[10].scrolledX() * 48 + 24, $gameMap._events[10].scrolledY() * 48 + 36);
				c.stroke();
				c.closePath();
				SceneManager._scene._spriteset._pictureContainer.children[11]._refresh();
			}

			$gameVariables.setValue(165, t); //ゲージの方向
			//################ 釣り中 ####################
		} else if ($gameSwitches.value(189)) {
			//引く
			if (Input.isPressed("ok") || TouchInput.isLongPressed() || TouchInput.isTriggered()) {
				var rad = ($gameVariables.value(163) / 180) * 3.14; //0-3.14
				var power = $gameVariables.value(164) / 363; //0~1
				var maxR = 2500;
				var homeX = $gamePlayer._x * 48;
				var homeY = $gamePlayer._y * 48;
				var t = $gameVariables.value(165);
				var castTime = 60;

				t -= 0.2 / power; //引く速度をどの距離でも一定にする
				//終了フラグ
				if (t < 0.01) {
					//TODO 下に同じのあるけどまとめる時間ないから
					var screenX = homeX / 48;
					var screenY = homeY / 48;
					if (screenX < 13) {
						screenX = 0;
					} else if ($dataMap.width - 14 < screenX) {
						screenX = $dataMap.width - 27;
					} else {
						screenX -= 13;
					}
					if (screenY < 7) {
						screenY = 0;
					} else if ($dataMap.height - 8 < screenY) {
						screenY = $dataMap.height - 15;
					} else {
						screenY -= 7;
					}

					$gameMap._displayX = screenX;
					$gameMap._displayY = screenY; //見える範囲の一番上のY座標

					$gameSwitches.setValue(193, false);
					$gameSwitches.setValue(189, false);
					$gameScreen.erasePicture(12);
					$gameTemp.reserveCommonEvent(1183);

					$gameMap._events[10]._realX = 0;
					$gameMap._events[10]._x = 0;
					$gameMap._events[10]._realY = 0;
					$gameMap._events[10]._y = 0;
				} else {
					if (castTime < t) {
						t = castTime;
						$gameSwitches.setValue(188, false);
						$gameSwitches.setValue(189, true);
					}

					var screenX = homeX + (maxR * power * Math.cos(rad) * t) / castTime;
					var screenY = homeY + (maxR * power * Math.sin(rad) * t) / castTime;
					screenX /= 48;
					screenY /= 48;

					$gameMap._events[10]._realX = screenX;
					$gameMap._events[10]._x = screenX;
					$gameMap._events[10]._realY = screenY;
					$gameMap._events[10]._y = screenY;

					if (screenX < 13) {
						screenX = 0;
					} else if ($dataMap.width - 14 < screenX) {
						screenX = $dataMap.width - 27;
					} else {
						screenX -= 13;
					}
					if (screenY < 7) {
						screenY = 0;
					} else if ($dataMap.height - 8 < screenY) {
						screenY = $dataMap.height - 15;
					} else {
						screenY -= 7;
					}

					$gameMap._displayX = screenX;
					$gameMap._displayY = screenY; //見える範囲の一番上のY座標

					//糸
					var b = SceneManager._scene._spriteset._pictureContainer.children[11].bitmap;
					if (b && b._loadingState == "loaded") {
						b._context = b._context || b.canvas.getContext("2d");
						b.clear();
						var c = b._context;
						c.beginPath();

						c.strokeStyle = "rgba(255,255,255,1)";
						c.lineWidth = 1;
						c.moveTo($gamePlayer.screenX(), $gamePlayer.screenY());
						c.lineTo($gameMap._events[10].scrolledX() * 48 + 24, $gameMap._events[10].scrolledY() * 48 + 36);
						c.stroke();
						c.closePath();
						SceneManager._scene._spriteset._pictureContainer.children[11]._refresh();
					}
					$gameVariables.setValue(165, t); //ゲージの方向
				}
			}
			//################### 食いつき中 #################
		} else if ($gameSwitches.value(190)) {
			var t = $gameVariables.value(162);
			var id = $gameVariables.value(166);
			var ev = $gameMap._events[id];

			if (t) {
				ev._x += ($gameMap._events[10]._x - ev._x) / t;
				ev._y += ($gameMap._events[10]._y - 4 / 48 - ev._y) / t;

				ev._realX = ev._x;
				ev._realY = ev._y;

				ev.CBR_fish.rotation = Math.atan2($gameMap._events[10]._y - ev._y, $gameMap._events[10]._x - ev._x) - 3.14 / 2;

				t--;
				if (t < 0) {
					t = 0;
				}
				$gameVariables.setValue(162, t);
				//フィーッシュ！
			} else {
				//AudioManager.playBgs({"name":"mizushaba","volume":90,"pitch":100,"pan":0});
				$gameSwitches.setValue(193, false);
				$gameSwitches.setValue(190, false);
				$gameSwitches.setValue(191, true);
				$gameTemp.reserveCommonEvent(1180);
				ev._moveSpeed = 8;
				$gameVariables.setValue(162, null);
				$gameVariables.setValue(177, 500); //糸パワー
				$gameScreen.showPicture(13, "_fish_icon", 0, 0, 0, 100, 100, 255, 0);
			}
			//####################### 格闘中 #######################
		} else if ($gameSwitches.value(191)) {
			var id = $gameVariables.value(166);
			var ev = $gameMap._events[id];
			var bui = $gameMap._events[10];
			var obj = $gameVariables.value(162);
			if (!obj) {
				var rad = 2 * 3.14 * Math.random();
				var rang = 3 + 4 * Math.random();
				obj = {
					count: 100 + 100 * Math.random(),
					rad: rad, //向き
					rang: rang,
					targetX: ev._x + rang * Math.cos(rad),
					targetY: ev._y + rang * Math.sin(rad),
					power: 0.4, //0.2-1 引いた時どれだけ陸に近付けるか どんどん下がる
					string: 0.5, //0-1　範囲超えたら切れる
					scrollSpeedX: 0,
					scrollSpeedY: 0
				};
			}

			if (obj.count < 0) {
				obj.count = 40 + 70 * Math.random();
				obj.string += 0.4; //動く時少し糸ゲージの負担が増える
				obj.power -= 0.01;
				var rad = 2 * 3.14 * Math.random();
				var rang = 7 + 8 * Math.random();
				obj.targetX = ev._x + rang * Math.cos(rad);

				if (obj.targetX < 0) {
					obj.targetX += 8;
				} else if ($dataMap.width < obj.targetX) {
					obj.targetX -= 8;
				}
				obj.targetY = ev._y + (rang / 2) * Math.sin(rad) + 5;
				if ($dataMap.height < obj.targetY) {
					obj.targetY -= 10;
				}
				ev.CBR_fish.rotation = Math.atan2(obj.targetY - ev._y, obj.targetX - ev._x) - 3.14 / 2;
			}

			ev._x += (obj.targetX - ev._x) / obj.count;
			ev._y += (obj.targetY - ev._y) / obj.count;

			var homeX = $gamePlayer._x;
			var homeY = $gamePlayer._y;

			//引く
			var stringLife = $gameVariables.value(177); // 0 - 1000

			//ここで難易度を設定
			if (Input.isTriggered("ok") || TouchInput.isTriggered()) {
				var yowari = 0.01;
				//メダカは楽に
				if (ev.CBR_fish.fishId == 1) {
					yowari *= 3;
					//ナマズは結構難しく
				} else if (ev.CBR_fish.fishId == 3) {
					yowari *= 0.5;
					//ナイルパーチは結構難しく
				} else if (ev.CBR_fish.fishId == 4) {
					yowari *= 0.3;
					//タコも
				} else if (ev.CBR_fish.fishId == 5) {
					yowari *= 0.3;
				}
				obj.power -= yowari;
				if (obj.power < 0.04) {
					obj.power = 0.04;
				}
				var r = 5 / 48 / obj.power;
				var rad = Math.atan2(homeY - ev._y, homeX - ev._x) + 3.14;

				ev._y -= r * Math.sin(rad);
				obj.targetY -= r * Math.sin(rad);

				ev._x -= r * Math.cos(rad);
				obj.targetX -= r * Math.cos(rad);
				stringLife += 50; //糸の寿命
			}

			// x: 48 * 11 + Math.random() * 48 * 4,
			// y: 48 * 15.2 + Math.random() * 48 * 7.5,
			ev._x = ev._x.clamp(11, 11 + 4);
			ev._y = ev._y.clamp(3, 15.2 + 7.5);

			//イベントとブイの移動
			ev._realX = ev._x;
			ev._realY = ev._y;
			bui._x = ev._x;
			bui._y = ev._y;
			bui._realX = ev._x;
			bui._realY = ev._y;

			var screenX = ev._x;
			var screenY = ev._y;

			$gameMap._events[10]._realX = screenX;
			$gameMap._events[10]._x = screenX;
			$gameMap._events[10]._realY = screenY;
			$gameMap._events[10]._y = screenY;

			if (screenX < 13) {
				screenX = 0;
			} else if ($dataMap.width - 14 < screenX) {
				screenX = $dataMap.width - 27;
			} else {
				screenX -= 13;
			}
			if (screenY < 7) {
				screenY = 0;
			} else if ($dataMap.height - 8 < screenY) {
				screenY = $dataMap.height - 15;
			} else {
				screenY -= 7;
			}

			//スクロールスピード なんかブルブル震えるけどもういいや
			var saX = screenX - $gameMap._displayX;
			var saY = screenY - $gameMap._displayY;
			if (10 / 48 < saX) {
				obj.scrollSpeedX += 1 / 48;
				if (10 / 48 < obj.scrollSpeedX) {
					obj.scrollSpeedX = 10 / 48;
				}
				$gameMap._displayX += obj.scrollSpeedX;
			} else if (saX < -10 / 48) {
				obj.scrollSpeedX -= 1 / 48;
				if (obj.scrollSpeedX < -10 / 48) {
					obj.scrollSpeedX = -10 / 48;
				}
				$gameMap._displayX += obj.scrollSpeedX;
			} else {
				if (1 / 48 < obj.scrollSpeedX) {
					obj.scrollSpeedX -= 1 / 48;
				} else if (obj.scrollSpeedX < 1 / 48) {
					obj.scrollSpeedX += 1 / 48;
				}
			}

			if (10 / 48 < saY) {
				obj.scrollSpeedY += 1 / 48;
				if (10 / 48 < obj.scrollSpeedY) {
					obj.scrollSpeedY = 10 / 48;
				}
				$gameMap._displayY += obj.scrollSpeedY;
			} else if (saY < -10 / 48) {
				obj.scrollSpeedY -= 1 / 48;
				if (obj.scrollSpeedY < -10 / 48) {
					obj.scrollSpeedY = -10 / 48;
				}
				$gameMap._displayY += obj.scrollSpeedY;
			} else {
				if (1 / 48 < obj.scrollSpeedY) {
					obj.scrollSpeedY -= 1 / 48;
				} else if (obj.scrollSpeedY < 1 / 48) {
					obj.scrollSpeedY += 1 / 48;
				}
			}

			//何かの拍子でスクロールがは見出ないように
			if ($gameMap._displayX < 0) {
				$gameMap._displayX = 0;
			}
			if ($gameMap._displayY < 0) {
				$gameMap._displayY = 0;
			} else if ($dataMap.height - Graphics._height / 48 < $gameMap._displayY) {
				$gameMap._displayY = $dataMap.height - Graphics._height / 48;
			}

			//糸
			var b = SceneManager._scene._spriteset._pictureContainer.children[11].bitmap;
			if (b && b._loadingState == "loaded") {
				b._context = b._context || b.canvas.getContext("2d");
				b.clear();
				var c = b._context;
				c.beginPath();

				c.strokeStyle = "rgba(255,255,255,1)";
				c.lineWidth = 1;
				c.moveTo($gamePlayer.screenX(), $gamePlayer.screenY());
				c.lineTo($gameMap._events[10].scrolledX() * 48 + 24, $gameMap._events[10].scrolledY() * 48 + 36);
				c.stroke();
				c.closePath();
				SceneManager._scene._spriteset._pictureContainer.children[11]._refresh();
			}

			//お魚ゲージの色
			stringLife -= 3;

			//ナイルパーチは下がりにくく
			if (ev.CBR_fish.fishId == 4) {
				stringLife += 1;
			}

			var r = 0;
			var g = 0;
			var b = 0;
			if (stringLife <= 500) {
				b = 255 - (stringLife / 500) * 255;
				g = (stringLife / 500) * 255;
			} else {
				g = 255 - ((stringLife - 500) / 500) * 255;
				r = ((stringLife - 500) / 500) * 255;
			}
			$gameScreen.tintPicture(13, [r, g, b, 0], 0);

			obj.count--;
			$gameVariables.setValue(162, obj);

			$gameVariables.setValue(177, stringLife);

			//釣れたら
			if (Math.sqrt((homeX - ev._x) * (homeX - ev._x) + (homeY - ev._y) * (homeY - ev._y)) < 100 / 48) {
				var b = SceneManager._scene._spriteset._pictureContainer.children[11].bitmap;
				if (b && b._loadingState == "loaded") {
					b._context = b._context || b.canvas.getContext("2d");
					b.clear();
				}

				AudioManager.fadeOutBgs(0);
				$gameSwitches.setValue(191, false);
				$gameSwitches.setValue(192, true);
				AudioManager.playSe({ name: "", volume: 90, pitch: 150, pan: 0 });
				$gameVariables.setValue(162, 50);
				if (ev._characterName == "!$_fish") {
					ev.setImage("!$_fish_riku", 6);
				} else {
					ev.setImage("!$_fish_riku", 6);
				}
				ev.CBR_fish.rotation = 0;
				ev._moveSpeed = 3;
				AudioManager.playSe({ name: "Water1", volume: 90, pitch: 100, pan: 0 });
				$gameMap._events[10]._realX = 0;
				$gameMap._events[10]._x = 0;
				$gameMap._events[10]._realY = 0;
				$gameMap._events[10]._y = 0;
			}

			//糸が切れたら
			if (1000 < stringLife || stringLife < 0) {
				var b = SceneManager._scene._spriteset._pictureContainer.children[11].bitmap;
				if (b && b._loadingState == "loaded") {
					b._context = b._context || b.canvas.getContext("2d");
					b.clear();
				}

				AudioManager.fadeOutBgs(0);
				$gameSwitches.setValue(191, false);
				$gameSwitches.setValue(185, true);
				$gameVariables.setValue(162, 50);
				ev.CBR_fish.rotation = 0;
				AudioManager.playSe({ name: "Blow5", volume: 90, pitch: 100, pan: 0 });
				$gameMap._events[10]._realX = 0;
				$gameMap._events[10]._x = 0;
				$gameMap._events[10]._realY = 0;
				$gameMap._events[10]._y = 0;
			}

			// 釣れたので寄せ中
		} else if ($gameSwitches.value(192)) {
			var t = $gameVariables.value(162);

			var id = $gameVariables.value(166);
			var ev = $gameMap._events[id];

			if (0 < t) {
				var homeX = $gamePlayer._x;
				var homeY = $gamePlayer._y;

				ev._x += (homeX - ev._x) / t;
				ev._y += (homeY - ev._y) / t;

				//視点もここで治そう
				$gameMap._displayX += Math.max(0, $gamePlayer._x - 13) / t - $gameMap._displayX / t;
				$gameMap._displayY += Math.max(0, $gamePlayer._y - 7) / t - $gameMap._displayY / t;

				t--;
				$gameVariables.setValue(162, t);
			} else if (t == 0) {
				$gameVariables.setValue(161, ev.CBR_fish.fishId);
				t--;
				$gameVariables.setValue(162, t);
				$gameTemp.reserveCommonEvent(1181);
			}
			//逃げられた
		} else if ($gameSwitches.value(185)) {
			var t = $gameVariables.value(162);
			$gameMap._events[10]._realX = 0;
			$gameMap._events[10]._x = 0;
			$gameMap._events[10]._realY = 0;
			$gameMap._events[10]._y = 0;
			if (0 < t) {
				var homeX = $gamePlayer._x;
				var homeY = $gamePlayer._y;

				var id = $gameVariables.value(166);
				var ev = $gameMap._events[id];
				ev._moveSpeed = 3;
				ev._x += (homeX - ev._x) / t;
				ev._y += (homeY - ev._y) / t;

				//視点もここで治そう
				$gameMap._displayX += Math.max(0, $gamePlayer._x - 13) / t - $gameMap._displayX / t;
				$gameMap._displayY += Math.max(0, $gamePlayer._y - 7) / t - $gameMap._displayY / t;

				t--;
				$gameVariables.setValue(162, t);
			} else if (t == 0) {
				t--;
				$gameVariables.setValue(162, t);
				$gameTemp.reserveCommonEvent(1182);
			}
		}
	} else {
		//売春用
		if (!$gameSwitches.value(181)) {
			$gamePlayer.update(this.isPlayerActive());
		}
	}

	$gameTimer.update(this.isActive());
	$gameScreen.update();
};

//矢印の原点を指定
Sprite_Picture.prototype.updateOrigin = function () {
	const picture = this.picture();
	if (picture.origin() === 0) {
		this.anchor.x = 0;
		this.anchor.y = 0;
	} else {
		this.anchor.x = 0.5;
		this.anchor.y = 0.5;
	}
	if (picture._name == "_yaji" || picture._name == "_icon") {
		this.anchor.x = 0.5;
		this.anchor.y = 0;
	}
};

// 今は作業途中なのでコメントアウト
// //oneone1用フキダシ位置調整

const _Sprite_Balloon_updatePosition = Sprite_Balloon.prototype.updatePosition;
Sprite_Balloon.prototype.updatePosition = function () {
	_Sprite_Balloon_updatePosition.apply(this);

	if (this.targetObject._characterName == "!$_fish" || this.targetObject._characterName == "!$_tako") {
		this.x = this._target.x;
		this.y = this._target.y;
	}
};

// パワー決めるゲージ用
var Sprite_Picture_____update = Sprite_Picture.prototype.update;
Sprite_Picture.prototype.update = function () {
	Sprite_Picture_____update.call(this);
	this.updateBitmap();
	if (this.visible) {
		this.updateOrigin();
		this.updatePosition();
		this.updateScale();
		this.updateTone();
		this.updateOther();
		const picture = this.picture();
		if (picture.CBR_clip) {
			const data = picture.CBR_clip;
			this.setFrame(data.x, data.y, data.w, data.h);
		}
	}
};

(() => {
	"use strict";
	//フォーカスが外れてもゲームが動く
	SceneManager.isGameActive = function () {
		return true;
	};
})();
