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.

Move_toward_player 8-direction movement

Does anyone have a version of the def move_toward_player in Game_Character 3 to have 8 directions?

I came up with this:

Code:
    if sx == 0 or sx == 1 and sy == 0 or sy == 1
      case rand(2)
      when 0
        rand(2) == 0 ? sx += 1 : sy += 1
      when 1
        rand(2) == 0 ? sx -= 1 : sy -= 1
      end
    end
    if sx == 0 or sx == -1 and sy == 0 or sy == -1
      case rand(2)
      when 0
        rand(2) == 0 ? sx += 1 : sy += 1
      when 1
        rand(2) == 0 ? sx -= 1 : sy -= 1
      end
    end
    if sx < 0 and sy > 0
      move_upper_right
    elsif sx < 0 and sy < 0
      move_lower_right
    elsif sx > 0 and sy > 0
      move_upper_left
    elsif sx > 0 and sy < 0
      move_lower_left
    elsif sx < 0 and sy == 0
      move_right
    elsif sx > 0 and sy == 0
      move_left
    elsif sx == 0 and sy < 0
      move_down
    elsif sx == 0 and sy > 0
      move_up
    end

It's very noob-ish and laggy, plus: the character moves around like a headless chicken. Sure, he follows the hero, but he also randomly walks around in the progress. I hope some expert around here can shed some light on things. ;)
 

arev

Sponsor

here's what I've made for f0tz!'s pixelmovement:
Code:
  def turn_toward_player
    vector = Math.atan2(($game_player.y - @y), ($game_player.x - @x)) * 180 / Math::PI
    case vector
      when -22.49..22.49
        @direction = 6
      when 22.5..67.49
        @direction = 3
      when 67.5..112.49
        @direction = 2
      when 112.5..157.49
        @direction = 1
      when 157.5..180
        @direction = 4
      when -180..-157.5
        @direction = 4
      when -157.49..-112.5
        @direction = 7
      when -112.49..-67.5
        @direction = 8
      when -67.49..-22.5
        @direction = 9
      end
    end
Code:
  def move_toward_player
    turn_toward_player
    move_forward
  end
you can easily change it so the event instantly walks toward instead of first facing, and then walking forward. though the effect is the same (unless you want to walk bacwards toward player :p)
 

arev

Sponsor

Code:
  def move_forward
    case @direction
    when 1
      move_lower_left
    when 2
      move_down(false)
    when 3
      move_lower_right
    when 4
      move_left(false)
    when 6
      move_right(false)
    when 7
      move_upper_left
    when 8
      move_up(false)
    when 9
      move_upper_right
    end
  end

now that you mention it i remember f0tz! messed up some things with directions of the sprite. what 8 way script are you using?
 
.. o_O I'm using none, heh. I just want the enemy to move 8 directions, not m'self. ^^ Btw, I tried it out and it's really awkward... The sprite faces right when moving up-left, and the sprite doesn't show all the time. oO
 

arev

Sponsor

ahh... you're using 4 way chars, aren't you?
damn... I'm so used to 8 way chars I can't even think of any others :]
ok... try this:
Code:
  def move_toward_player
    vector = Math.atan2(($game_player.y - @y), ($game_player.x - @x)) * 180 / Math::PI
    case vector
      when -22.49..22.49
        move_right(false)
      when 22.5..67.49
        move_lower_right
      when 67.5..112.49
        move_down(false)
      when 112.5..157.49
        move_lower_left
      when 157.5..180
        move_left(false)
      when -180..-157.5
        move_left(false)
      when -157.49..-112.5
        move_upper_left
      when -112.49..-67.5
        move_up(false)
      when -67.49..-22.5
        move_upper_right
      end
    end
 

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