Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #===============================================================================
- # ** Pathfind
- #===============================================================================
- class Pathfind
- ANGLES = [0, 45, 90, 135, 180, 225, 270, 315]
- INDEX = [6, 9, 8, 7, 4, 1, 2, 3]
- attr_reader :character
- def initialize(character, x, y)
- # Initialize required instance variables that the system needs.
- @character, @x, @y = character, x, y
- @route, @route_blocked = [], false
- calculate_route
- end
- def calculate_route
- # Set up an intial route. This will be deviated from if object blocks path.
- temp = [@character.x, @character.y]
- until temp == [@x, @y]
- move = next_move(temp[0], temp[1])
- # Break loop if the method cannot calculate a route.
- break if move == nil
- # Add move code, then reset temp variable
- @route.push(move)
- temp = next_coordinates(temp[0], temp[1], move)
- end
- end
- def next_coordinates(x, y, dir)
- # Returns the new coordinates of a successful move in given direction
- return case dir
- when 1 then [x - 1, y + 1] # Down-Left
- when 2 then [x, y + 1] # Down
- when 3 then [x + 1, y + 1] # Down-Right
- when 4 then [x - 1, y] # Left
- when 6 then [x + 1, y] # Right
- when 7 then [x - 1, y - 1] # Up-Left
- when 8 then [x, y - 1] # Up
- when 9 then [x + 1, y - 1] # Up-Right
- end
- end
- def get_angle(x1, y1, x2, y2)
- # Returns the angle between two points on the map.
- angle = Math.atan2(y1 - y2, x2 - x1) * 180 / Math::PI
- return (angle < 0) ? angle + 360 : angle
- end
- def next_move(x, y)
- # Returns the numerical "code" of which direction to make.
- angle = get_angle(x, y, @x, @y)
- value = ANGLES.find {|ang| (angle - ang.floor).abs < 45 }
- i = ANGLES.index(value)
- directions = [i, (i + 1) % 8, (i - 1) % 8].collect {|j| INDEX[j] }
- # Attempt first coordinate, if that fails, attempts next closest two
- directions.each {|d|
- return d if @character.passable?(*(next_coordinates(x, y, d) + [d])) }
- end
- def adapt_route
- # Calculate move on the fly.
- @character.make_move(next_move(@character.x, @character.y))
- end
- def update
- # If character is not moving, execute next move.
- unless @character.moving?
- @route_blocked ? @character.make_move(@route.shift) : adapt_route
- end
- end
- def complete?
- # Returns true/false if the move route is finished
- return @route.empty? || ((@character.x == @x) && (@character.y == @y))
- end
- end
- #===============================================================================
- # ** Game_Map
- #===============================================================================
- class Game_Map
- attr_accessor :pathfind
- alias zer0_pathfind_setup setup
- def setup(map_id)
- # Initialize update array for pathfinding.
- @pathfind = []
- zer0_pathfind_setup(map_id)
- end
- alias zer0_pathfind_upd update
- def update
- # Update all pathfinders that are not complete.
- unless @pathfind.empty?
- # Update pathfinders, then remove any that are complete.
- @pathfind.each {|pathfind| pathfind.update }
- @pathfind.reject! {|pathfind| pathfind.complete? }
- end
- # Normal method
- zer0_pathfind_upd
- end
- end
- #===============================================================================
- # ** Game_Character
- #===============================================================================
- class Game_Character
- def pathfind(x, y)
- # Adds this character's route to the map's pathfind update.
- pathfinder = Pathfind.new(self, x, y)
- # Remove any route from the update if it already exists for this character
- $game_map.pathfind.reject! {|pathfind| pathfind.character == self }
- # Add the route
- $game_map.pathfind.push(pathfinder)
- end
- def make_move(dir)
- # Make a move using the passed number as a condition for direction.
- case dir
- when 1 then move_lower_left
- when 2 then move_down(!@direction_fix)
- when 3 then move_lower_right
- when 4 then move_left(!@direction_fix)
- when 6 then move_right(!@direction_fix)
- when 7 then move_upper_left
- when 8 then move_up(!@direction_fix)
- when 9 then move_upper_right
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement