//=============================================================================
// RPG Maker MZ - IM_AvoidParallelTrigger
//
// Copyright 2025 min-pub. All rights reserved.
// This source code or any portion thereof must not be
// reproduced or used without licensed in any manner whatsoever.
//=============================================================================
/*:
 * @target MZ
 * @plugindesc 並列実行イベント回避用関数群
 * @author 四号戦車
 * @base PluginCommonBase
 * @base PluginUtils
 * @orderAfter PluginCommonBase
 * @orderAfter PluginUtils
 * 
 * @command catchPlayerMove
 * @text プレイヤー移動監視
 * @desc プレイヤーが指定の座標から動いたら
 *       指定のイベントを実行します
 * 
 * @arg playerX
 * @text プレイヤーX座標
 * @desc 0を指定すると呼び出し時のプレイヤー座標を使用します
 * @type number
 * @default 0
 * 
 * @arg playerY
 * @text プレイヤーY座標
 * @desc 0を指定すると呼び出し時のプレイヤー座標を使用します
 * @type number
 * @default 0
 * 
 * @arg event
 * @text 実行するテキスト
 * @type common_event
 * @defualt 0
 * 
 * 
 * @help IM_AvoidParallelTrigger.js
 *
 * Version: 0.0.1
 *
 * イベントのトリガーに並列実行をするのはダメ絶対。
 * ここに関数を作ってでも回避しましょう。
 * 
 */
(() => {
	"use strict";

    //========================================================================
    // プラグイン定義
    //========================================================================
    const pluginName = "IM_AvoidParallelTrigger";
    const script = document.currentScript;
    const param  = PluginManagerEx.createParameter(script);

    //========================================================================
    // 定数定義
    //========================================================================

    //========================================================================
    // コアスクリプト変更部 (Game_Player)
    //========================================================================
    const _Game_Player_prototype_executeMove = Game_Player.prototype.executeMove;
    Game_Player.prototype.executeMove = function(direction) {
        _Game_Player_prototype_executeMove.apply(this, arguments);
        if (!!$gameTemp.playerWatches && $gameTemp.playerWatches.length > 0) {
            const playerX = this.x;
            const playerY = this.y;
            const target = $gameTemp.playerWatches.find(trigger => {
                if (trigger.event === 0) {
                    return false;
                }
                return trigger.x !== playerX || trigger.y !== playerY;
            });

            if (!!target) {
                // 一度実行したトリガーは削除
                $gameTemp.playerWatches =  $gameTemp.playerWatches.filter(trigger => trigger !== target);
                PluginUtils.fireCommonEvent(target.event);
            }
        }
    };

    //========================================================================
    // プラグインコマンド
    //========================================================================
    PluginManagerEx.registerCommand(script, 'catchPlayerMove', args => {
        if (!$gameTemp.playerWatches) {
            $gameTemp.playerWatches = [];
        }

        if (args.event > 0) {
            const x = (args.playerX > 0 ) ? args.playerX : $gamePlayer.x;
            const y = (args.playerX > 0 ) ? args.playerY : $gamePlayer.y;

            $gameTemp.playerWatches.push({
                x: x,
                y: y,
                event: args.event,
            });
        }
    });

})();
