# Skrypt by KGC edited by Ozzma
# www.ultimateam.pl

module KGC
 module Dash_8DirMove
  # 8 kierunkowe chodzenie true/false
  ENABLE_8DIR = true
 
  # 8 klatkowa animacja true/false
  ENABLE_8DIR_ANIMATION = true

  # Szybkość chodzenia
  DEFAULT_WALK_SPEED = 4
 
  # Szybkość podejścia
  DASH_SPEED_RATE    = 3
 end
end
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
#------------------------------------------------------------------------------
$imported = {} if $imported == nil
$imported["Dash_8DirMove"] = true

module KGC::Dash_8DirMove
 
  SLANT_SUFFIX = "#"
end

module KGC
 module Commands
  module_function
 
  def reset_walk_speed
    $game_player.reset_move_speed
  end
 
  def set_walk_speed(value)
    $game_system.temp_walk_speed = value
  end
 
  def set_dash_speed(value)
    $game_system.temp_dash_speed = value
  end
 end
end

class Game_Interpreter
  include KGC::Commands
end

class Game_System
 
  attr_accessor :temp_dash_speed         
  attr_accessor :temp_walk_speed         
 
  alias initialize_KGC_Dash_8DirMove initialize
  def initialize
    initialize_KGC_Dash_8DirMove
    @temp_dash_speed = nil
    @temp_walk_speed = nil
  end 
end
# www.ultimateam.pl
class Game_Party < Game_Unit
 
  def decrease_steps
    @steps -= 1
  end
end

class Game_Character
 
  def direction_8dir
    return @direction
  end
 
  alias set_direction_KGC_Dash_8DirMove set_direction
  def set_direction(direction)
    last_dir = @direction

    set_direction_KGC_Dash_8DirMove(direction)

    if !@direction_fix && direction != 0
      @direction_8dir = direction
    end
  end
 
  alias move_lower_left_KGC_Dash_8DirMove move_lower_left
  def move_lower_left
    move_lower_left_KGC_Dash_8DirMove

    @direction_8dir = 1 unless @direction_fix
  end
 
  alias move_lower_right_KGC_Dash_8DirMove move_lower_right
  def move_lower_right
    move_lower_right_KGC_Dash_8DirMove

    @direction_8dir = 3 unless @direction_fix
  end
 
  alias move_upper_left_KGC_Dash_8DirMove move_upper_left
  def move_upper_left
    move_upper_left_KGC_Dash_8DirMove

    @direction_8dir = 7 unless @direction_fix
  end
 
  alias move_upper_right_KGC_Dash_8DirMove move_upper_right
  def move_upper_right
    move_upper_right_KGC_Dash_8DirMove

    @direction_8dir = 9 unless @direction_fix
  end
end

class Game_Player < Game_Character
 
  alias initialize_KGC_Dash_8DirMove initialize
  def initialize
    initialize_KGC_Dash_8DirMove

    reset_move_speed
  end
 
  def direction_8dir
    @direction_8dir = @direction if @direction_8dir == nil
    return @direction_8dir
  end
 
  def reset_move_speed
    @move_speed = KGC::Dash_8DirMove::DEFAULT_WALK_SPEED
  end
# www.ultimateam.pl
if KGC::Dash_8DirMove::ENABLE_8DIR
 
  def move_by_input
    return unless movable?
    return if $game_map.interpreter.running?

    if @reserved_move != nil
      case @reserved_move
      when :down
        move_down if passable_l?(2)
      when :left
        move_left if passable_l?(4)
      when :right
        move_right if passable_l?(6)
      when :up
        move_up if passable_l?(8)
      end
      @reserved_move = nil
      return
    end

    last_steps = $game_party.steps

    case Input.dir8
    when 1
      if !passable_l?(2) && passable_l?(4)
       
        move_left
        @reserved_move = :down
      elsif passable_l?(2) && !passable_l?(4)
       
        move_down
        @reserved_move = :left
      elsif passable_l?(2)
       
        move_down
        move_left
      end
      @direction = 2
    when 2
      move_down
    when 3
      if !passable_l?(2) && passable_l?(6)
       
        move_right
        @reserved_move = :down
      elsif passable_l?(2) && !passable_l?(6)
       
        move_down
        @reserved_move = :right
      elsif passable_l?(2)
       
        move_down
        move_right
      end
      @direction = 6
    when 4
      move_left
    when 6
      move_right
    when 7
      if !passable_l?(8) && passable_l?(4)
       
        move_left
        @reserved_move = :up
      elsif passable_l?(8) && !passable_l?(4)
       
        move_up
        @reserved_move = :left
      elsif passable_l?(8)
       
        move_up
        move_left
      end
      @direction = 4
    when 8
      move_up
    when 9
      if !passable_l?(8) && passable_l?(6)
       
        move_right
        @reserved_move = :up
      elsif passable_l?(8) && !passable_l?(6)
       
        move_up
        @reserved_move = :right
      elsif passable_l?(8)
       
        move_up
        move_right
      end
      @direction = 8
    else
      return
    end
    @direction_8dir = Input.dir8

    if $game_party.steps - last_steps == 2
      $game_party.decrease_steps
    end
  end

  def passable_l?(d)
    if $imported["TilesetExtension"]
      return passable?(@x, @y, d)
    else
      case d
      when 1
        return passable?(@x-1, @y+1)
      when 2
        return passable?(@x, @y+1)
      when 3
        return passable?(@x+1, @y+1)
      when 4
        return passable?(@x-1, @y)
      when 6
        return passable?(@x+1, @y)
      when 7
        return passable?(@x-1, @y-1)
      when 8
        return passable?(@x, @y-1)
      when 9
        return passable?(@x+1, @y-1)
      end
    end
    return false
  end
end 

  def update_move
    distance = 2 ** @move_speed   
    if dash?                     
      distance *= KGC::Dash_8DirMove::DASH_SPEED_RATE
     
      if $game_system.temp_dash_speed == nil
        @move_speed = KGC::Dash_8DirMove::DASH_SPEED_RATE
      else
        @move_speed = $game_system.temp_dash_speed
      end
    else
      if $game_system.temp_walk_speed == nil
         @move_speed = KGC::Dash_8DirMove::DEFAULT_WALK_SPEED
      else
         @move_speed = $game_system.temp_walk_speed
       end
     
    end
    distance = Integer(distance)

    @real_x = [@real_x - distance, @x * 256].max if @x * 256 < @real_x
    @real_x = [@real_x + distance, @x * 256].min if @x * 256 > @real_x
    @real_y = [@real_y - distance, @y * 256].max if @y * 256 < @real_y
    @real_y = [@real_y + distance, @y * 256].min if @y * 256 > @real_y
    update_bush_depth unless moving?
    if @walk_anime
      @anime_count += 1.5
    elsif @step_anime
      @anime_count += 1
    end
  end
end

if KGC::Dash_8DirMove::ENABLE_8DIR_ANIMATION

class Sprite_Character < Sprite_Base
 
  SLANT_ANIME_TABLE = { 1=>2, 3=>6, 7=>4, 9=>8 }
 
  alias update_bitmap_KGC_Dash_8DirMove update_bitmap
  def update_bitmap
    name_changed = (@character_name != @character.character_name)

    update_bitmap_KGC_Dash_8DirMove

    if @tile_id > 0             
      @enable_slant = false
      return
    end
    return unless name_changed

    @enable_slant = true
    begin
      @character_name_slant = @character_name + KGC::Dash_8DirMove::SLANT_SUFFIX
      Cache.character(@character_name_slant)
    rescue
      @enable_slant = false
    end
  end
 
  alias update_src_rect_KGC_Dash_8DirMove update_src_rect
  def update_src_rect
    return if @tile_id > 0

    if @enable_slant
      update_src_rect_for_slant
    else
      update_src_rect_KGC_Dash_8DirMove
    end
  end
 
  def update_src_rect_for_slant
    index = @character.character_index
    pattern = @character.pattern < 3 ? @character.pattern : 1
    sx = (index % 4 * 3 + pattern) * @cw

    dir = @character.direction_8dir
    case dir % 2
    when 0 
      if @last_slant
        self.bitmap = Cache.character(@character_name)
        @last_slant = false
      end
    else   
      unless @last_slant
        self.bitmap = Cache.character(@character_name_slant)
        @last_slant = true
      end
     
      dir = SLANT_ANIME_TABLE[dir]
    end

    sy = (index / 4 * 4 + (dir - 2) / 2) * @ch
    self.src_rect.set(sx, sy, @cw, @ch)
  end
end
end 
# www.ultimateam.pl