Scripts, Tutorials & Tools This forum is for scripts and code, as well as all kinds of tutorials, software, tools and so forth. Remember to give credit!
The thread revival limit does not apply here.

Reply
 
Thread Tools
  #1    
Old February 5th, 2017 (8:44 AM). Edited February 5th, 2017 by khkramer.
khkramer's Avatar
khkramer khkramer is offline
     
    Join Date: Apr 2010
    Gender: Male
    Posts: 316
    I've made a modified version of pbMoveTowardPlayer, which allows an event to move towards another event instead of the player ( handy for cutscenes etc where positions are dynamic ).

    Here is the script, place it somewhere above main:

    Spoiler:
    Code:
    class Game_Character
      def move_toward_event(event)
        sx = @x - event.x
        sy = @y - event.y
        if sx == 0 and sy == 0
          return
        end
        abs_sx = sx.abs
        abs_sy = sy.abs
        if abs_sx == abs_sy
          (rand(2) == 0) ? abs_sx += 1 : abs_sy += 1
        end
        if abs_sx > abs_sy
          sx > 0 ? move_left : move_right
          if not moving? and sy != 0
            sy > 0 ? move_up : move_down
          end
        else
          sy > 0 ? move_up : move_down
          if not moving? and sx != 0
            sx > 0 ? move_left : move_right
          end
        end
      end
      
      def move_toward_position(x, y)
        sx = @x - x
        sy = @y - y
        if sx == 0 and sy == 0
          return
        end
        abs_sx = sx.abs
        abs_sy = sy.abs
        if abs_sx == abs_sy
          (rand(2) == 0) ? abs_sx += 1 : abs_sy += 1
        end
        if abs_sx > abs_sy
          sx > 0 ? move_left : move_right
          if not moving? and sy != 0
            sy > 0 ? move_up : move_down
          end
        else
          sy > 0 ? move_up : move_down
          if not moving? and sx != 0
            sx > 0 ? move_left : move_right
          end
        end
      end
    end
    
    def getEventByName(name)
      if name.is_a?(Symbol)
        name = name.to_s
      end
      
      $game_map.events.values.each do |event|
        return event if event.name.downcase == name.downcase
      end
      
      return nil
    end
    
    def _resolveEvent(event)
      if event.is_a?(Game_Event)
        return event
      end
      
      if event.is_a?(Numeric)
        return get_character(event)
      end
      
      return getEventByName(event) # Symbol or string
    end
    
    def pbMoveTowardEvent(event, otherEvent)
      event = _resolveEvent(event)
      otherEvent = _resolveEvent(otherEvent)
      
      maxsize=[$game_map.width,$game_map.height].max
      #return if !pbEventCanReachPlayer?(event,otherEvent,maxsize)
      loop do
        x=event.x
        y=event.y
        event.move_toward_event(otherEvent)
        break if event.x==x && event.y==y
        while event.moving?
          Graphics.update
          Input.update
          pbUpdateSceneMap
        end
      end
      $PokemonMap.addMovedEvent(event.id) if $PokemonMap
    end
    
    def pbMoveTowardPosition(event, x, y)
      event = _resolveEvent(event)
        
      maxsize=[$game_map.width,$game_map.height].max
      #return if !pbEventCanReachPlayer?(event,otherEvent,maxsize)
      loop do
        event.move_toward_position(x, y)
        break if event.x==x && event.y==y
        while event.moving?
          Graphics.update
          Input.update
          pbUpdateSceneMap
        end
      end
      $PokemonMap.addMovedEvent(event.id) if $PokemonMap
    end


    The script can be used in an event like this:

    Code:
    pbMoveTowardEvent(:Distraction, :Oak)
    Where :Distraction and :Redwood are the names of the respective events ( case insensitive ).

    Or to move to a specified position instead:

    Code:
    pbMoveTowardPosition(:Distraction, 24, 15)
    Where :Distraction is the event's name, 24 is the X, and 15 is the Y.


    Hope this is useful for some of you :)
    If you find any bugs or have a suggestion, please let me know.
    __________________


    Lurking for 8 years..
    Reply With Quote
      #2    
    Old April 3rd, 2017 (3:47 AM).
    Lionhearted Lionhearted is offline
       
      Join Date: Mar 2017
      Posts: 3
      This script looks great! Nice idea, I'm definitely going to try it out! :3
      Reply With Quote
        #3    
      Old January 7th, 2018 (8:51 AM).
      khkramer's Avatar
      khkramer khkramer is offline
         
        Join Date: Apr 2010
        Gender: Male
        Posts: 316
        Quote:
        Originally Posted by The Dewitty View Post
        This could be really useful! Have the bugs mentioned by Jazerules been fixed?
        Few months later and I still haven't fixed them :3c
        That's mostly cause this was intended as a really simple script, and I myself use it 90% for movement in indoor cutscenes. In situations like that it works perfectly fine.

        I did at one point have another pathfinder script based on an A* pathfinder edited to work with essentials which is much more useful for the problems described by Jazerules: dealing with obstacles, long distances etc. I'm going search my PC to see if I still got the script and if so I intend to release it here since I've picked up fangame development again.
        __________________


        Lurking for 8 years..
        Reply With Quote
        Reply

        Quick Reply

        Join the conversation!

        Create an account to post a reply in this thread, participate in other discussions, and more!

        Create a PokéCommunity Account
        Thread Tools

        Posting Rules
        You may not post new threads
        You may not post replies
        You may not post attachments
        You may not edit your posts

        BB code is On
        Smilies are On
        [IMG] code is On
        HTML code is Off

        Forum Jump


        All times are GMT -8. The time now is 11:16 PM.