Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Jumping towards?

Status
Not open for further replies.
Time to bump this old thread. Is there a script command perhaps that allows x event to jump towards or away from the player? Also, a command that allows them to jump towards/away from another event?
 
I guess you can use this:

Code:
#==============================================================================
# ** Game_Character (Jump Commands By SephirothSpawn)
#==============================================================================
  
class Game_Character
  #--------------------------------------------------------------------------
  # * Jump toward character
  #--------------------------------------------------------------------------
  def jump_toward_character(character = $game_player)
    # Get difference in player coordinates
    sx = @x - character.x
    sy = @y - character.y
    # If coordinates are equal
    if sx == 0 and sy == 0
      return
    end
    # Get absolute value of difference
    abs_sx = sx.abs
    abs_sy = sy.abs
    # If horizontal and vertical distances are equal
    if abs_sx == abs_sy
      # Increase one of them randomly by 1
      rand(2) == 0 ? abs_sx += 1 : abs_sy += 1
    end
    # If horizontal distance is longer
    if abs_sx > abs_sy
      # Jump towards character, prioritize left and right directions
      sx > 0 ? jump(-1, 0) : jump(1, 0)
      if not jumping? and sy != 0
        sy > 0 ? jump(0, -1) : jump(0, 1)
      end
    # If vertical distance is longer
    else
      # Jump towards character, prioritize up and down directions
      sy > 0 ? jump(0, -1) : jump(0, 1)
      if not jumping? and sx != 0
        sx > 0 ? jump(-1, 0) : jump(1, 0)
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Jump away from character
  #--------------------------------------------------------------------------
  def jump_away_from_character(character = $game_player)
    # Get difference in player coordinates
    sx = @x - character.x
    sy = @y - character.y
    # If coordinates are equal
    if sx == 0 and sy == 0
      return
    end
    # Get absolute value of difference
    abs_sx = sx.abs
    abs_sy = sy.abs
    # If horizontal and vertical distances are equal
    if abs_sx == abs_sy
      # Increase one of them randomly by 1
      rand(2) == 0 ? abs_sx += 1 : abs_sy += 1
    end
    # If horizontal distance is longer
    if abs_sx > abs_sy
      # Jump away from character, prioritize left and right directions
      sx > 0 ? jump(1, 0) : jump(-1, 0)
      if not jumping? and sy != 0
        sy > 0 ? jump(0, 1) : jump(0, -1)
      end
    # If vertical distance is longer
    else
      # Jump away from character, prioritize up and down directions
      sy > 0 ? jump(0, 1) : jump(0, -1)
      if not jumping? and sx != 0
        sx > 0 ? jump(1, 0) : jump(-1, 0)
      end
    end
  end
end

Insert above Main. Just use
Code:
e1 = <character>
e2 = <character>
-then-
e1.jump_toward_character(e2)
-or-
e1.jump_away_from_character(e2)

Replace <character> with:

The Player : $game_player
An Event : $game_map.events[event_id]

I haven't tested this. Let me know.
 
Status
Not open for further replies.

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top