#==============================================================================
# ** Map Auto-Scroll
#------------------------------------------------------------------------------
# SephirothSpawn
# Version 1.0
# 2008-09-23
#------------------------------------------------------------------------------
# * Description:
#
# This script was designed to make map auto-scrolling even better, providing
# diagonal movements to any position. You may scroll to the center of any
# position, targeting the player, an event, an object you create, or
# a predefined position.
#------------------------------------------------------------------------------
# * Instructions:
#
# Place the script below the SDK (if included) and above main.
#------------------------------------------------------------------------------
# * Syntax:
#
# Scrolling to character (player/events)
# - $game_map.scroll_to_character(id, speed)
# - Script : scroll_to_character(id, speed)
# id: 0-Player, N-Event ID
#
# Scrolling to object (any object with .x and .y methods)
# - $game_map.scroll_to_object(object, speed)
# - Script : scroll_to_object(object, speed)
#
# Scroll to position
# - $game_map.scroll_to_target(x, y, speed)
# - Script : scroll_to_target(x, y, speed)
#
# speed: display distance change (16 = default 4 speed)
#------------------------------------------------------------------------------
# * Terms & Conditions:
#
# Copyright (C) 2007 SephirothSpawn (Timothy Hoffman)
# Free for non-commercial use.
# 10 USD commercial license. Contact via. [email=SephirothSpawn@hotmail.com]SephirothSpawn@hotmail.com[/email]
#
# Any modifications to the system are not to be re-distributed without my
# consent.
#==============================================================================
#==============================================================================
# ** SDK Log
#==============================================================================
if Object.const_defined?(:SDK)
SDK.log('Map Auto-Scroll', 'SephirothSpawn', 1.0, '2008-09-23')
end
#------------------------------------------------------------------------------
# * Begin SDK Enable Test
#------------------------------------------------------------------------------
if Object.const_defined?(:SDK) == false || !SDK.enabled?('Map Auto-Scroll')
#==============================================================================
# ** Game_Map
#==============================================================================
class Game_Map
#--------------------------------------------------------------------------
# * Constants
#--------------------------------------------------------------------------
Map_Width = 20
Map_Height = 15
Default_Scroll_Speed = 16
#--------------------------------------------------------------------------
# * Alias Listings
#--------------------------------------------------------------------------
alias_method :seph_autoscroll_gmmap_scrlg?, :scrolling?
alias_method :seph_autoscroll_gmmap_update, :update
#--------------------------------------------------------------------------
# * Scrolling?
#--------------------------------------------------------------------------
def scrolling?
return seph_autoscroll_gmmap_scrlg? || @autoscoll_rest != nil
end
#--------------------------------------------------------------------------
# * Frame Update
#--------------------------------------------------------------------------
def update
# If autoscroll target exist
if @scroll_target_x != nil
# Update autoscroll
update_autoscroll
end
# Original update
seph_autoscroll_gmmap_update
end
#--------------------------------------------------------------------------
# * Frame Update: Autoscroll
#--------------------------------------------------------------------------
def update_autoscroll
# If vertical scroll
if @display_x == @scroll_target_x
# Gets distance
distance = [@scroll_speed, (@scroll_target_y - @display_y).abs].min
# If scroll up
if @display_y > @scroll_target_y
scroll_up(distance)
# If scroll down
else
scroll_down(distance)
end
# If horizontal scroll
elsif @display_y == @scroll_target_y
# Gets distance
distance = [@scroll_speed, (@scroll_target_x - @display_x).abs].min
# If scroll left
if @display_x > @scroll_target_x
scroll_left(distance)
# If scroll right
else
scroll_right(distance)
end
# If diagonal scroll
else
# Get differences
diff_x = (@scroll_target_x - @display_x).abs
diff_y = (@scroll_target_y - @display_y).abs
# Reconfigure autoscroll rest
distance = Math.hypot(diff_x, diff_y)
# Gets distance
distance = [@scroll_speed, distance].min
# Gets angle
a = Math.atan2((@scroll_target_y - @display_y).abs,
(@scroll_target_x - @display_x).abs.to_f)
# Gets other triangle sides
so = Math.sin(a) * distance
sa = Math.cos(a) * distance
# Put both distances in array
sides = [sa, so]
# Get x and y distance moves
if diff_x > diff_y
x_move, y_move = sides.max, sides.min
else
x_move, y_move = sides.min, sides.max
end
# If move left
if @display_x > @scroll_target_x
scroll_left(x_move)
# If move right
else
scroll_right(x_move)
end
# If move up
if @display_y > @scroll_target_y
scroll_up(y_move)
else
scroll_down(y_move)
end
end
# If target reached
if @scroll_target_x == @display_x && @scroll_target_y == @display_y
@scroll_target_x, @scroll_target_y = nil, nil
end
end
#--------------------------------------------------------------------------
# * Scroll to character
#--------------------------------------------------------------------------
def scroll_to_character(id = 0, speed = Default_Scroll_Speed)
# Get character
if id == 0
chr = $game_player
elsif @events.has_key?(id)
chr = @events[id]
else
p 'Unable to find character for id: ' + id.to_s
return
end
# Scroll to target
scroll_to_target(chr.x, chr.y, speed)
end
#--------------------------------------------------------------------------
# * Scroll to object
#--------------------------------------------------------------------------
def scroll_to_object(object, speed = Default_Scroll_Speed)
# If x and y definable attributes
if object.respond_to?(:x) && object.respond_to?(:y)
# Scroll to target
scroll_to_target(object.x, object.y, speed)
end
# Return bad object
p 'Object undefined x and/or y method: ' + object.to_s
end
#--------------------------------------------------------------------------
# * Scroll to target
#--------------------------------------------------------------------------
def scroll_to_target(x, y, speed = Default_Scroll_Speed)
# Return if already scrolling
return if scrolling?
# If invalid position
if $game_map.valid?(x, y) == false
p 'Bad x and y position given: ' + x.to_s + ', ' + y.to_s
return
end
# Gets target display x & y
max_x = ($game_map.width - Map_Width) * 128
max_y = ($game_map.height - Map_Height) * 128
t_display_x = [0, [x * 128 - Game_Player::CENTER_X, max_x].min].max
t_display_y = [0, [y * 128 - Game_Player::CENTER_Y, max_y].min].max
# Get differences
diff_x = (t_display_x - @display_x).abs
diff_y = (t_display_y - @display_y).abs
# If x difference 0
if diff_x == 0
@autoscroll_rest = diff_y
# If y difference is 0
elsif diff_y == 0
@autoscroll_rest = diff_x
# If diagonal movement
else
@autoscroll_rest = Math.hypot(diff_x, diff_y)
end
# Save coordinates and speed
@scroll_target_x = t_display_x
@scroll_target_y = t_display_y
@scroll_speed = speed
end
end
#==============================================================================
# ** Interpreter
#==============================================================================
class Interpreter
#--------------------------------------------------------------------------
# * Scroll to character
#--------------------------------------------------------------------------
def scroll_to_character(id = 0, speed = Game_Map::Default_Scroll_Speed)
$game_map.scroll_to_character(id, speed)
end
#--------------------------------------------------------------------------
# * Scroll to object
#--------------------------------------------------------------------------
def scroll_to_object(object, speed = Game_Map::Default_Scroll_Speed)
$game_map.scroll_to_object(object, speed)
end
#--------------------------------------------------------------------------
# * Scroll to target
#--------------------------------------------------------------------------
def scroll_to_target(x, y, speed = Game_Map::Default_Scroll_Speed)
$game_map.scroll_to_target(x, y, speed)
end
end
#------------------------------------------------------------------------------
# * End SDK Enable Test
#------------------------------------------------------------------------------
end