/*
 * --------------------------------------------------
 * MNKR_TMCloudDustMZ.js (スイッチ15がONのときのみ動作)
 *   Ver.1.0.1
 * Copyright (c) 2020 Munokura
 * This software is released under the MIT license.
 * http://opensource.org/licenses/mit-license.php
 * --------------------------------------------------
 */

var Imported = Imported || {};
Imported.TMCloudDust = true;
var TMPlugin = TMPlugin || {};

(() => {
  "use strict";

  const pluginName = document.currentScript.src.split("/").pop().replace(/\.js$/, "");
  TMPlugin.CloudDust = {};
  TMPlugin.CloudDust.Parameters = PluginManager.parameters(pluginName);
  TMPlugin.CloudDust.DustImage = TMPlugin.CloudDust.Parameters['dustImage'] || 'Dust1';
  TMPlugin.CloudDust.MaxDusts = +(TMPlugin.CloudDust.Parameters['maxDusts'] || 64);
  TMPlugin.CloudDust.JumpDusts = +(TMPlugin.CloudDust.Parameters['jumpDusts'] || 5);
  TMPlugin.CloudDust.DashDusts = +(TMPlugin.CloudDust.Parameters['dashDusts'] || 3);

  function Game_CloudDust() {
    this.initialize.apply(this, arguments);
  }

  Game_Map.prototype.addCloudDust = function (x, y, speed, radian) {
    if (!$gameSwitches.value(15)) return; // スイッチ15がOFFなら処理しない
    if (!$gameSystem.isDustEnabled()) return;
    for (let i = 0; i < TMPlugin.CloudDust.MaxDusts; i++) {
      if (!this._cloudDusts[i].exists()) {
        this._cloudDusts[i].setup(x, y, speed, radian);
        break;
      }
    }
  };

  Game_CharacterBase.prototype.addCloudDust = function (speed, radian) {
    if (!$gameSwitches.value(15)) return; // スイッチ15がOFFなら処理しない
    $gameMap.addCloudDust(this._realX + 0.5, this._realY + 1.0, speed, radian);
  };

  Game_Player.prototype.moveStraight = function (d) {
    if (!$gameSwitches.value(15)) { // スイッチ15がOFFなら処理しない
      _Game_Player_moveStraight.call(this, d);
      return;
    }
    let n = $gameSystem.dashDusts();
    let radian;
    if (n > 0) {
      if (this.isDashing() && this.canPass(this.x, this.y, d)) {
        if (d === 2) {
          radian = Math.PI * 1.5;
        } else if (d === 4) {
          radian = 0;
        } else if (d === 6) {
          radian = Math.PI;
        } else {
          radian = Math.PI / 2;
        }
        for (let i = 0; i < n; i++) {
          this.addCloudDust(0.03, radian);
        }
      }
    }
    _Game_Player_moveStraight.call(this, d);
  };

  Game_CharacterBase.prototype.updateJump = function () {
    _Game_CharacterBase_updateJump.call(this);
    if (!$gameSwitches.value(15)) return; // スイッチ15がOFFなら処理しない
    if (this._jumpCount === 0) {
      for (let i = 0; i < $gameSystem.jumpDusts(); i++) {
        this.addCloudDust(0.02, i % 2 * Math.PI);
      }
    }
  };

  PluginManager.registerCommand(pluginName, "setDustXy", args => {
    if (!$gameSwitches.value(15)) return; // スイッチ15がOFFなら処理しない
    const arr = [args.setX, args.setY, args.dusts, args.speed, args.direction];
    let n = parseInt(arr[2] || 1);
    for (let i = 0; i < n; i++) {
      $gameMap.addCloudDust(arr[0], arr[1], arr[3], arr[4]);
    }
  });

  PluginManager.registerCommand(pluginName, "setDustEvent", function (args) {
    if (!$gameSwitches.value(15)) return; // スイッチ15がOFFなら処理しない
    const arr = [args.EventId, args.dusts, args.speed, args.direction];
    let character = this.character(+arr[0]);
    if (character) {
      let n = parseInt(arr[1] || 1);
      for (let i = 0; i < n; i++) {
        character.addCloudDust(arr[2], arr[3]);
      }
    }
  });

  PluginManager.registerCommand(pluginName, "setJumpDusts", args => {
    if (!$gameSwitches.value(15)) return; // スイッチ15がOFFなら処理しない
    const arr = [args.dusts];
    $gameSystem.setJumpDusts(+arr[0]);
  });

  PluginManager.registerCommand(pluginName, "setDashDusts", args => {
    if (!$gameSwitches.value(15)) return; // スイッチ15がOFFなら処理しない
    const arr = [args.dusts];
    $gameSystem.setDashDusts(+arr[0]);
  });

  PluginManager.registerCommand(pluginName, "stopDust", args => {
    if (!$gameSwitches.value(15)) return; // スイッチ15がOFFなら処理しない
    $gameSystem.disableDust();
  });

  PluginManager.registerCommand(pluginName, "startDust", args => {
    if (!$gameSwitches.value(15)) return; // スイッチ15がOFFなら処理しない
    $gameSystem.enableDust();
  });

  function Sprite_CloudDust() {
    this.initialize.apply(this, arguments);
  }

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

  Sprite_CloudDust.prototype.initialize = function (cloudDust) {
    Sprite.prototype.initialize.call(this);
    this._cloudDust = cloudDust;
    this.scale = this._cloudDust.scale();
    this.visible = false;
    this.createBitmap();
  };

  Sprite_CloudDust.prototype.update = function () {
    Sprite.prototype.update.call(this);
    if (!$gameSwitches.value(15)) { 
      this.visible = false;
      return;
    }
    if (this._cloudDust.exists()) {
      this.visible = true;
      this.x = this._cloudDust.screenX();
      this.y = this._cloudDust.screenY();
      this.opacity = this._cloudDust.opacity();
      this.rotation = this._cloudDust.rotation();
    } else {
      this.visible = false;
    }
  };

  Sprite_CloudDust.prototype.createBitmap = function () {
    this.bitmap = ImageManager.loadSystem(TMPlugin.CloudDust.DustImage);
    this.anchor.x = 0.5;
    this.anchor.y = 0.5;
    this.z = 3;
  };

  const _Spriteset_Map_createLowerLayer = Spriteset_Map.prototype.createLowerLayer;
  Spriteset_Map.prototype.createLowerLayer = function () {
    _Spriteset_Map_createLowerLayer.call(this);
    this.createCloudDust();
  };

  Spriteset_Map.prototype.createCloudDust = function () {
    this._cloudDustSprites = [];
    $gameMap.cloudDusts().forEach(function (cloudDust) {
      this._cloudDustSprites.push(new Sprite_CloudDust(cloudDust));
    }, this);
    for (let i = 0; i < this._cloudDustSprites.length; i++) {
      this._tilemap.addChild(this._cloudDustSprites[i]);
    }
  };

})();
