查看: 2185|回复: 4

[已经过期] 让事件开启后,角色随机方向退后一步?

[复制链接]
梦石
0
星屑
1556
在线时间
626 小时
注册时间
2010-8-5
回帖
317
发表于 2015-10-8 17:36:08 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

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

×
下面是我的鼠标脚本。因为目前有一个问题:鼠标开启事件的时候,如果角色是贴着NPC的,那么只要鼠标打开事件一次,那么这个事件就会无线循环
另外,现在开启事件的话, 是距离7以内的事件直接打开。但是难免会有角色贴着NPC的情况。那么上面的问题就来了,
然后我个人想个方法,就是让他开启事件后,让角色随机方向退后一步[与NPC拉开距离].....原谅我这神逻辑,当然有其他的解决方法更好啦
  1. #==============================================================================
  2. # ■ Game_Player
  3. #------------------------------------------------------------------------------
  4. #  处理主角的类。事件启动的判定、以及地图的滚动等功能。
  5. # 本类的实例请参考 $game_player。
  6. #==============================================================================

  7. class Game_Player < Game_Character
  8.   if @self_alias == nil
  9.     alias self_update update
  10.     @self_alias = true
  11.   end
  12.   #--------------------------------------------------------------------------
  13.   # X☆R 添加:鼠标指向位置是否是NPC
  14.   #--------------------------------------------------------------------------
  15.   def get_npc
  16.     x, y = Mouse.get_mouse_pos
  17.     for event in $game_map.events.values
  18.       #next if event.character_name == ""
  19.       if x > event.screen_x - 16 and x < event.screen_x + 16 and
  20.        y > event.screen_y - 64 and y < event.screen_y
  21.         return event
  22.       end
  23.     end
  24.     return nil
  25.   end
  26.   #--------------------------------------------------------------------------
  27.   # ● 完整鼠标系统
  28.   #--------------------------------------------------------------------------
  29.   def update
  30.     # X☆R 添加:点击鼠标左键,并且距离小于 7 ,启动事件。
  31.     if Mouse.trigger?(0x01)
  32.       if get_npc != nil and (self.x-get_npc.x).abs + (self.y-get_npc.y).abs < 7
  33.         get_npc.start
  34.         return
  35.       end
  36.     end  
  37.     # 搜索路径
  38.     seek_path
  39.     # 当持续按下鼠标左键时
  40.     if Mouse.press?(0x01)
  41.       return if !System.whether_move
  42.       @count = 20 if @count.nil?
  43.       @count -= 1
  44.       if @count <= 0
  45.         @self_state = 2
  46.         @count = nil
  47.       end
  48.       follow(true) if @self_state != 2 and @count < 18
  49.     elsif Mouse.trigger?(0x02)
  50.       @self_state = 0
  51.       follow(false)
  52.     else
  53.       @count = 20
  54.     end
  55.     # 移动
  56.     if @self_state == 1
  57.       player_move
  58.     elsif @paths != nil and @self_state == 2
  59.       follow(true)
  60.     end
  61.     # 别名
  62.     self_update
  63.   end
  64.   #--------------------------------------------------------------------------
  65.   # ● 搜索路径
  66.   #--------------------------------------------------------------------------
  67.   def seek_path
  68.     # 获取鼠标在画面的坐标
  69.     x, y = Mouse.get_mouse_pos
  70.     # 当按下鼠标左键时
  71.     if Mouse.trigger?(0x01)
  72.       # 禁止移动的情况下返回
  73.       return if !System.whether_move
  74.       # 排除各种无效的情况
  75.       unless $game_system.map_interpreter.running? or
  76.         @move_route_forcing or $game_temp.message_window_showing
  77.         # 获取目标点
  78.         trg_x = (x + $game_map.display_x / 4) / 32
  79.         trg_y = (y + $game_map.display_y / 4) / 32
  80.         # 目标点不可以通行的情况下返回
  81.         return unless target_passable?(trg_x,trg_y)
  82.         # 初始化未开启事件
  83.         @event = nil
  84.         # 判断是否可以打开事件
  85.         event_start = check_event_custom_start(x, y)
  86.         # 若不能开启事件
  87.         if !event_start
  88.           # 初始化
  89.           @paths = []
  90.           @steps = 0
  91.           @self_state = 1
  92.           # 若存在未开启事件
  93.           if @event != nil
  94.             trg_x,trg_y = @event.x,@event.y
  95.           end
  96.           # 若目标不为自身则开始寻路
  97.           if trg_x != self.x or trg_y != self.y
  98.             # 添加点击动画
  99.             add_animation if @event.nil?
  100.             # 获得路径
  101.             @paths = Find_Path.new.find_player_short_path(trg_x, trg_y, x, y)
  102.           end
  103.         else
  104.           # 角色面向事件
  105.           self.turn_toward_player(@event) if !@event.nil? and !self.moving?
  106.           @steps = @paths = nil
  107.         end
  108.       end
  109.     end
  110.   end
  111.   #--------------------------------------------------------------------------
  112.   # ● 可以通行判定
  113.   #     x : X 坐标
  114.   #     y : Y 坐标
  115.   #--------------------------------------------------------------------------
  116.   def target_passable?(x, y)
  117.     # 获取地图通行表
  118.     passages = $data_tilesets[$game_map.tileset_id].passages
  119.     # 从层按从上到下的顺序调查循环
  120.     for i in [2, 1, 0]
  121.       # 取得元件 ID
  122.       tile_id = $game_map.data[x, y, i]
  123.       # 取得元件 ID 失败
  124.       if tile_id == nil
  125.         # 不能通行
  126.         return false
  127.       # 如果全方向的障碍物的接触被设置的情况下
  128.       elsif passages[tile_id] & 0x0f == 0x0f
  129.         # 不能通行
  130.         return false
  131.       # 这以外的优先度为 0 的情况下
  132.       elsif $game_map.priorities[tile_id] == 0
  133.         # 可以通行
  134.         return true
  135.       end
  136.     end
  137.     # 可以通行
  138.     return true
  139.   end
  140.   #--------------------------------------------------------------------------
  141.   # ● 判断是否可以打开事件
  142.   #--------------------------------------------------------------------------
  143.   def check_event_custom_start(x, y)
  144.     # 循环检查所有事件
  145.     for event in $game_map.events.values
  146.       bitmap = RPG::Cache.character(event.character_name,event.character_hue)
  147.       cw = bitmap.width / 8#event.animation_frame
  148.       ch = bitmap.height / 8#event.direction_frame
  149.       _x = event.screen_x
  150.       _y = event.screen_y
  151.       d = event.direction
  152.       case event.direction_frame
  153.       when 4
  154.         sy = d > 8 ? 2 : d > 7 ? 3 : d > 6 ? 3 : d > 5 ? 2 : d > 3 ? 1 : d > 2 ? 0 : d > 1 ? 0 : d > 0 ? 1 : 5
  155.       when 8
  156.         sy = d > 8 ? 7 : d > 7 ? 3 : d > 6 ? 6 : d > 5 ? 2 : d > 3 ? 1 : d > 2 ? 5 : d > 1 ? 0 : d > 0 ? 4 : 5
  157.       end
  158.       if x >= _x - cw / 2 and x <= _x + cw / 2 and y >= _y - ch and y <= _y and
  159.         bitmap.get_pixel(x - _x + cw / 2 + event.pattern * cw, y - _y + ch + sy * ch).alpha > 0
  160.         for i in 0...event.list.size
  161.           if ["Item", "Npc"].include?(event.list[i].parameters[0])
  162.             way_x = self.x - event.x
  163.             way_y = self.y - event.y
  164.             if way_x.abs + way_y.abs <= 8 and !self.moving?
  165.               # 矫正主角姿势
  166.               self.straighten
  167.               # 角色面向事件

  168.               # 开启事件
  169.               event.start
  170.               return true
  171.               self.pattern
  172.             else
  173.               @event = event
  174.               return self.moving? ? true : false
  175.             end
  176.           end
  177.         end
  178.       end
  179.     end
  180.     return false
  181.   end
  182.   #--------------------------------------------------------------------------
  183.   # ● 添加鼠标点击动画
  184.   #--------------------------------------------------------------------------
  185.   def add_animation
  186.     x, y = Mouse.get_mouse_pos
  187.     animation = Sprite.new
  188.     animation.visible = false
  189.     animation.bitmap = Bitmap.new("UI/鼠标效果序列图")
  190.     map_x = x - 10 + $game_map.display_x / 4
  191.     map_y = y - 5 + $game_map.display_y / 4
  192.     animation.src_rect.set(0, 0, 24, 13)
  193.     $鼠标效果.push([animation,0,map_x,map_y])
  194.   end
  195.   #--------------------------------------------------------------------------
  196.   # ● 开始移动
  197.   #--------------------------------------------------------------------------
  198.   def player_move
  199.     # 排除无效情况
  200.     unless moving? or $game_system.map_interpreter.running? or
  201.       @move_route_forcing or $game_temp.message_window_showing
  202.       # 若没有完成路径
  203.       if @steps != nil and @paths != nil and @steps <= @paths.size
  204.         # 判断路径
  205.         case @paths[@steps]
  206.         when 6
  207.           move_right
  208.           @steps += 1
  209.         when 4
  210.           move_left
  211.           @steps += 1
  212.         when 2
  213.           move_down
  214.           @steps += 1
  215.         when 8
  216.           move_up
  217.           @steps += 1
  218.         when 1
  219.           move_lower_left
  220.           @steps += 1
  221.         when 3
  222.           move_lower_right
  223.           @steps += 1
  224.         when 7
  225.           move_upper_left
  226.           @steps += 1
  227.         when 9
  228.           move_upper_right
  229.           @steps += 1
  230.         end
  231.         # 若存在未开启事件
  232.         if @event != nil
  233.           way_x = self.x - @event.x
  234.           way_y = self.y - @event.y
  235.           if way_x.abs + way_y.abs <= 8
  236.             # 角色面向事件
  237.             self.turn_toward_player(@event)
  238.             # 矫正主角姿势
  239.             self.straighten
  240.             # 开启事件
  241.             @event.start
  242.             # 终止路径
  243.             @paths = nil
  244.             return
  245.           end
  246.         end
  247.       end
  248.     end
  249.   end
  250.   #--------------------------------------------------------------------------
  251.   # ● 跟随/转向
  252.   #--------------------------------------------------------------------------
  253.   def follow(move = true)
  254.     # 禁止移动的情况下返回
  255.     return if !System.whether_move
  256.     # 选择主角的情况下
  257.     if $select_player
  258.       for i in $鼠标效果
  259.         Picture.dispose(i[0])
  260.       end
  261.       return
  262.     end
  263.     # 鼠标在移动或者转向的情况下
  264.     x, y = Mouse.get_mouse_pos
  265.     # 鼠标在移动中的情况下
  266.     if @mouse_x != x or @mouse_y != y or @display_x != $game_map.display_x or @display_y != $game_map.display_y
  267.       # 添加点击动画
  268.       add_animation if move
  269.       # 获取角色位置
  270.       self_x = self.screen_x
  271.       self_y = self.screen_y
  272.       # 排除无效情况
  273.       unless moving? or $game_system.map_interpreter.running? or
  274.         @move_route_forcing or $game_temp.message_window_showing
  275.         if x > self_x
  276.           if y - self_y > - 0.4 * (x - self_x) and y - self_y < 0.4 * (x - self_x)
  277.             move_right if move
  278.             @direction = 6
  279.           end
  280.           if y - self_y > 0.4 * (x - self_x) and y - self_y < 2.4 * (x - self_x)
  281.             move_lower_right if move
  282.             @direction = 3
  283.           end
  284.           if y - self_y < - 0.4 * (x - self_x) and y - self_y > - 2.4 * (x - self_x)
  285.             move_upper_right if move
  286.             @direction = 9
  287.           end
  288.           if y - self_y > 2.4 * (x - self_x)
  289.             move_down if move
  290.             @direction = 2
  291.           end
  292.           if y - self_y < - 2.4 * (x - self_x)
  293.             move_up if move
  294.             @direction = 8
  295.           end
  296.         end
  297.         if x < self_x
  298.           if y - self_y > - 0.4 * (self_x - x) and y - self_y < 0.4 * (self_x - x)
  299.             move_left if move
  300.             @direction = 4
  301.           end
  302.           if y - self_y > 0.4 * (self_x - x) and y - self_y < 2.4 * (self_x - x)
  303.             move_lower_left if move
  304.             @direction = 1
  305.           end
  306.           if y - self_y < - 0.4 * (self_x - x) and y - self_y > - 2.4 * (self_x - x)
  307.             move_upper_left if move
  308.             @direction = 7
  309.           end
  310.           if y - self_y > 2.4 * (self_x - x)
  311.             move_down if move
  312.             @direction = 2
  313.           end
  314.           if y - self_y < - 2.4 * (self_x - x)
  315.             move_up if move
  316.             @direction = 8
  317.           end
  318.         end
  319.       end
  320.       # 记忆变量
  321.       @mouse_x,@mouse_y = Mouse.get_mouse_pos
  322.       @display_x = $game_map.display_x
  323.       @display_y = $game_map.display_y
  324.     end
  325.   end
  326. end
复制代码
头像被屏蔽
梦石
0
星屑
88
在线时间
631 小时
注册时间
2014-8-4
回帖
3243
发表于 2015-10-8 19:01:40 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
签名被屏蔽
回复

使用道具 举报

梦石
0
星屑
1556
在线时间
626 小时
注册时间
2010-8-5
回帖
317
 楼主| 发表于 2015-10-9 00:07:32 | 显示全部楼层
冷峻逸 发表于 2015-10-8 19:01
我的能力只能做到这样

其实方向无所谓,主要是事件开启完后,能让角色不和NPC接触。。。LS莭方法的话,会很麻烦、、因为NPC多的话。。。。。。。
回复

使用道具 举报

水月·镜花

梦石
10
星屑
403
在线时间
1181 小时
注册时间
2005-10-22
回帖
1118

开拓者

发表于 2015-10-9 07:20:57 来自手机 | 显示全部楼层
我做ADV的时候也是利用角色触碰。

重复触发的事件要重复执行,可以用独立开关。首先在事件内容最后,设置移动路线-角色后退一步-等待移动结束,然后打开独立开关。第二步新建一个事件页,出现条件为独立开关打开时。这样第一页处理完,这个事件会跑到第二页而不会重复执行第一页。最后设置第二页事件内容为自动执行,关闭独立开关,这样事件就可以重新等待触发了。
回复

使用道具 举报

梦石
0
星屑
1232
在线时间
1017 小时
注册时间
2011-4-30
回帖
1464
发表于 2015-10-9 15:51:25 | 显示全部楼层
搜索  .start
试着把这些中的注释掉,看看能不能达到效果。
比如  event.start 改成 #event.start
手机作答,错了勿怪
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部