#==============================================================================
#                   「月相システム」(ACE) Ver.1.0
#   製作者:奈々(なな)
#   へぷたなすくろーる http://heptanas.mamagoto.com/
#
#   ◇使用規約
#   使用される場合はスクリプト作成者として「奈々」を明記して下さい。
#   このスクリプトを改変したり、改変したものを配布するなどは自由ですが
#   その場合も元のスクリプトの作成者として名前は載せて下さい。
#   その他、詳しい利用規約はブログを参照して下さい。
#
#------------------------------------------------------------------------------
#
#   月の画像は、横に8つ並べた「Moon」というファイルを
#   GraphicsフォルダのSystemフォルダに入れます。(サイズは自由)
#   月だけでなく、時計や文字などを用意しても面白そうです。
#   
#   初期設定は、全て0を指定すると無効になります。
#   導入しただけだと、単に「月が満ち欠けするだけ」のスクリプトですが
#   結果を変数やスイッチに入れて、判定することができます。
#   
#   ダメージ計算式に変数を使い、月相に応じてダメージ変化
#   敵の戦闘行動にスイッチを条件にして、月相に応じた行動を取らせる
#   イベントで、満月でないと開かない扉を作るetc...
#   色々と工夫してみて下さい。
#   
#   またイベントコマンドのスクリプトから、月相を変化させられます。
#   「moon_phase(n)」と入れると、n番の月相になります。
#   nをマイナス、例えば-2とすれば、月相を2段階進めます。
#
#==============================================================================

#------------------------------------------------------------------------------
#◇初期設定
#------------------------------------------------------------------------------
module Nana
module MoonPhase
  
  #月相を格納する変数ID(イベントチェック用)
  #新月を0、満月を4とし、0,1,2,3,4,5,6,7と移行する
  PHASE_VAL = 1
  
  #月相を格納する変数ID(戦闘計算用)
  #新月を0、満月を100とし、0,25,50,75,100,75,50,25と移行する
  MOON_VAL = 2
  
  #月相に対応するスイッチID
  #新月から順に、対応するスイッチがONに、それ以外がOFFになる
  PHASE_SWS = [1, 2, 3, 4, 5, 6, 7, 8]

  #月相を一時的に非表示にするスイッチID
  NULL_SW = 9

  
  #マップ画面において、月相が変化する歩数
  WALK = 4
  
  #バトル画面において、月相が変化するターン数
  TURN = 1
  
  
  #月グラフィックの位置(マップ画面)
  # 0:非表示   1:左寄せ   2:中央寄せ  3:右寄せ
  MAP_POS = 1
  #XY座標の調整値
  MAP_AX = 2
  MAP_AY = 2
  
  #月グラフィックの位置(バトル画面)
  # 0:非表示   1:左寄せ   2:中央寄せ  3:右寄せ
  BATTLE_POS = 1
  #XY座標の調整値
  BATTLE_AX = 2
  BATTLE_AY = 2
  
  
end
end
#初期設定ここまで
#==============================================================================


#==============================================================================
# ■ Game_System
#------------------------------------------------------------------------------
#  システム周りのデータを扱うクラスです。セーブやメニューの禁止状態などを保存
# します。このクラスのインスタンスは $game_system で参照されます。
#==============================================================================

class Game_System
  #--------------------------------------------------------------------------
  # ● 公開インスタンス変数
  #--------------------------------------------------------------------------
  attr_accessor :moon_phase               # 月相
  attr_accessor :moon_count               # 月相カウント
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  alias moon_initialize initialize
  def initialize
    moon_initialize
    @moon_phase = 0
    @moon_count = 0
  end
  #--------------------------------------------------------------------------
  # ● 月相の変更
  #--------------------------------------------------------------------------
  def moon_phase=(moon_phase)
    @moon_phase = moon_phase
    
    @moon_phase = 0 if @moon_phase < 0
    while @moon_phase >= 8
      @moon_phase -= 8
    end
    
    if Nana::MoonPhase::PHASE_VAL > 0
      $game_variables[Nana::MoonPhase::PHASE_VAL] = @moon_phase
    end
    
    if Nana::MoonPhase::MOON_VAL > 0
      case @moon_phase
      when 0
        $game_variables[Nana::MoonPhase::MOON_VAL] = 0
      when 1, 7
        $game_variables[Nana::MoonPhase::MOON_VAL] = 25
      when 2, 6
        $game_variables[Nana::MoonPhase::MOON_VAL] = 50
      when 3, 5
        $game_variables[Nana::MoonPhase::MOON_VAL] = 75
      when 4
        $game_variables[Nana::MoonPhase::MOON_VAL] = 100
      end
    end
    
    Nana::MoonPhase::PHASE_SWS.each{|id| $game_switches[id] = false if id > 0 }
    sw = Nana::MoonPhase::PHASE_SWS[@moon_phase]
    $game_switches[sw] = true if sw > 0
    
  end
end

#==============================================================================
# ■ Spriteset_Map
#------------------------------------------------------------------------------
#  マップ画面のスプライトやタイルマップなどをまとめたクラスです。このクラスは
# Scene_Map クラスの内部で使用されます。
#==============================================================================

class Spriteset_Map
  #--------------------------------------------------------------------------
  # ● タイマースプライトの作成
  #--------------------------------------------------------------------------
  #initializeをaliasすると都合が悪いのでここに挿入
  alias moon_create_timer create_timer
  def create_timer
    moon_create_timer
    create_moon
  end
  #--------------------------------------------------------------------------
  # ● 月相の作成
  #--------------------------------------------------------------------------
  def create_moon
    @moon = Sprite_Moon.new(@viewport1)
    @moon.z = 500
  end
  #--------------------------------------------------------------------------
  # ● 解放
  #--------------------------------------------------------------------------
  alias moon_dispose dispose
  def dispose
    moon_dispose
    @moon.dispose
  end
  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  alias moon_update update
  def update
    moon_update
    @moon.update if @moon
  end
end

#==============================================================================
# ■ Spriteset_Battle
#------------------------------------------------------------------------------
#  バトル画面のスプライトをまとめたクラスです。このクラスは Scene_Battle クラ
# スの内部で使用されます。
#==============================================================================

class Spriteset_Battle
  #--------------------------------------------------------------------------
  # ● 戦闘背景(床)スプライトの作成
  #--------------------------------------------------------------------------
  alias moon_create_battleback1 create_battleback1
  def create_battleback1
    moon_create_battleback1
    create_moon
  end
  #--------------------------------------------------------------------------
  # ● 月相の作成
  #--------------------------------------------------------------------------
  def create_moon
    @moon = Sprite_Moon.new(@viewport1)
    @moon.z = 5
  end
  #--------------------------------------------------------------------------
  # ● 解放
  #--------------------------------------------------------------------------
  alias moon_dispose dispose
  def dispose
    moon_dispose
    @moon.dispose
  end
  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  alias moon_update update
  def update
    moon_update
    @moon.update if @moon
  end
end

#==============================================================================
# ■ Sprite_Moon
#------------------------------------------------------------------------------
#  月相のスプライト。
#==============================================================================

class Sprite_Moon < Sprite
  #--------------------------------------------------------------------------
  # ● オブジェクト初期化
  #--------------------------------------------------------------------------
  def initialize(viewport)
    super(viewport)
    pos = $game_party.in_battle ? Nana::MoonPhase::MAP_POS : Nana::MoonPhase::BATTLE_POS
    
    self.bitmap = pos == 0 ? Bitmap.new(0, 0) : Cache.system("Moon")
    phase = $game_system.moon_phase
    self.src_rect.set(phase*bitmap.width/8, 0, bitmap.width/8, bitmap.height)
    
    ax = $game_party.in_battle ? Nana::MoonPhase::MAP_AX : Nana::MoonPhase::BATTLE_AX
    ay = $game_party.in_battle ? Nana::MoonPhase::MAP_AY : Nana::MoonPhase::BATTLE_AY
    case pos
    when 1
      self.x = ax
      self.y = ay
    when 2
      self.x = (Graphics.width - self.width) / 2 + ax
      self.y = ay
    when 3
      self.x = Graphics.width - self.width + ax
      self.y = ay
    end
  end
  #--------------------------------------------------------------------------
  # ● フレーム更新
  #--------------------------------------------------------------------------
  def update
    super
    if $game_switches[Nana::MoonPhase::NULL_SW]
      self.visible = false
    else
      self.visible = true
    end
    
    phase = $game_system.moon_phase
    self.src_rect.set(phase*bitmap.width/8, 0, bitmap.width/8, bitmap.height)
  end
end

#==============================================================================
# ■ Scene_Battle
#------------------------------------------------------------------------------
#  バトル画面の処理を行うクラスです。
#==============================================================================

class Scene_Battle < Scene_Base
  #--------------------------------------------------------------------------
  # ● 開始処理
  #--------------------------------------------------------------------------
  alias moon_start start
  def start
    moon_start
    if Nana::MoonPhase::WALK > 0 && Nana::MoonPhase::TURN > 0
      $game_system.moon_count *= Nana::MoonPhase::TURN / Nana::MoonPhase::WALK
    end
  end
  #--------------------------------------------------------------------------
  # ● ターン終了
  #--------------------------------------------------------------------------
  alias moon_turn_end turn_end
  def turn_end
    if Nana::MoonPhase::TURN > 0
      $game_system.moon_count += 1
      if $game_system.moon_count >= Nana::MoonPhase::TURN
        $game_system.moon_count = 0
        $game_system.moon_phase += 1
      end
    end
    moon_turn_end
  end
  #--------------------------------------------------------------------------
  # ● 終了処理
  #--------------------------------------------------------------------------
  alias moon_terminate terminate
  def terminate
    if Nana::MoonPhase::WALK > 0 && Nana::MoonPhase::TURN > 0
      $game_system.moon_count *= Nana::MoonPhase::WALK / Nana::MoonPhase::TURN
    end
    moon_terminate
  end
end

#==============================================================================
# ■ Game_Player
#------------------------------------------------------------------------------
#  プレイヤーを扱うクラスです。イベントの起動判定や、マップのスクロールなどの
# 機能を持っています。このクラスのインスタンスは $game_player で参照されます。
#==============================================================================

class Game_Player < Game_Character
  #--------------------------------------------------------------------------
  # ● 歩数増加
  #--------------------------------------------------------------------------
  alias moon_increase_steps increase_steps
  def increase_steps
    moon_increase_steps
    
    if Nana::MoonPhase::WALK > 0
      $game_system.moon_count += 1
      if $game_system.moon_count >= Nana::MoonPhase::WALK
        $game_system.moon_count = 0
        $game_system.moon_phase += 1
      end
    end
  end
end

#==============================================================================
# ■ Game_Interpreter
#------------------------------------------------------------------------------
#  イベントコマンドを実行するインタプリタです。このクラスは Game_Map クラス、
# Game_Troop クラス、Game_Event クラスの内部で使用されます。
#==============================================================================

class Game_Interpreter
  #--------------------------------------------------------------------------
  # ● 月相の変更
  #--------------------------------------------------------------------------
  def moon_phase(phase)
    phase < 0 ? $game_system.moon_phase -= phase : $game_system.moon_phase = phase
    $game_system.moon_count = 0
  end
end