Guest User

Untitled

a guest
Jun 21st, 2014
178
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module MoveMod
  2.   def self.int_pow_of_two?(candidate)
  3.     raise "Integer required!" unless candidate.is_a?(Integer)
  4.     candidate > 0 && (candidate & (candidate - 1)) == 0
  5.   end
  6. end
  7.  
  8. module MoveMod::Diag
  9.   # Prefered facing switch when moving diagonally
  10.   # true: when moving diagonal use the horizontal direction
  11.   # false: when moving diagonal use the vertical direction
  12.   PREF_HORZ_DIR = false
  13. end
  14.  
  15. module MoveMod::GridExt
  16.   # The movement grid cell size
  17.   CELL_SIZE = 16                   # MUST BE 4, 8, 16
  18.   # The half movement gris cell size
  19.   HALF_CELL_SIZE = CELL_SIZE / 2  # Do not touch
  20.   # Multiplier
  21.   MUL = 32 / CELL_SIZE            # Do not touch
  22.   # Half Multiplier
  23.   HALF_MUL = MUL / 2              # Do not touch
  24.  
  25.   def self.scale_grid_size_up(a)
  26.     a * MUL
  27.   end
  28.  
  29.   def self.scale_grid_size_down(a)
  30.     a / MUL
  31.   end
  32.  
  33.   def self.scale_grid_pos_up(a)
  34.     a * MUL + HALF_MUL
  35.   end
  36.  
  37.   def self.scale_grid_pos_down(a)
  38.     a / MUL
  39.   end
  40. end
  41.  
  42. class Game_Map
  43.   attr_reader :grid_ext_width
  44.   attr_reader :grid_ext_height
  45.  
  46.   #######################
  47.   # Overwritten Methods #
  48.   #######################
  49.  
  50.   # Movement Grid Ext:
  51.   #   Initialize the movement grid width/height with 0, it is used before a map
  52.   #   is loaded
  53.   alias :Game_Map__initialize :initialize
  54.   def initialize
  55.     @grid_ext_width = 0
  56.     @grid_ext_height = 0
  57.  
  58.     Game_Map__initialize()
  59.   end
  60.  
  61.   # Movement Grid Ext:
  62.   #   Initialize the movement grid size by scalling the map size up
  63.   alias :Game_Map__setup :setup
  64.   def setup(map_id)
  65.     Game_Map__setup(map_id)
  66.  
  67.     @grid_ext_width = MoveMod::GridExt.scale_grid_size_up(@map.width)
  68.     @grid_ext_height = MoveMod::GridExt.scale_grid_size_up(@map.height)
  69.   end
  70.  
  71.   ###############
  72.   # New Methods #
  73.   ###############
  74.  
  75.   def screen_tile_grid_ext_x
  76.     Graphics.width / MoveMod::GridExt::CELL_SIZE
  77.   end
  78.  
  79.   def screen_tile_grid_ext_y
  80.     Graphics.height / MoveMod::GridExt::CELL_SIZE
  81.   end
  82.  
  83.   def display_grid_ext_x
  84.     display_x * MoveMod::GridExt::MUL
  85.   end
  86.  
  87.   def display_grid_ext_y
  88.     display_y * MoveMod::GridExt::MUL
  89.   end
  90.  
  91.   def adjust_grid_ext_x(x)
  92.     if loop_horizontal? && x < display_grid_ext_x - (@grid_ext_width - screen_tile_grid_ext_x) / 2
  93.       x - display_grid_ext_x + @grid_ext_width
  94.     else
  95.       x - display_grid_ext_x
  96.     end
  97.   end
  98.  
  99.   def adjust_grid_ext_y(y)
  100.     if loop_vertical? && y < display_grid_ext_y - (@grid_ext_height - screen_tile_grid_ext_y) / 2
  101.       y - display_grid_ext_y + @grid_ext_height
  102.     else
  103.       y - display_grid_ext_y
  104.     end
  105.   end
  106.  
  107.   def round_grid_ext_x(x)
  108.     loop_horizontal? ? (x + @grid_ext_width) % @grid_ext_width : x
  109.   end
  110.  
  111.   def round_grid_ext_y(y)
  112.     loop_vertical? ? (y + @grid_ext_height) % @grid_ext_height : y
  113.   end
  114.  
  115.   def round_grid_ext_x_with_direction(x, d)
  116.     round_grid_ext_x(x + (d == 6 ? 1 : d == 4 ? -1 : 0))
  117.   end
  118.  
  119.   def round_grid_ext_y_with_direction(y, d)
  120.     round_grid_ext_y(y + (d == 2 ? 1 : d == 8 ? -1 : 0))
  121.   end
  122. end
  123.  
  124. class Game_CharacterBase
  125.   attr_reader :grid_ext_x
  126.   attr_reader :grid_ext_y
  127.   attr_reader :grid_ext_real_x
  128.   attr_reader :grid_ext_real_y
  129.   attr_reader :direction
  130.  
  131.   #######################
  132.   # Overwritten Methods #
  133.   #######################
  134.  
  135.   # Movement Grid Ext:
  136.   #   Initialize movement grid position
  137.   alias :Game_Map__init_public_members :init_public_members
  138.   def init_public_members
  139.     @initial_moved = false
  140.     @grid_ext_x = 0
  141.     @grid_ext_y = 0
  142.     @grid_ext_real_x = 0
  143.     @grid_ext_real_y = 0
  144.  
  145.     Game_Map__init_public_members()
  146.   end
  147.  
  148.   # Movement Grid Ext:
  149.   #     Check movement grid position instead of map position
  150.   alias :Game_CharacterBase__moving? :moving?
  151.   def moving?
  152.     @grid_ext_real_x != @grid_ext_x || @grid_ext_real_y != @grid_ext_y
  153.   end
  154.  
  155.   # Diaginal Movement:
  156.   #   Change logic to require both directions to be passable. This prevents
  157.   #   cutting corners of non passable tiles.
  158.   alias :Game_CharacterBase__diagonal_passable? :diagonal_passable?
  159.   def diagonal_passable?(x, y, horz, vert)
  160.     x2 = $game_map.round_x_with_direction(x, horz)
  161.     y2 = $game_map.round_y_with_direction(y, vert)
  162.     (passable?(x, y, vert) && passable?(x, y2, horz)) &&
  163.     (passable?(x, y, horz) && passable?(x2, y, vert))
  164.   end
  165.  
  166.   # Movement Grid Ext:
  167.   #   Added assignment of movement grid ext position
  168.   alias :Game_CharacterBase__moveto :moveto
  169.   def moveto(x, y)
  170.     x_same = (@initial_moved && MoveMod::GridExt.scale_grid_pos_down(@grid_ext_x) == @x)
  171.     y_same = (@initial_moved && MoveMod::GridExt.scale_grid_pos_down(@grid_ext_y) == @y)
  172.  
  173.     Game_CharacterBase__moveto(x, y)
  174.  
  175.     if !x_same
  176.       @grid_ext_x = MoveMod::GridExt.scale_grid_pos_up(@x)
  177.       @grid_ext_real_x = @grid_ext_x
  178.     end
  179.  
  180.     if !y_same
  181.       @grid_ext_y = MoveMod::GridExt.scale_grid_pos_up(@y)
  182.       @grid_ext_real_y = @grid_ext_y
  183.     end
  184.    
  185.     @initial_moved = true
  186.   end
  187.  
  188.   # Movement Grid Ext:
  189.   #   Use movement grid position instead of map position so we can display
  190.   #   characters with smaller than 32 steps. We don't add the half cell offset.
  191.   #   That gives visually the effect that the character is aligned to the
  192.   #   original grid.
  193.   def screen_x
  194.     $game_map.adjust_grid_ext_x(@grid_ext_real_x) * MoveMod::GridExt::CELL_SIZE + (self.is_a?(Game_Player) || self.is_a?(Game_Follower) ? MoveMod::GridExt::HALF_CELL_SIZE : 0)
  195.   end
  196.  
  197.   # Movement Grid Ext:
  198.   #   Use movement grid position instead of map position so we can display
  199.   #   characters with smaller than 32 steps
  200.   def screen_y
  201.     $game_map.adjust_grid_ext_y(@grid_ext_real_y) * MoveMod::GridExt::CELL_SIZE + (MoveMod::GridExt::HALF_MUL * MoveMod::GridExt::CELL_SIZE) - shift_y - jump_height
  202.     # + (MoveMod::GridExt::CELL_SIZE * (MoveMod::GridExt::MUL))
  203.   end
  204.  
  205.   # Movement Grid Ext:
  206.   #   Added movement grid position update
  207.   alias :Game_CharacterBase__update_move :update_move
  208.   def update_move
  209.     @grid_ext_real_x = [@grid_ext_real_x - distance_per_frame * MoveMod::GridExt::MUL, @grid_ext_x].max if @grid_ext_x < @grid_ext_real_x
  210.     @grid_ext_real_x = [@grid_ext_real_x + distance_per_frame * MoveMod::GridExt::MUL, @grid_ext_x].min if @grid_ext_x > @grid_ext_real_x
  211.     @grid_ext_real_y = [@grid_ext_real_y - distance_per_frame * MoveMod::GridExt::MUL, @grid_ext_y].max if @grid_ext_y < @grid_ext_real_y
  212.     @grid_ext_real_y = [@grid_ext_real_y + distance_per_frame * MoveMod::GridExt::MUL, @grid_ext_y].min if @grid_ext_y > @grid_ext_real_y
  213.  
  214.     Game_CharacterBase__update_move()
  215.   end
  216.  
  217.   # Movement Grid Ext:
  218.   #   Changed to update movement grid position and scale down to map position
  219.   def move_straight(d, turn_ok = true)
  220.     new_grid_ext_x = $game_map.round_grid_ext_x_with_direction(@grid_ext_x, d)
  221.     new_grid_ext_y = $game_map.round_grid_ext_y_with_direction(@grid_ext_y, d)
  222.     new_grid_ext_real_x = $game_map.x_with_direction(new_grid_ext_x, reverse_dir(d))
  223.     new_grid_ext_real_y = $game_map.y_with_direction(new_grid_ext_y, reverse_dir(d))
  224.     scaled_down_x = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_x);
  225.     scaled_down_y = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_y);
  226.     scaled_down_real_x = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_real_x);
  227.     scaled_down_real_y = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_real_y);
  228.  
  229.     @move_succeed = (scaled_down_x == @x && scaled_down_y == @y)
  230.     @move_succeed = passable?(@x, @y, d) if !@move_succeed
  231.  
  232.     if @move_succeed
  233.       set_direction(d)
  234.       @grid_ext_x = new_grid_ext_x
  235.       @grid_ext_y = new_grid_ext_y
  236.       @grid_ext_real_x = new_grid_ext_real_x
  237.       @grid_ext_real_y = new_grid_ext_real_y
  238.       @x = scaled_down_x
  239.       @y = scaled_down_y
  240.       @real_x = scaled_down_real_x
  241.       @real_y = scaled_down_real_y
  242.       increase_steps
  243.       return true
  244.     elsif turn_ok
  245.       set_direction(d)
  246.       check_event_trigger_touch_front
  247.     end
  248.    
  249.     return false
  250.   end
  251.  
  252.   # Diagonal Movement:
  253.   #   Fixed character turning
  254.   # Movement Grid Ext:
  255.   #   Changed to update movement grid position and scale down to map position
  256.   def move_diagonal(horz, vert)
  257.     new_grid_ext_x = $game_map.round_grid_ext_x_with_direction(@grid_ext_x, horz)
  258.     new_grid_ext_y = $game_map.round_grid_ext_y_with_direction(@grid_ext_y, vert)
  259.     new_grid_ext_real_x = $game_map.x_with_direction(new_grid_ext_x, reverse_dir(horz))
  260.     new_grid_ext_real_y = $game_map.y_with_direction(new_grid_ext_y, reverse_dir(vert))
  261.     scaled_down_x = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_x);
  262.     scaled_down_y = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_y);
  263.     scaled_down_real_x = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_real_x);
  264.     scaled_down_real_y = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_real_y);
  265.  
  266.     @move_succeed = (scaled_down_x == @x && scaled_down_y == @y)
  267.     if !@move_succeed
  268.       @move_succeed = diagonal_passable?(@x, @y, horz, vert)
  269.       if !@move_succeed
  270.         return true if move_straight(horz)
  271.         return move_straight(vert)
  272.       end
  273.     end
  274.  
  275.     if @move_succeed
  276.       turn_diagonal(horz, vert, false)
  277.       @grid_ext_x = new_grid_ext_x
  278.       @grid_ext_real_x = new_grid_ext_real_x
  279.       @x = scaled_down_x
  280.       @real_x = scaled_down_real_x
  281.       @grid_ext_y = new_grid_ext_y
  282.       @grid_ext_real_y = new_grid_ext_real_y
  283.       @y = scaled_down_y
  284.       @real_y = scaled_down_real_y
  285.       increase_steps
  286.       return true
  287.     else
  288.       turn_diagonal(horz, vert, true)
  289.     end
  290.    
  291.     return false
  292.   end
  293.  
  294.   ###############
  295.   # New Methods #
  296.   ###############
  297.  
  298.   # Diagonal Movement:
  299.   #   Turns the character towards either horizontal or vertical direction.
  300.   def turn_diagonal(horz, vert, check_touch_front)
  301.     # swap horz with vert if prefer horizontal display when moving diagonal
  302.     horz, vert = vert, horz if MoveMod::Diag::PREF_HORZ_DIR
  303.    
  304.     if vert == 0
  305.       set_direction(horz)
  306.       check_event_trigger_touch_front if check_touch_front
  307.     else
  308.       if horz != 0
  309.         set_direction(horz)
  310.         check_event_trigger_touch_front if check_touch_front
  311.       end
  312.       set_direction(vert)
  313.       check_event_trigger_touch_front if check_touch_front
  314.     end
  315.   end
  316. end
  317.  
  318. class Game_Player
  319.   #######################
  320.   # Overwritten Methods #
  321.   #######################
  322.  
  323.   # Diagonal Movement
  324.   #   Added ability to use Input.dir8 as movement source. Diagonal movement is
  325.   #   already in place and must just be called after manually translating
  326.   #   Input.dir8 into horizontal and vertical direction
  327.   alias :Game_Player__move_by_input :move_by_input
  328.   def move_by_input
  329.     return if !movable? || $game_map.interpreter.running?
  330.    
  331.     return if Input.dir8 == 0
  332.     diag_dir = Input.dir8
  333.     h_dir = 0
  334.     v_dir = 0
  335.  
  336.     if diag_dir >= 1 && diag_dir <= 3
  337.       v_dir = 2
  338.     elsif diag_dir >= 7 && diag_dir <= 9
  339.       v_dir = 8
  340.     end
  341.  
  342.     if diag_dir == 1 || diag_dir == 4 || diag_dir == 7
  343.       h_dir = 4
  344.     elsif diag_dir == 3 || diag_dir == 6 || diag_dir == 9
  345.       h_dir = 6
  346.     end
  347.  
  348.     move_diagonal(h_dir, v_dir)
  349.   end
  350. end
  351.  
  352. class Game_Character
  353.   def grid_ext_distance_x_from(x)
  354.     result = @grid_ext_x - x
  355.     if $game_map.loop_horizontal? && result.abs > $game_map.grid_ext_width / 2
  356.       if result < 0
  357.         result += $game_map.grid_ext_width
  358.       else
  359.         result -= $game_map.grid_ext_width
  360.       end
  361.     end
  362.     result
  363.   end
  364.  
  365.   def grid_ext_distance_y_from(y)
  366.     result = @grid_ext_y - y
  367.     if $game_map.loop_vertical? && result.abs > $game_map.grid_ext_height / 2
  368.       if result < 0
  369.         result += $game_map.grid_ext_height
  370.       else
  371.         result -= $game_map.grid_ext_height
  372.       end
  373.     end
  374.     result
  375.   end
  376. end
  377.  
  378. class Game_Follower
  379.   def chase_preceding_character
  380.     unless moving?
  381.       sx = grid_ext_distance_x_from(@preceding_character.grid_ext_x)
  382.       sy = grid_ext_distance_y_from(@preceding_character.grid_ext_y)
  383.       sx_dir = sx > 0 ? 4 : 6
  384.       sy_dir = sy > 0 ? 8 : 2
  385.       sx_abs = sx.abs
  386.       sy_abs = sy.abs
  387.  
  388.       if (sx_abs >= MoveMod::GridExt::MUL || sy_abs >= MoveMod::GridExt::MUL) && sx != 0 && sy != 0
  389.         move_diagonal(sx_dir, sy_dir)
  390.       elsif sx_abs >= MoveMod::GridExt::MUL
  391.         if sy_abs > 0
  392.           move_straight(sy_dir)
  393.         else
  394.           move_straight(sx_dir)
  395.         end
  396.       elsif sy_abs >= MoveMod::GridExt::MUL
  397.         if sx_abs > 0
  398.           move_straight(sx_dir)
  399.         else
  400.           move_straight(sy_dir)
  401.         end
  402.       end
  403.     end
  404.   end
  405. end
RAW Paste Data