I'm using Trickster's moving window script and was wondering if there is a way to decrease the speed of the window as it gets closer to the destination.
I've tried putting @move_speed -= 1 in the last def, but it didn't work so well (the window did slow down, but the speed became negative and travel the opposite way ':| )
Thanks in advance :thumb:
I've tried putting @move_speed -= 1 in the last def, but it didn't work so well (the window did slow down, but the speed became negative and travel the opposite way ':| )
Thanks in advance :thumb:
Code:
class Numeric
def sign
return 0 if self.zero?
return (self / self.abs).to_i
end
end
class Window_Base
#--------------------------------------------------------------------------
# * Move the sprite
# x : x coordinate of the destination point
# y : y coordinate of the destination point
# speed : Speed of movement
#--------------------------------------------------------------------------
def move(x, y, speed = 1)
# Set Destination Points speed and move count set moving flag
@destination_x = x
@destination_y = y
@move_speed = speed
@moving = true
# Get the distance + (negative if to left or up positive if down or right)
@distance_x = (@destination_x - self.x).to_f
@distance_y = (@destination_y - self.y).to_f
# Get slant distance (hypotenuse of the triangle (xf,yi) (xi,yf) and (xf,yf))
@distance = Math.sqrt(@distance_x ** 2 + @distance_y ** 2)
# Calculate angle of movement which is later used to determine direction
# If X distance is 0 (Prevent Infinity Error)
if @distance_x == 0
# The Angle is sign(distance_y) * - π / 2 (90° or 270°)
@angle = @distance_y.sign * Math::PI / 2
# If Y distance is 0 (Prevent Incorrect Direction for later)
elsif @distance_y == 0
# The Angle is sign(distance_x) - 1 * π / 2 (0° or 180°)
@angle = (@distance_x.sign - 1) * Math::PI / 2
else
# The Angle is the Arctangent of @distance_y / @distance_x (slope)
# Returns [-Ï€,Ï€]
@angle = Math.atan2(@distance_y, @distance_x.to_f)
end
# Convert the angle to degrees
@angle *= 180 / Math::PI
end
#--------------------------------------------------------------------------
# * Update Move
#--------------------------------------------------------------------------
alias moving_window_update update
def update
moving_window_update
update_move
end
#--------------------------------------------------------------------------
# * Moving?
#--------------------------------------------------------------------------
def moving?
return @moving
end
#--------------------------------------------------------------------------
# * Update Move
#--------------------------------------------------------------------------
def update_move
# If not moving
if (self.x == @destination_x and self.y == @destination_y) or not @moving
@moving = false
return
end
# move increase x = the cosine of the arctangent of the dist x over dist y
# move increase x = cos(arctan(disty/distx)) simplified by trigonometry
# to distance_x / slant_distance, the sprite moves (move speed)
# along the slanted line (if it is slanted)
movinc_x = @move_speed * @distance_x.abs / @distance
# same reasoning with y increase except it is the sine of the arctangent
# move increase y = sin(arctan(disty/distx)) simplified by trigonometry
# to distance_y / slant_distance
movinc_y = @move_speed * @distance_y.abs / @distance
# Move the sign of the distance left + move increase or the remaining distance
# left if it will go past that point
if @move_speed != 0
# Get distance remaining
remain_x = (@destination_x - self.x).abs
remain_y = (@destination_y - self.y).abs
self.x += (@destination_x - self.x).sign * [movinc_x.ceil, remain_x].min
self.y += (@destination_y - self.y).sign * [movinc_y.ceil, remain_y].min
end
# If Destination Reached stop moving
if self.x == @destination_x and self.y == @destination_y
@moving = false
return
end
end
end