扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 115|回复: 0

[转载发布] 八方向靠近主角/远离主角算法(附上小翅膀)

[复制链接]
累计送礼:
0 个
累计收礼:
0 个
  • TA的每日心情
    开心
    5 小时前
  • 签到天数: 114 天

    连续签到: 4 天

    [LV.6]常住居民II

    2338

    主题

    403

    回帖

    1万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    6
    卡币
    10622
    OK点
    16
    推广点
    0
    同能卷
    0
    积分
    13391

    灌水之王

    发表于 2024-4-19 16:55:54 | 显示全部楼层 |阅读模式
    =.=为了弄支持纸娃娃的小翅膀,我特意写了八方向的算法。
    1.   #--------------------------------------------------------------------------  # ● 接近主角  #--------------------------------------------------------------------------  def move_toward_player    # 求得与主角的坐标差    sx = @x - $game_player.x    sy = @y - $game_player.y    # 坐标相等情况下    if sx == 0 and sy == 0      return    end    # 求得差的绝对值    abs_sx = sx.abs    abs_sy = sy.abs    # 横距离与纵距离相等的情况下    if abs_sx == abs_sy      #if sx > 0       # if sy > 0       # move_upper_left      # elsif sy < 0       # move_lower_left      # end     #elsif sx < 0      #  if sy > 0      #  move_upper_right     #elsif sy < 0     #  move_lower_right     #end     #end         rand(2) == 0 ? abs_sx += 1 : abs_sy += 1    end    # 横侧距离长的情况下    if abs_sx > abs_sy      # 左右方向优先。向主角移动     # sx > 0 ? move_left : move_right      if sx > 0        if sy > 0        move_upper_left       elsif sy < 0        move_lower_left     else       move_left       end     else       if sy > 0        move_upper_right     elsif sy < 0       move_lower_right     else       move_right     end     end      if not moving? and sy != 0        sy > 0 ? move_up : move_down      end    # 竖侧距离长的情况下    else abs_sx < abs_sy      # 上下方向优先。向主角移动      #sy > 0 ? move_up : move_down      if sy > 0        if sx > 0          move_upper_left        elsif sx < 0          move_upper_right        else          move_up        end      else        if sx > 0        move_lower_left     elsif sx < 0       move_lower_right     else       move_down     end     end      if not moving? and sx != 0        sx > 0 ? move_left : move_right      end    end  end  #--------------------------------------------------------------------------  # ● 远离主角  #--------------------------------------------------------------------------  def move_away_from_player    # 求得与主角的坐标差    sx = @x - $game_player.x    sy = @y - $game_player.y    # 坐标相等情况下    if sx == 0 and sy == 0      return    end    # 求得差的绝对值    abs_sx = sx.abs    abs_sy = sy.abs    # 横侧距离长的情况下    if abs_sx > abs_sy      # 左右方向优先。向主角移动     # sx > 0 ? move_left : move_right      if sx > 0        if sy > 0        move_lower_right       elsif sy < 0        move_upper_right     else       move_right       end     else       if sy > 0        move_lower_left     elsif sy < 0       move_upper_left     else       move_left     end     end      if not moving? and sy != 0        sy > 0 ? move_down : move_up      end    # 竖侧距离长的情况下    else abs_sx < abs_sy      # 上下方向优先。向主角移动      #sy > 0 ? move_up : move_down      if sy > 0        if sx > 0          move_lower_right        elsif sx < 0          move_lower_left        else          move_down        end      else        if sx > 0        move_upper_right     elsif sx < 0       move_upper_left     else       move_up     end     end      if not moving? and sx != 0        sx > 0 ? move_right : move_left      end    end  end复制代码
    复制代码
    附上工程:
                 本帖来自P1论坛作者奶油Da蛋糕,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=133599  若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    x
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2025-3-14 23:57 , Processed in 0.091559 second(s), 54 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表