Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module MoveMod
- def self.int_pow_of_two?(candidate)
- raise "Integer required!" unless candidate.is_a?(Integer)
- candidate > 0 && (candidate & (candidate - 1)) == 0
- end
- end
- module MoveMod::Diag
- # Prefered facing switch when moving diagonally
- # true: when moving diagonal use the horizontal direction
- # false: when moving diagonal use the vertical direction
- PREF_HORZ_DIR = false
- end
- module MoveMod::GridExt
- # The movement grid cell size
- CELL_SIZE = 16 # MUST BE 4, 8, 16
- # The half movement gris cell size
- HALF_CELL_SIZE = CELL_SIZE / 2 # Do not touch
- # Multiplier
- MUL = 32 / CELL_SIZE # Do not touch
- # Half Multiplier
- HALF_MUL = MUL / 2 # Do not touch
- def self.scale_grid_size_up(a)
- a * MUL
- end
- def self.scale_grid_size_down(a)
- a / MUL
- end
- def self.scale_grid_pos_up(a)
- a * MUL + HALF_MUL
- end
- def self.scale_grid_pos_down(a)
- a / MUL
- end
- end
- class Game_Map
- attr_reader :grid_ext_width
- attr_reader :grid_ext_height
- #######################
- # Overwritten Methods #
- #######################
- # Movement Grid Ext:
- # Initialize the movement grid width/height with 0, it is used before a map
- # is loaded
- alias :Game_Map__initialize :initialize
- def initialize
- @grid_ext_width = 0
- @grid_ext_height = 0
- Game_Map__initialize()
- end
- # Movement Grid Ext:
- # Initialize the movement grid size by scalling the map size up
- alias :Game_Map__setup :setup
- def setup(map_id)
- Game_Map__setup(map_id)
- @grid_ext_width = MoveMod::GridExt.scale_grid_size_up(@map.width)
- @grid_ext_height = MoveMod::GridExt.scale_grid_size_up(@map.height)
- end
- ###############
- # New Methods #
- ###############
- def screen_tile_grid_ext_x
- Graphics.width / MoveMod::GridExt::CELL_SIZE
- end
- def screen_tile_grid_ext_y
- Graphics.height / MoveMod::GridExt::CELL_SIZE
- end
- def display_grid_ext_x
- display_x * MoveMod::GridExt::MUL
- end
- def display_grid_ext_y
- display_y * MoveMod::GridExt::MUL
- end
- def adjust_grid_ext_x(x)
- if loop_horizontal? && x < display_grid_ext_x - (@grid_ext_width - screen_tile_grid_ext_x) / 2
- x - display_grid_ext_x + @grid_ext_width
- else
- x - display_grid_ext_x
- end
- end
- def adjust_grid_ext_y(y)
- if loop_vertical? && y < display_grid_ext_y - (@grid_ext_height - screen_tile_grid_ext_y) / 2
- y - display_grid_ext_y + @grid_ext_height
- else
- y - display_grid_ext_y
- end
- end
- def round_grid_ext_x(x)
- loop_horizontal? ? (x + @grid_ext_width) % @grid_ext_width : x
- end
- def round_grid_ext_y(y)
- loop_vertical? ? (y + @grid_ext_height) % @grid_ext_height : y
- end
- def round_grid_ext_x_with_direction(x, d)
- round_grid_ext_x(x + (d == 6 ? 1 : d == 4 ? -1 : 0))
- end
- def round_grid_ext_y_with_direction(y, d)
- round_grid_ext_y(y + (d == 2 ? 1 : d == 8 ? -1 : 0))
- end
- end
- class Game_CharacterBase
- attr_reader :grid_ext_x
- attr_reader :grid_ext_y
- attr_reader :grid_ext_real_x
- attr_reader :grid_ext_real_y
- attr_reader :direction
- #######################
- # Overwritten Methods #
- #######################
- # Movement Grid Ext:
- # Initialize movement grid position
- alias :Game_Map__init_public_members :init_public_members
- def init_public_members
- @initial_moved = false
- @grid_ext_x = 0
- @grid_ext_y = 0
- @grid_ext_real_x = 0
- @grid_ext_real_y = 0
- Game_Map__init_public_members()
- end
- # Movement Grid Ext:
- # Check movement grid position instead of map position
- alias :Game_CharacterBase__moving? :moving?
- def moving?
- @grid_ext_real_x != @grid_ext_x || @grid_ext_real_y != @grid_ext_y
- end
- # Diaginal Movement:
- # Change logic to require both directions to be passable. This prevents
- # cutting corners of non passable tiles.
- alias :Game_CharacterBase__diagonal_passable? :diagonal_passable?
- def diagonal_passable?(x, y, horz, vert)
- x2 = $game_map.round_x_with_direction(x, horz)
- y2 = $game_map.round_y_with_direction(y, vert)
- (passable?(x, y, vert) && passable?(x, y2, horz)) &&
- (passable?(x, y, horz) && passable?(x2, y, vert))
- end
- # Movement Grid Ext:
- # Added assignment of movement grid ext position
- alias :Game_CharacterBase__moveto :moveto
- def moveto(x, y)
- x_same = (@initial_moved && MoveMod::GridExt.scale_grid_pos_down(@grid_ext_x) == @x)
- y_same = (@initial_moved && MoveMod::GridExt.scale_grid_pos_down(@grid_ext_y) == @y)
- Game_CharacterBase__moveto(x, y)
- if !x_same
- @grid_ext_x = MoveMod::GridExt.scale_grid_pos_up(@x)
- @grid_ext_real_x = @grid_ext_x
- end
- if !y_same
- @grid_ext_y = MoveMod::GridExt.scale_grid_pos_up(@y)
- @grid_ext_real_y = @grid_ext_y
- end
- @initial_moved = true
- end
- # Movement Grid Ext:
- # Use movement grid position instead of map position so we can display
- # characters with smaller than 32 steps. We don't add the half cell offset.
- # That gives visually the effect that the character is aligned to the
- # original grid.
- def screen_x
- $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)
- end
- # Movement Grid Ext:
- # Use movement grid position instead of map position so we can display
- # characters with smaller than 32 steps
- def screen_y
- $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
- # + (MoveMod::GridExt::CELL_SIZE * (MoveMod::GridExt::MUL))
- end
- # Movement Grid Ext:
- # Added movement grid position update
- alias :Game_CharacterBase__update_move :update_move
- def update_move
- @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
- @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
- @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
- @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
- Game_CharacterBase__update_move()
- end
- # Movement Grid Ext:
- # Changed to update movement grid position and scale down to map position
- def move_straight(d, turn_ok = true)
- new_grid_ext_x = $game_map.round_grid_ext_x_with_direction(@grid_ext_x, d)
- new_grid_ext_y = $game_map.round_grid_ext_y_with_direction(@grid_ext_y, d)
- new_grid_ext_real_x = $game_map.x_with_direction(new_grid_ext_x, reverse_dir(d))
- new_grid_ext_real_y = $game_map.y_with_direction(new_grid_ext_y, reverse_dir(d))
- scaled_down_x = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_x);
- scaled_down_y = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_y);
- scaled_down_real_x = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_real_x);
- scaled_down_real_y = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_real_y);
- @move_succeed = (scaled_down_x == @x && scaled_down_y == @y)
- @move_succeed = passable?(@x, @y, d) if !@move_succeed
- if @move_succeed
- set_direction(d)
- @grid_ext_x = new_grid_ext_x
- @grid_ext_y = new_grid_ext_y
- @grid_ext_real_x = new_grid_ext_real_x
- @grid_ext_real_y = new_grid_ext_real_y
- @x = scaled_down_x
- @y = scaled_down_y
- @real_x = scaled_down_real_x
- @real_y = scaled_down_real_y
- increase_steps
- return true
- elsif turn_ok
- set_direction(d)
- check_event_trigger_touch_front
- end
- return false
- end
- # Diagonal Movement:
- # Fixed character turning
- # Movement Grid Ext:
- # Changed to update movement grid position and scale down to map position
- def move_diagonal(horz, vert)
- new_grid_ext_x = $game_map.round_grid_ext_x_with_direction(@grid_ext_x, horz)
- new_grid_ext_y = $game_map.round_grid_ext_y_with_direction(@grid_ext_y, vert)
- new_grid_ext_real_x = $game_map.x_with_direction(new_grid_ext_x, reverse_dir(horz))
- new_grid_ext_real_y = $game_map.y_with_direction(new_grid_ext_y, reverse_dir(vert))
- scaled_down_x = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_x);
- scaled_down_y = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_y);
- scaled_down_real_x = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_real_x);
- scaled_down_real_y = MoveMod::GridExt.scale_grid_pos_down(new_grid_ext_real_y);
- @move_succeed = (scaled_down_x == @x && scaled_down_y == @y)
- if !@move_succeed
- @move_succeed = diagonal_passable?(@x, @y, horz, vert)
- if !@move_succeed
- return true if move_straight(horz)
- return move_straight(vert)
- end
- end
- if @move_succeed
- turn_diagonal(horz, vert, false)
- @grid_ext_x = new_grid_ext_x
- @grid_ext_real_x = new_grid_ext_real_x
- @x = scaled_down_x
- @real_x = scaled_down_real_x
- @grid_ext_y = new_grid_ext_y
- @grid_ext_real_y = new_grid_ext_real_y
- @y = scaled_down_y
- @real_y = scaled_down_real_y
- increase_steps
- return true
- else
- turn_diagonal(horz, vert, true)
- end
- return false
- end
- ###############
- # New Methods #
- ###############
- # Diagonal Movement:
- # Turns the character towards either horizontal or vertical direction.
- def turn_diagonal(horz, vert, check_touch_front)
- # swap horz with vert if prefer horizontal display when moving diagonal
- horz, vert = vert, horz if MoveMod::Diag::PREF_HORZ_DIR
- if vert == 0
- set_direction(horz)
- check_event_trigger_touch_front if check_touch_front
- else
- if horz != 0
- set_direction(horz)
- check_event_trigger_touch_front if check_touch_front
- end
- set_direction(vert)
- check_event_trigger_touch_front if check_touch_front
- end
- end
- end
- class Game_Player
- #######################
- # Overwritten Methods #
- #######################
- # Diagonal Movement
- # Added ability to use Input.dir8 as movement source. Diagonal movement is
- # already in place and must just be called after manually translating
- # Input.dir8 into horizontal and vertical direction
- alias :Game_Player__move_by_input :move_by_input
- def move_by_input
- return if !movable? || $game_map.interpreter.running?
- return if Input.dir8 == 0
- diag_dir = Input.dir8
- h_dir = 0
- v_dir = 0
- if diag_dir >= 1 && diag_dir <= 3
- v_dir = 2
- elsif diag_dir >= 7 && diag_dir <= 9
- v_dir = 8
- end
- if diag_dir == 1 || diag_dir == 4 || diag_dir == 7
- h_dir = 4
- elsif diag_dir == 3 || diag_dir == 6 || diag_dir == 9
- h_dir = 6
- end
- move_diagonal(h_dir, v_dir)
- end
- end
- class Game_Character
- def grid_ext_distance_x_from(x)
- result = @grid_ext_x - x
- if $game_map.loop_horizontal? && result.abs > $game_map.grid_ext_width / 2
- if result < 0
- result += $game_map.grid_ext_width
- else
- result -= $game_map.grid_ext_width
- end
- end
- result
- end
- def grid_ext_distance_y_from(y)
- result = @grid_ext_y - y
- if $game_map.loop_vertical? && result.abs > $game_map.grid_ext_height / 2
- if result < 0
- result += $game_map.grid_ext_height
- else
- result -= $game_map.grid_ext_height
- end
- end
- result
- end
- end
- class Game_Follower
- def chase_preceding_character
- unless moving?
- sx = grid_ext_distance_x_from(@preceding_character.grid_ext_x)
- sy = grid_ext_distance_y_from(@preceding_character.grid_ext_y)
- sx_dir = sx > 0 ? 4 : 6
- sy_dir = sy > 0 ? 8 : 2
- sx_abs = sx.abs
- sy_abs = sy.abs
- if (sx_abs >= MoveMod::GridExt::MUL || sy_abs >= MoveMod::GridExt::MUL) && sx != 0 && sy != 0
- move_diagonal(sx_dir, sy_dir)
- elsif sx_abs >= MoveMod::GridExt::MUL
- if sy_abs > 0
- move_straight(sy_dir)
- else
- move_straight(sx_dir)
- end
- elsif sy_abs >= MoveMod::GridExt::MUL
- if sx_abs > 0
- move_straight(sx_dir)
- else
- move_straight(sy_dir)
- end
- end
- end
- end
- end
RAW Paste Data