Moving Windows
#1
Moving Windows
Version: 1.0


Introduction
This scriptlet adds a few functions to the Window_Base class that allows windows to move.


Script
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


Instructions
Add in a new script above main

to move a window just call
.move(x, y, speed)

where x is the destination x coordinate
y is the destination y coordinate
speed is the speed of the movement

Other functions are
.moving? => True if window is moving false otherwise

Within the Window Class
@angle => returns the angle of the movement in degrees


FAQ
Note: Permission granted by Trickster to post:
Quote:And if you post what you have now of my stuff then you don't have the latest versions. I'm too lazy/busy to post stuff.
As this is his material, it is deletable upon his request. Due to his current absense, no support is available. Please do not PM or eMail him for support.


Compatibility
Should be compatible with any script


Credits and Thanks
J-Street for requesting.


Terms and Conditions
Hey, I posted this publicly. You can use it. What do you expect? But if you do use it, I do expect you to spell my name correctly in your game. And yes you can use it in commercial games too.
Reply }


Possibly Related Threads…
Thread Author Replies Views Last Post
   Auto-Sizing Text Windows RPG Advocate 9 19,170 06-11-2011, 06:18 PM
Last Post: sagam12
   Popup Windows deValdr 5 10,970 06-02-2011, 09:38 AM
Last Post: deValdr
   Command Windows - Animated Text Selwyn 0 4,394 03-03-2008, 06:18 AM
Last Post: Selwyn



Users browsing this thread: 1 Guest(s)