Project1
标题: [稍微伸手属性]像素行走的问题 [打印本页]
作者: Wind2010 时间: 2012-8-1 00:00
标题: [稍微伸手属性]像素行走的问题
本帖最后由 Wind2010 于 2012-8-2 23:36 编辑
以下为Fux兄的像素行走脚本,其中2-33行为自己的添加
四方向行走时没有什么问题,但是八方向行走的话貌似不能和四方向一样绕开障碍?求解如何解决- class Game_Character
- #--------------------------------------------------------------------------
- # ● 向左下移动
- #--------------------------------------------------------------------------
- def move_lower_left(turn_enabled = true,bot=false)
- move_down(!turn_enabled,bot)
- move_left(!turn_enabled,bot)
- @direction = (@direction == 6 ? 4 : @direction == 8 ? 2 : @direction) if turn_enabled and !@direction_fix
- end
- #--------------------------------------------------------------------------
- # ● 向右下移动
- #--------------------------------------------------------------------------
- def move_lower_right(turn_enabled = true,bot=false)
- move_down(!turn_enabled,bot)
- move_right(!turn_enabled,bot)
- @direction = (@direction == 4 ? 6 : @direction == 8 ? 2 : @direction) if turn_enabled and !@direction_fix
- end
- #--------------------------------------------------------------------------
- # ● 向左上移动
- #--------------------------------------------------------------------------
- def move_upper_left(turn_enabled = true,bot=false)
- move_up(!turn_enabled,bot)
- move_left(!turn_enabled,bot)
- @direction = (@direction == 6 ? 4 : @direction == 2 ? 8 : @direction) if turn_enabled and !@direction_fix
- end
- #--------------------------------------------------------------------------
- # ● 向右上移动
- #--------------------------------------------------------------------------
- def move_upper_right(turn_enabled = true,bot=false)
- move_up(!turn_enabled,bot)
- move_right(!turn_enabled,bot)
- @direction = (@direction == 4 ? 6 : @direction == 2 ? 8 : @direction) if turn_enabled and !@direction_fix
- end
- alias:md:move_down
- def move_down(turn_enabled = true,bot=false)
- unless bot
- speed = (2**@move_speed).to_f/128
- if passable?(@x.to_i, (@y+speed).to_i, 2)
- if @x.to_i != @x
- if passable?((@x+0.9).to_i,(@y+speed).to_i, 2)
- turn_down if turn_enabled
- @y += speed
- else
- unless check_event_trigger_touch(@x.to_i+1, (@y+speed).to_i+1)
- @y = (@y+speed).to_i
- @x -= speed
- turn_down if turn_enabled
- else
- turn_down if turn_enabled
- end
- end
- else
- turn_down if turn_enabled
- @y += speed
- end
- else
- if passable?((@x+0.9).to_i,(@y+speed).to_i, 2)
- unless check_event_trigger_touch(@x.to_i, (@y+speed).to_i+1)
- @y = (@y+speed).to_i
- @x += speed
- turn_down if turn_enabled
- else
- turn_down if turn_enabled
- end
- else
- turn_down if turn_enabled
- @y = (@y+speed).to_i
- check_event_trigger_touch(@x.to_i, @y.to_i+1)
- end
- end
- else
- md(turn_enabled)
- end
- end
- alias:ml:move_left
- def move_left(turn_enabled = true,bot=false)
- unless bot
- speed = (2**@move_speed).to_f/128
- if passable?((@x-speed+0.9).to_i,@y.to_i, 4)
- if @y.to_i != @y
- if passable?((@x-speed+0.9).to_i,(@y+0.9).to_i, 4)
- turn_left if turn_enabled
- @x -= speed
- else
- unless check_event_trigger_touch(@x.to_i-1, @y.to_i+1)
- @x = @x.to_i
- @y -= speed
- turn_left if turn_enabled
- else
- turn_left if turn_enabled
- end
- end
- else
- turn_left if turn_enabled
- @x -= speed
- end
- else
- if passable?((@x-speed+0.9).to_i,(@y+0.9).to_i, 4)
- unless check_event_trigger_touch(@x.to_i-1, @y.to_i)
- @x = @x.to_i
- @y += speed
- turn_left if turn_enabled
- else
- turn_left if turn_enabled
- end
- else
- turn_left if turn_enabled
- @x = @x.to_i
- check_event_trigger_touch(@x.to_i-1, @y.to_i)
- end
- end
- else
- ml(turn_enabled)
- end
- end
- alias:mr:move_right
- def move_right(turn_enabled = true,bot=false)
- unless bot
- speed = (2**@move_speed).to_f/128
- if passable?((@x+speed).to_i,@y.to_i, 6)
- if @y.to_i != @y
- if passable?((@x+speed).to_i,(@y+0.9).to_i, 6)
- turn_right if turn_enabled
- @x += speed
- else
- unless check_event_trigger_touch((@x+speed).to_i+1, @y.to_i+1)
- @x = (@x+speed).to_i
- @y -= speed
- turn_right if turn_enabled
- else
- @x = (@x+speed).to_i
- turn_right if turn_enabled
- end
- end
- else
- turn_right if turn_enabled
- @x += speed
- end
- else
- if passable?((@x+speed).to_i,(@y+0.9).to_i, 6)
- unless check_event_trigger_touch((@x+speed).to_i+1, @y.to_i)
- @x = (@x+speed).to_i
- @y += speed
- turn_right if turn_enabled
- else
- @x = (@x+speed).to_i
- turn_right if turn_enabled
- end
- else
- turn_right if turn_enabled
- @x = (@x+speed).to_i
- check_event_trigger_touch(@x.to_i+1, @y.to_i)
- end
- end
- else
- mr(turn_enabled)
- end
- end
- alias:mu:move_up
- def move_up(turn_enabled = true,bot=false)
- unless bot
- speed = (2**@move_speed).to_f/128
- if passable?(@x.to_i, (@y-speed+0.9).to_i, 8)
- if @x.to_i != @x
- if passable?((@x+0.9).to_i,(@y-speed+0.9).to_i, 8)
- turn_up if turn_enabled
- @y -= speed
- else
- unless check_event_trigger_touch(@x.to_i+1, @y.to_i-1)
- @y = @y.to_i
- @x -= speed
- turn_up if turn_enabled
- else
- turn_up if turn_enabled
- end
- end
- else
- turn_up if turn_enabled
- @y -= speed
- end
- else
- if passable?((@x+0.9).to_i,(@y-speed+0.9).to_i, 8)
- unless check_event_trigger_touch(@x.to_i, @y.to_i-1)
- @y = @y.to_i
- @x += speed
- turn_up if turn_enabled
- else
- turn_up if turn_enabled
- end
- else
- turn_up if turn_enabled
- @y = @y.to_i
- check_event_trigger_touch(@x.to_i, @y.to_i-1)
- end
- end
- else
- mu(turn_enabled)
- end
- end
-
- end
- class Game_Player < Game_Character
- 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+0.5).between?(@x,@x+1) and (event.y+0.5).between?(@y,@y+1)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+0.5).between?(new_x,new_x+1) and (event.y+0.5).between?(new_y,new_y+1)and triggers.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.dir8
- when 1
- move_lower_left
- when 2
- move_down(true,false)
- when 3
- move_lower_right
- when 4
- move_left(true,false)
- when 6
- move_right(true,false)
- when 7
- move_upper_left
- when 8
- move_up(true,false)
- when 9
- move_upper_right
- 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
- unless $DEBUG and Input.press?(Input::CTRL)
- if @encounter_count > 0
- @encounter_count -= 1
- end
- end
- end
- end
- if Input.trigger?(Input::C)
- check_event_trigger_there([0,1,2]) unless check_event_trigger_here([0])
- end
- end
- end
- end
复制代码 另外为什么像现在这样的话,事件用接近主角时会无限改变方向?- class Game_Character
- def move_toward(to)
- sx = @x - to.x
- sy = @y - to.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 and sy>0
- move_upper_left
- elsif sx>0 and sy<0
- move_lower_left
- elsif sx<0 and sy>0
- move_upper_right
- elsif sx<0 and sy<0
- move_lower_right
- end
- end
- if abs_sx > abs_sy
- sx > 0 ? move_left : move_right
- if not moving? and sy != 0
- sy > 0 ? move_up : move_down
- end
- else
- sy > 0 ? move_up : move_down
- if not moving? and sx != 0
- sx > 0 ? move_left : move_right
- end
- end
- end
- end
复制代码