/*:
 * @target MZ
 * @plugindesc ピクチャを揺らすスクリプト
 * @author
 *
 * @help $gameScreen.startPicShake(ピクチャ番号, 強さ, スピード, フレーム);
 * で1, 4, 5, 6, 7, 8番のピクチャを揺らす
 *
 */

(function () {
	var Sprite_Picture_updatePosition = Sprite_Picture.prototype.updatePosition;
	Sprite_Picture.prototype.updatePosition = function () {
		Sprite_Picture_updatePosition.call(this);

		var ary = [1, 4, 5, 6, 7, 8, 9, 10];
		var idx = ary.indexOf(this._pictureId);

		if (idx != -1) {
			this.x += $gameScreen.shakePic(idx);
		}
	};

	var Game_Screen_clear = Game_Screen.prototype.clear;
	Game_Screen.prototype.clear = function () {
		Game_Screen_clear.call(this);

		var ary = [1, 4, 5, 6, 7, 8, 9, 10];
		for (var i = 0; i < ary.length; i++) {
			this.clearPicShake(i);
		}
	};

	var Game_Screen_update = Game_Screen.prototype.update;
	Game_Screen.prototype.update = function () {
		Game_Screen_update.call(this);

		this.updatePicShake();
	};

	Game_Screen.prototype.startPicShake = function (num, power, speed, duration) {
		this._shakePicPower = this._shakePicPower || [];
		this._shakePicSpeed = this._shakePicSpeed || [];
		this._shakePicDuration = this._shakePicDuration || [];
		this._shakePicDirection = this._shakePicDirection || [];
		this._shakePic = this._shakePic || [];

		var ary = [1, 4, 5, 6, 7, 8, 9, 10];
		var idx = ary.indexOf(num);

		this._shakePic[idx] = 1;
		this._shakePicPower[idx] = power;
		this._shakePicSpeed[idx] = speed;
		this._shakePicDuration[idx] = duration;
		this._shakePicDirection[idx] = 1;
	};

	Game_Screen.prototype.clearPicShake = function (idx) {
		this._shakePicPower = this._shakePicPower || [];
		this._shakePicSpeed = this._shakePicSpeed || [];
		this._shakePicDuration = this._shakePicDuration || [];
		this._shakePicDirection = this._shakePicDirection || [];
		this._shakePic = this._shakePic || [];

		this._shakePicPower[idx] = 0;
		this._shakePicSpeed[idx] = 0;
		this._shakePicDuration[idx] = 0;

		this._shakePicDirection[idx] = 1;
		this._shakePic[idx] = 0;
	};

	Game_Screen.prototype.shakePic = function (idx) {
		return this._shakePic[idx] || 0;
	};

	Game_Screen.prototype.updatePicShake = function () {
		var ary = [1, 4, 5, 6, 7, 8, 9, 10];
		for (var i = 0; i < ary.length; i++) {
			if (this._shakePicDuration && this._shakePicDuration[i] > 0 && this._shakePic[i] !== 0) {
				const delta = (this._shakePicPower[i] * this._shakePicSpeed[i] * this._shakePicDirection[i]) / 10;
				if (this._shakePicDuration[i] <= 1 && this._shakePic[i] * (this._shakePic[i] + delta) < 0) {
					this._shakePic[i] = 0;
				} else {
					this._shakePic[i] += delta;
				}
				if (this._shakePic[i] > this._shakePicPower[i] * 2) {
					this._shakePicDirection[i] = -1;
				}
				if (this._shakePic[i] < -this._shakePicPower[i] * 2) {
					this._shakePicDirection[i] = 1;
				}
				this._shakePicDuration[i]--;

				if (this._shakePicDuration[i] < 1) {
					this._shakePic[i] = 0;
				}
			}
		}
	};
})();
