赞 | 0 |
VIP | 0 |
好人卡 | 0 |
积分 | 1 |
经验 | 6059 |
最后登录 | 2017-5-29 |
在线时间 | 274 小时 |
Lv1.梦旅人
- 梦石
- 0
- 星屑
- 50
- 在线时间
- 274 小时
- 注册时间
- 2008-2-18
- 帖子
- 219
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
- #==============================================================================
- # ■ Find_Path
- #------------------------------------------------------------------------------
- # 寻路算法--完整鼠标系统(八方向)专用版
- # By whbm
- #==============================================================================
- class Find_Path
- #--------------------------------------------------------------------------
- def initialize #初始化
- @open_list = []
- @close_lise = []
- @path = []
- end #结束初始化
- #--------------------------------------------------------------------------
- def fp_passable?(x, y, d, tr_x = -2, tr_y = -2) #开始判定通行
- return false if (tr_x == @unable_xa or
- tr_x == @unable_xb or
- tr_y == @unable_ya or
- tr_y == @unable_yb)
- if [2, 4, 6, 8].include?(d)
- if $game_player.passable?(x, y, d)
- return true
- else
- return false
- end
- else
- case d
- when 1
- if ($game_player.passable?(x, y, 4) and
- $game_player.passable?(x - 1, y, 2)) or
- ($game_player.passable?(x, y, 2) and
- $game_player.passable?(x, y + 1, 4))
- return true
- else
- return false
- end
- when 3
- if ($game_player.passable?(x, y, 6) and
- $game_player.passable?(x + 1, y, 2)) or
- ($game_player.passable?(x, y, 2) and
- $game_player.passable?(x, y + 1, 6))
- return true
- else
- return false
- end
- when 7
- if ($game_player.passable?(x, y, 4) and
- $game_player.passable?(x - 1, y, 8)) or
- ($game_player.passable?(x, y, 8) and
- $game_player.passable?(x, y - 1, 4))
- return true
- else
- return false
- end
- when 9
- if ($game_player.passable?(x, y, 6) and
- $game_player.passable?(x + 1, y, 8)) or
- ($game_player.passable?(x, y, 8) and
- $game_player.passable?(x, y - 1, 6))
- return true
- else
- return false
- end
- end
- end
- end #结束判定通行
- #--------------------------------------------------------------------------
- def get_g(now_point) #开始计算G值
- d = now_point[2]
- return 0 if d == 5
- father_point = get_father_point(now_point)
- g = father_point[3] + ((d == 1 or d == 3 or d == 7 or d == 9) ? 14 : 10)
- return g
- end #结束计算G值
- #--------------------------------------------------------------------------
- def get_h(now_point) #开始计算H值
- now_x = now_point[0]
- now_y = now_point[1]
- #print @trg_x,now_x,@trg_y,now_y
- h = (@trg_x - now_x).abs + (@trg_y - now_y).abs
- return h * 10
- end #结束计算H值
- #--------------------------------------------------------------------------
- def get_f(now_point) #开始计算F值
- f = now_point[3] + now_point[4]
- return f
- end #结束计算F值
- #--------------------------------------------------------------------------
- def get_point(x, y) #取已知坐标点
- if @open_list.size != 0
- @open_list.each do |point|
- if point[0] == x and point[1] == y
- return point
- break
- end
- end
- end
- if @close_list.size != 0
- @close_list.each do |point|
- if point[0] == x and point[1] == y
- return point
- break
- end
- end
- end
- end #结束取已知坐标点
- #--------------------------------------------------------------------------
- def get_father_point(now_point) #取已知点的父节点
- d = now_point[2]
- return now_point if d == 5
- x = now_point[0] + ((d == 9 or d == 6 or d == 3) ? 1 : ((d == 7 or d == 4 or d == 1) ? -1 : 0))
- y = now_point[1] + ((d == 1 or d == 2 or d == 3) ? 1 : ((d == 7 or d == 8 or d == 9) ? -1 : 0))
- return get_point(x, y)
- end #结束取已知点的父节点
- #--------------------------------------------------------------------------
- def new_point(x, y, d) #开始建立新节点
- #print x,y,d
- point = [x, y, d]
- point.push get_g(point)
- point.push get_h(point)
- point.push get_f(point)
- return point
- end #结束建立新节点
- #--------------------------------------------------------------------------
- def get_direction(self_x, self_y, trg_x, trg_y)
- if trg_x > self_x
- if trg_y - self_y > - 0.4 * ( trg_x - self_x ) and
- trg_y - self_y < 0.4 * ( trg_x - self_x )
- return 6
- end
- if trg_y - self_y > 0.4 * ( trg_x - self_x ) and
- trg_y - self_y < 2.4 * ( trg_x - self_x )
- return 3
- end
- if trg_y - self_y < - 0.4 * ( trg_x - self_x ) and
- trg_y - self_y > - 2.4 * ( trg_x - self_x )
- return 9
- end
- if trg_y - self_y > 2.4 * ( trg_x - self_x )
- return 2
- end
- if trg_y - self_y < - 2.4 * ( trg_x - self_x )
- return 8
- end
- end
- if trg_x < self_x
- if trg_y - self_y > - 0.4 * ( self_x - trg_x ) and
- trg_y - self_y < 0.4 * ( self_x - trg_x )
- return 4
- end
- if trg_y - self_y > 0.4 * ( self_x - trg_x ) and
- trg_y - self_y < 2.4 * ( self_x - trg_x )
- return 1
- end
- if trg_y - self_y < - 0.4 * ( self_x - trg_x ) and
- trg_y - self_y > - 2.4 * ( self_x - trg_x )
- return 7
- end
- if trg_y - self_y > 2.4 * ( self_x - trg_x )
- return 2
- end
- if trg_y - self_y < - 2.4 * ( self_x - trg_x )
- return 8
- end
- end
- end
- #--------------------------------------------------------------------------
- def get_d_x_y(x, y, d)
- d_x = x + ((d == 9 or d == 6 or d == 3) ? 1 : ((d == 7 or d == 4 or d == 1) ? -1 : 0))
- d_y = y + ((d == 1 or d == 2 or d == 3) ? 1 : ((d == 7 or d == 8 or d == 9) ? -1 : 0))
- return d_x, d_y
- end
- #--------------------------------------------------------------------------
- def find_short_path_other(self_x, self_y, trg_x, trg_y,
- real_self_x, real_self_y, real_trg_x, real_trg_y)
- @self_x = self_x
- @self_y = self_y
- @now_x = self_x
- @now_y = self_y
- @trg_x = trg_x
- @trg_y = trg_y
- @path = []
- direction = get_direction(real_self_x, real_self_y, real_trg_x, real_trg_y)
- @now_trg_x, @now_trg_y = get_d_x_y(@self_x, @self_y, direction)
- while fp_passable?(@now_x, @now_y, direction)
- @path.push direction
- @now_x = @now_trg_x
- @now_y = @now_trg_y
- @now_trg_x, @now_trg_y = get_d_x_y(@now_x, @now_y, direction)
- end
- return @path
- end
- #--------------------------------------------------------------------------
- def find_short_path(self_x, self_y, trg_x, trg_y,
- real_self_x, real_self_y, real_trg_x, real_trg_y) #开始搜索路径
-
- return find_short_path_other(self_x, self_y, trg_x, trg_y,
- real_self_x, real_self_y, real_trg_x, real_trg_y) if not
- (fp_passable?(trg_x, trg_y + 1, 8) or
- fp_passable?(trg_x + 1, trg_y, 4) or
- fp_passable?(trg_x - 1, trg_y, 6) or
- fp_passable?(trg_x, trg_y - 1, 2)) and @goal_type != 1
-
-
- #根据屏幕限定搜索面积..加速
- @unable_xa = $game_map.display_x / 128 - 1
- @unable_ya = $game_map.display_y / 128 - 1
- @unable_xb = $game_map.display_x / 128 + 20
- @unable_yb = $game_map.display_y / 128 + 20
-
-
- @self_x = self_x
- @self_y = self_y
- @now_x = self_x
- @now_y = self_y
- @trg_x = trg_x
- @trg_y = trg_y
- @open_list = []
- @close_list = []
- #准备搜索
- #print @self_x,@self_y
- @now_point = new_point(@self_x, @self_y, 5) #令起始点为当前点
- @open_list.push @now_point #将当前点加入关闭列表
- #开始搜索
- begin
- loop do
- check_trg = check_around_point(@now_point)
- if check_trg == true
- @path = get_path
- break
- end
- @now_point = get_lowest_f_point
- if @now_point == [] or @now_point == nil
- @path = []
- break
- end
- end
- rescue Hangup
- retry
- end
- return @path
- end #结束搜索路径
- #--------------------------------------------------------------------------
- def find_player_short_path(trg_x, trg_y,
- real_trg_x, real_trg_y) #寻找角色的最短路径
- self_x = $game_player.x
- self_y = $game_player.y
- real_self_x = $game_player.screen_x
- real_self_y = $game_player.screen_y
- @goal_type, event = $game_map.check_event_custom_exist(real_trg_x, real_trg_y)
- if @goal_type == 1
- trg_x = event.x
- trg_y = event.y
- end
- return find_short_path(self_x, self_y, trg_x, trg_y,
- real_self_x, real_self_y, real_trg_x, real_trg_y)
- end #结束角色的寻找路径
- #--------------------------------------------------------------------------
- def get_path #取得最终的路径
- path = []
- now_point = @open_list[@open_list.size - 1]
- path.push(10 - now_point[2])
- last_point = now_point
- loop do
- now_point = get_father_point(now_point)
- break if now_point[2] == 5
- path.push(10 - now_point[2])
- end
- return path.reverse
- end #结束取得最终的路径
- #--------------------------------------------------------------------------
- def get_lowest_f_point #开始取得最低F值的点
- if @open_list == []
- return []
- end
- last_lowest_f_point = @open_list[0]
- @open_list.each do |point|
- last_lowest_f_point = point if point[5] < last_lowest_f_point[5]
- end
- return last_lowest_f_point
- end #结束取得最低F值点
- #--------------------------------------------------------------------------
- def check_around_point(point) #开始检查已知点的八方节点
- for d in [1, 2, 3, 4, 6, 7, 8, 9]
- x = point[0] + ((d == 9 or d == 6 or d == 3) ? 1 : ((d == 7 or d == 4 or d == 1) ? -1 : 0))
- y = point[1] + ((d == 1 or d == 2 or d == 3) ? 1 : ((d == 7 or d == 8 or d == 9) ? -1 : 0))
- if in_close_list?(x, y) #在关闭列表中
- next
- elsif in_open_list?(x, y) #在开启列表中
- get_new_g_point = new_point(x, y, 10 - d)
- get_last_g_point = get_point(x, y)
- if get_new_g_point[3] >= get_last_g_point[3]
- next
- else
- #如果改变父节点是新G值更小则确定改变
- @open_list[@open_list.index(get_last_g_point)] = get_new_g_point
- end
- else
- if fp_passable?(point[0], point[1], d, x, y)
- # 如果不在开启列表中、且不在关闭列表中、且通行则添加它到新八周节点
- @open_list.push new_point(x, y, 10 - d)
- #如果将目标点添加到了开启列表中就返回true
- return true if x == @trg_x and y == @trg_y
- return true if @goal_type == 1 and [1, 0, -1].include?(x - @trg_x) and [1, 0, -1].include?(y - @trg_y)
- end
- end
- end
- #此刻没有找到目标点并将当前点加入关闭列表并在开启列表中删除
- @close_list.push point
- @open_list.delete(point)
- #此刻没找到目标点并返回false
- return false
- end #结束计算已知点的八方节点
- #--------------------------------------------------------------------------
- def in_open_list?(x, y) #开始检查谋点是否在开启列表中
- @open_list.each do |point|
- return true if point[0] == x and point[1] == y
- end
- return false
- end #结束检查谋点是否在开启列表中
- #--------------------------------------------------------------------------
- def in_close_list?(x, y) #开始检查谋点是否在关闭列表中
- @close_list.each do |point|
- return true if point[0] == x and point[1] == y
- end
- return false
- end #结束检查谋点是否在关闭列表中
- #--------------------------------------------------------------------------
- end
复制代码- #=============================
- # ■ Game_Player
- #------------------------------------------------------------------------------
- # 处理主角的类。事件启动的判定、以及地图的滚动等功能。
- # 本类的实例请参考 $game_player。
- #===========================================================================
- $决定键失效 = 20 # 开关25号打开的时候决定键失效
- #===========================================================================
- class Game_Player < Game_Character
- #--------------------------------------------------------------------------
- # ● 恒量
- #--------------------------------------------------------------------------
- CENTER_X = (320 - 16) * 4 # 画面中央的 X 坐标 * 4
- CENTER_Y = (240 - 16) * 4 # 画面中央的 Y 坐标 * 4
- #--------------------------------------------------------------------------
- # ● 可以通行判定
- # x : X 坐标
- # y : Y 坐标
- # d : 方向 (0,2,4,6,8) ※ 0 = 全方向不能通行的情况判定 (跳跃用)
- #--------------------------------------------------------------------------
- def passable?(x, y, d)
- # 求得新的坐标
- new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
- new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)
- # 坐标在地图外的情况下
- unless $game_map.valid?(new_x, new_y)
- # 不能通行
- return false
- end
- # 调试模式为 ON 并且 按下 CTRL 键的情况下
- if $DEBUG and Input.press?(Input::CTRL)
- # 可以通行
- return true
- end
- super
- end
- #--------------------------------------------------------------------------
- # ● 像通到画面中央一样的设置地图的显示位置
- #--------------------------------------------------------------------------
- def center(x, y)
- max_x = ($game_map.width - 20) * 128
- max_y = ($game_map.height - 15) * 128
- $game_map.display_x = [0, [x * 128 - CENTER_X, max_x].min].max
- $game_map.display_y = [0, [y * 128 - CENTER_Y, max_y].min].max
- end
- #--------------------------------------------------------------------------
- # ● 向指定的位置移动
- # x : X 座標
- # y : Y 座標
- #--------------------------------------------------------------------------
- def moveto(x, y)
- super
- # 自连接
- center(x, y)
- # 生成遇敌计数
- make_encounter_count
- end
- #--------------------------------------------------------------------------
- # ● 增加步数
- #--------------------------------------------------------------------------
- def increase_steps
- super
- # 不是强制移动路线的场合
- unless @move_route_forcing
- # 增加步数
- $game_party.increase_steps
- # 步数是偶数的情况下
- if $game_party.steps % 2 == 0
- # 检查连续伤害
- $game_party.check_map_slip_damage
- end
- end
- end
- #--------------------------------------------------------------------------
- # ● 获取遇敌计数
- #--------------------------------------------------------------------------
- def encounter_count
- return @encounter_count
- end
- #--------------------------------------------------------------------------
- # ● 生成遇敌计数
- #--------------------------------------------------------------------------
- def make_encounter_count
- # 两种颜色震动的图像
- if $game_map.map_id != 0
- n = $game_map.encounter_step
- @encounter_count = rand(n) + rand(n) + 1
- end
- end
- #--------------------------------------------------------------------------
- # ● 刷新
- #--------------------------------------------------------------------------
- def refresh
- # 同伴人数为 0 的情况下
- if $game_party.actors.size == 0
- # 清除角色的文件名及对像
- @character_name = ""
- @character_hue = 0
- # 分支结束
- return
- end
- # 获取带头的角色
- actor = $game_party.actors[0]
- # 设置角色的文件名及对像
- @character_name = actor.character_name
- @character_hue = actor.character_hue
- # 初始化不透明度和合成方式子
- @opacity = 255
- @blend_type = 0
- end
- #--------------------------------------------------------------------------
- # ● 同位置的事件启动判定
- #--------------------------------------------------------------------------
- def check_event_trigger_here(triggers)
- result = false
- # 事件执行中的情况下
- if $game_system.map_interpreter.running?
- return result
- end
- # 全部事件的循环
- for event in $game_map.events.values
- # 事件坐标与目标一致的情况下
- if event.x == @x and event.y == @y and triggers.include?(event.trigger)
- # 跳跃中以外的情况下、启动判定是同位置的事件
- if not event.jumping? and event.over_trigger?
- event.start
- result = true
- end
- end
- end
- return result
- end
- #--------------------------------------------------------------------------
- # ● 正面事件的启动判定
- #--------------------------------------------------------------------------
- def check_event_trigger_there(triggers)
- result = false
- # 事件执行中的情况下
- if $game_system.map_interpreter.running?
- return result
- end
- # 计算正面坐标
- new_x = @x + (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
- new_y = @y + (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
- # 全部事件的循环
- for event in $game_map.events.values
- # 事件坐标与目标一致的情况下
- if event.x == new_x and event.y == new_y and
- triggers.include?(event.trigger)
- # 跳跃中以外的情况下、启动判定是正面的事件
- if not event.jumping? and not event.over_trigger?
- event.start
- result = true
- end
- end
- end
- # 找不到符合条件的事件的情况下
- if result == false
- # 正面的元件是计数器的情况下
- if $game_map.counter?(new_x, new_y)
- # 计算 1 元件里侧的坐标
- new_x += (@direction == 6 ? 1 : @direction == 4 ? -1 : 0)
- new_y += (@direction == 2 ? 1 : @direction == 8 ? -1 : 0)
- # 全事件的循环
- for event in $game_map.events.values
- # 事件坐标与目标一致的情况下
- if event.x == new_x and event.y == new_y and
- triggers.include?(event.trigger)
- # 跳跃中以外的情况下、启动判定是正面的事件
- if not event.jumping? and not event.over_trigger?
- event.start
- result = true
- end
- end
- end
- end
- end
- return result
- end
- #--------------------------------------------------------------------------
- # ● 接触事件启动判定
- #--------------------------------------------------------------------------
- def check_event_trigger_touch(x, y)
- result = false
- # 事件执行中的情况下
- if $game_system.map_interpreter.running?
- return result
- end
- # 全事件的循环
- for event in $game_map.events.values
- # 事件坐标与目标一致的情况下
- if event.x == x and event.y == y and [1,2].include?(event.trigger)
- # 跳跃中以外的情况下、启动判定是正面的事件
- if not event.jumping? and not event.over_trigger?
- event.start
- result = true
- end
- end
- end
- return result
- end
- #--------------------------------------------------------------------------
- # ● 画面更新
- #--------------------------------------------------------------------------
- def update
- # 本地变量记录移动信息
- last_moving = moving?
- # 移动中、事件执行中、强制移动路线中、
- # 信息窗口一个也不显示的时候
- unless moving? or $game_system.map_interpreter.running? or
- @move_route_forcing or $game_temp.message_window_showing
- # 如果方向键被按下、主角就朝那个方向移动
- case Input.dir4
- when 2
- move_down
- when 4
- move_left
- when 6
- move_right
- when 8
- move_up
- end
- end
- # 本地变量记忆坐标
- last_real_x = @real_x
- last_real_y = @real_y
- super
- # 角色向下移动、画面上的位置在中央下方的情况下
- if @real_y > last_real_y and @real_y - $game_map.display_y > CENTER_Y
- # 画面向下卷动
- $game_map.scroll_down(@real_y - last_real_y)
- end
- # 角色向左移动、画面上的位置在中央左方的情况下
- if @real_x < last_real_x and @real_x - $game_map.display_x < CENTER_X
- # 画面向左卷动
- $game_map.scroll_left(last_real_x - @real_x)
- end
- # 角色向右移动、画面上的位置在中央右方的情况下
- if @real_x > last_real_x and @real_x - $game_map.display_x > CENTER_X
- # 画面向右卷动
- $game_map.scroll_right(@real_x - last_real_x)
- end
- # 角色向上移动、画面上的位置在中央上方的情况下
- if @real_y < last_real_y and @real_y - $game_map.display_y < CENTER_Y
- # 画面向上卷动
- $game_map.scroll_up(last_real_y - @real_y)
- end
- # 不在移动中的情况下
- unless moving?
- # 上次主角移动中的情况
- if last_moving
- # 与同位置的事件接触就判定为事件启动
- result = check_event_trigger_here([1,2])
- # 没有可以启动的事件的情况下
- if result == false
- # 调试模式为 ON 并且按下 CTRL 键的情况下除外
- unless $DEBUG and Input.press?(Input::CTRL)
- # 遇敌计数下降
- if @encounter_count > 0
- @encounter_count -= 1
- end
- end
- end
- end
- # 按下 C 键的情况下
- if $game_switches[$决定键失效] == true
- else
- if Input.trigger?(Input::C)
- # 判定为同位置以及正面的事件启动
- check_event_trigger_here([0])
- check_event_trigger_there([0,1,2])
- end
- end
- end
- end
- end
复制代码
两个一起用 ~八方向 失效
谁帮我整和下? 版务信息:本贴由楼主自主结贴~ |
|