301 lines
7.8 KiB
Ruby
301 lines
7.8 KiB
Ruby
|
||
|
||
|
||
|
||
|
||
|
||
|
||
class Game_Map
|
||
|
||
attr_accessor :cam_target, :cam_speed # 加cam_speed存follow速度,神技解鎖!
|
||
attr_accessor :map
|
||
|
||
def scroll_to_display_pos(tx, ty, speed)
|
||
rx = tx - @display_x
|
||
ry = ty - @display_y
|
||
|
||
if rx.abs < 0.1 && ry.abs < 0.1 # epsilon到達,鎖定!
|
||
@scroll_blocked = true
|
||
return
|
||
end
|
||
|
||
# step強化:speed/16讓6=0.375t/f(飛快),9=0.5625(超猛),目視差異爆表!
|
||
step = speed / 16.0
|
||
|
||
dx = (rx <=> 0) * [rx.abs, step].min
|
||
dy = (ry <=> 0) * [ry.abs, step].min
|
||
|
||
# 邊界鐵壁,撞牆就停(不像你追我一樣頭破血流)
|
||
dx = 0 if dx > 0 && !can_move?(6); dx = 0 if dx < 0 && !can_move?(4)
|
||
dy = 0 if dy > 0 && !can_move?(2); dy = 0 if dy < 0 && !can_move?(8)
|
||
|
||
# 核心保險:如果dx&dy全0(無法動)但還沒到,視同邊界追到,強制blocked!防卡死神技
|
||
if dx.zero? && dy.zero? && (rx.abs >= 0.1 || ry.abs >= 0.1)
|
||
@scroll_blocked = true
|
||
return # 邊界卡住=追到,釋放interpreter,Game_Player重生慶祝~你這豬腦終於安全了
|
||
end
|
||
|
||
@display_x += dx
|
||
@display_y += dy
|
||
set_display_pos(@display_x, @display_y) # clamp&loop
|
||
|
||
@scroll_blocked = false # 還在追
|
||
Fiber.yield if $game_map.cam_target # follow時yield,smooth
|
||
end
|
||
|
||
|
||
|
||
# 入口:target tile pos → display pos
|
||
def scroll_to_target(x, y, speed)
|
||
tx = x - $game_player.center_x
|
||
ty = y - $game_player.center_y
|
||
scroll_to_display_pos(tx.to_f, ty.to_f, speed)
|
||
end
|
||
|
||
def scroll_input(x, y, speed)
|
||
if @display_y.to_i < y && @display_x.to_i > x && can_move?(2) && can_move?(4)
|
||
dir = 1
|
||
elsif @display_y.to_i > y && @display_x.to_i > x && can_move?(8) && can_move?(4)
|
||
dir = 7
|
||
elsif @display_y.to_i > y && @display_x.to_i < x && can_move?(8) && can_move?(6)
|
||
dir = 9
|
||
elsif @display_y.to_i < y && @display_x.to_i < x && can_move?(2) && can_move?(6)
|
||
dir = 3
|
||
elsif @display_y.to_i < y && can_move?(2)
|
||
dir = 2
|
||
elsif @display_y.to_i > y && can_move?(8)
|
||
dir = 8
|
||
elsif @display_x.to_i < x && can_move?(6)
|
||
dir = 6
|
||
elsif @display_x.to_i > x && can_move?(4)
|
||
dir = 4
|
||
else
|
||
dir = 0
|
||
end
|
||
|
||
if dir > 0
|
||
start_scroll(dir, 1, speed)
|
||
@scroll_blocked = false
|
||
else
|
||
@scroll_blocked = true
|
||
end
|
||
|
||
Fiber.yield while scrolling?
|
||
end
|
||
|
||
def cannot_scroll?
|
||
@scroll_blocked == true
|
||
end
|
||
|
||
def can_move?(dir)
|
||
case dir
|
||
when 2
|
||
(@display_y % @map.height) < height - (Graphics.height / 32)
|
||
when 4
|
||
(@display_x % @map.width) > 0
|
||
when 6
|
||
(@display_x % @map.width) < width - (Graphics.width / 32)
|
||
when 8
|
||
(@display_y % @map.height) > 0
|
||
end
|
||
end
|
||
|
||
# overwrite
|
||
def do_scroll(dir, dis)
|
||
case dir
|
||
when 1
|
||
scroll_down(dis)
|
||
scroll_left(dis)
|
||
when 2
|
||
scroll_down(dis)
|
||
when 3
|
||
scroll_down(dis)
|
||
scroll_right(dis)
|
||
when 4
|
||
scroll_left(dis)
|
||
when 6
|
||
scroll_right(dis)
|
||
when 7
|
||
scroll_up(dis)
|
||
scroll_left(dis)
|
||
when 8
|
||
scroll_up(dis)
|
||
when 9
|
||
scroll_up(dis)
|
||
scroll_right(dis)
|
||
end
|
||
end
|
||
|
||
def set_event_display_pos(x, y)
|
||
x = [0, [x, width - screen_tile_x].min].max unless loop_horizontal?
|
||
y = [0, [y, height - screen_tile_y].min].max unless loop_vertical?
|
||
@display_x = (x + width) % width
|
||
@display_y = (y + height) % height
|
||
end
|
||
|
||
end # Game_Map
|
||
|
||
|
||
class Game_Interpreter
|
||
|
||
def cam_center(speed = 8)
|
||
$game_map.cam_target = -1
|
||
scroll_to_event(0, speed) if speed != 0
|
||
$game_map.set_display_pos(
|
||
$game_player.x - $game_player.center_x,
|
||
$game_player.y - $game_player.center_y
|
||
)
|
||
$game_map.cam_target = 0
|
||
end
|
||
|
||
def cam_set(x, y, speed = 8)
|
||
$game_map.cam_target = -1
|
||
scroll_to_target(x, y, speed) if speed != 0
|
||
$game_map.set_display_pos(
|
||
x - $game_player.center_x,
|
||
y - $game_player.center_y
|
||
)
|
||
end
|
||
|
||
#def scroll_to_target(x, y, speed)
|
||
# scroll_x = (x - $game_player.center_x).to_i
|
||
# scroll_y = (y - $game_player.center_y).to_i
|
||
#
|
||
# loop do
|
||
# $game_map.scroll_to_target(scroll_x, scroll_y, speed)
|
||
# break if
|
||
# ($game_map.display_x == scroll_x &&
|
||
# $game_map.display_y == scroll_y) ||
|
||
# $game_map.cannot_scroll?
|
||
# end
|
||
#end
|
||
#
|
||
#def scroll_to_event(id, speed)
|
||
# char = id > 0 ? $game_map.events[id] : $game_player
|
||
#
|
||
# loop do
|
||
# scroll_x = char.x - $game_player.center_x
|
||
# scroll_y = char.y - $game_player.center_y
|
||
# $game_map.scroll_to_target(scroll_x, scroll_y, speed)
|
||
# break if
|
||
# ($game_map.display_x == scroll_x &&
|
||
# $game_map.display_y == scroll_y) ||
|
||
# $game_map.cannot_scroll?
|
||
# end
|
||
#end
|
||
def scroll_to_target(x, y, speed)
|
||
if speed == 0 # 瞬間?直接set,懶人模式
|
||
$game_map.set_display_pos(x - $game_player.center_x, y - $game_player.center_y)
|
||
return
|
||
end
|
||
|
||
tx = (x - $game_player.center_x).to_f
|
||
ty = (y - $game_player.center_y).to_f
|
||
timeout = 600 # 防無限loop(10秒),你這肥宅別搞壞地圖
|
||
|
||
loop do
|
||
$game_map.scroll_to_target(x, y, speed) # 每iter推一步
|
||
|
||
# break條件:epsilon or blocked or timeout(安全網)
|
||
rdx = $game_map.display_x - tx
|
||
rdy = $game_map.display_y - ty
|
||
break if rdx.abs < 0.1 && rdy.abs < 0.1 || $game_map.cannot_scroll? || (timeout -= 1) <= 0
|
||
end
|
||
end
|
||
|
||
def scroll_to_event(id, speed)
|
||
if speed == 0
|
||
char = id > 0 ? $game_map.events[id] : $game_player
|
||
$game_map.set_display_pos(char.x - $game_player.center_x, char.y - $game_player.center_y)
|
||
return
|
||
end
|
||
|
||
char = id > 0 ? $game_map.events[id] : $game_player
|
||
timeout = 600
|
||
|
||
loop do
|
||
tx = (char.x - $game_player.center_x).to_f
|
||
ty = (char.y - $game_player.center_y).to_f
|
||
$game_map.scroll_to_target(char.x, char.y, speed) # 動態recalc,追event超順
|
||
|
||
rdx = $game_map.display_x - tx
|
||
rdy = $game_map.display_y - ty
|
||
break if rdx.abs < 0.1 && rdy.abs < 0.1 || $game_map.cannot_scroll? || (timeout -= 1) <= 0
|
||
end
|
||
end
|
||
def cam_follow(event_id, speed = 6)
|
||
$game_map.cam_target = -1
|
||
scroll_to_event(event_id, speed) if speed != 0
|
||
$game_map.cam_target = event_id
|
||
end
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
def cam_follow(event_id, speed = 6)
|
||
$game_map.cam_target = -1 # 先斷開
|
||
$game_map.cam_speed = [[speed, 1].max, 12].min # clamp 1~12,9超猛OK!
|
||
scroll_to_event(event_id, $game_map.cam_speed) unless speed == 0 # 初始追(0=瞬間)
|
||
$game_map.cam_target = event_id # 啟動持續lerp追
|
||
end
|
||
|
||
# cam_center/set 保留,但用cam_speed=6
|
||
def cam_center(speed = 6)
|
||
$game_map.cam_target = -1
|
||
$game_map.cam_speed = 6
|
||
scroll_to_event(0, speed) unless speed == 0
|
||
$game_map.set_display_pos($game_player.x - $game_player.center_x, $game_player.y - $game_player.center_y)
|
||
$game_map.cam_target = 0
|
||
end
|
||
|
||
def cam_set(x, y, speed = 6)
|
||
$game_map.cam_target = -1
|
||
$game_map.cam_speed = speed
|
||
scroll_to_target(x, y, speed) unless speed == 0
|
||
$game_map.set_display_pos(x - $game_player.center_x, y - $game_player.center_y)
|
||
end
|
||
|
||
# scroll_to_* loop保留(用epsilon)
|
||
def scroll_to_target(x, y, speed)
|
||
tx = (x - $game_player.center_x).to_f
|
||
ty = (y - $game_player.center_y).to_f
|
||
timeout = 600
|
||
loop do
|
||
$game_map.scroll_to_target(x, y, speed)
|
||
rdx = $game_map.display_x - tx
|
||
rdy = $game_map.display_y - ty
|
||
break if rdx.abs < 0.1 && rdy.abs < 0.1 || $game_map.cannot_scroll? || (timeout -=1) <=0
|
||
end
|
||
end
|
||
|
||
def scroll_to_event(id, speed)
|
||
char = id > 0 ? $game_map.events[id] : $game_player
|
||
timeout = 600
|
||
loop do
|
||
tx = (char.x - $game_player.center_x).to_f # 用x簡化,real_x在update用
|
||
ty = (char.y - $game_player.center_y).to_f
|
||
$game_map.scroll_to_target(char.x, char.y, speed)
|
||
rdx = $game_map.display_x - tx
|
||
rdy = $game_map.display_y - ty
|
||
break if rdx.abs < 0.1 && rdy.abs < 0.1 || $game_map.cannot_scroll? || (timeout -=1) <=0
|
||
end
|
||
end
|
||
|
||
|
||
|
||
|
||
end # Game_Interpreter
|