Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Moving windows

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:
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
 
that is only good for windows that are no wider than 30 ~ 50 pixels, because with that code, the window will slows down really quickly and then suddenly moves real slow.
 

arev

Sponsor

OK, try this:
Code:
@var_speed = Math.sqrt((@destination_x - self.x)*(@destination_x - self.x) + (@destination_y - self.y)*(@destination_y - self.y))
@move_speed = [@var_speed/2, 1].max
 
Before I resolve this

Code:
var_speed = Math.sqrt((@destination_x - self.x)**2 + (@destination_y - self.y)**2)
@move_speed = [var_speed/2, 1].max

Rewritten version of arevulopapo's code

If I come up with something else I'll post it here

STOP STEALING MY CHANCE TO DO MATH :p
 
hehe

another question, if I use the .move in the menu scene and press ESC (to return to scene_map), how can I make it so that the windows moves out from the screen before they are disposed?
 
not sure what you meant by run Scene_map, but I got it working anyway ^^
Thanks for your help! :D

(*for anyone to wants to know how I did it ==> )what I did was set the .move after the "break if main_scenechange?" loop. Then make another loop like this~
Code:
loop do
      main_loop
     break if @command_window.moving? == false && @playtime_window.moving? == false && @gold_window.moving? == false && @status_window.moving? == false
    end
 
Ok GIVE ME A BREAK, Sheesh. I do have a life you know I can't go around answering questions as soon as I see them because I don't have that much time. I do make notes and come back to it later, I'm in between scripts and requests I'm doing, college, friends, family, moderating, etc.

Also you should have some code to get the windows moving first try posting that and explain what you are trying to do, since it is stated in that thread that it is a tool for scripters.

Also sorry for the warning my mistake, but don't go bringing it to public attention or else you will get in trouble for that.


EDIT: Err actually in the status window the windows are not updated that is the reason why they don't move you will have to add this to the update method

Code:
@status_window.update
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top