TheoAllen

Iseng2 RGSS3 (11) - Smooth Sliding

Apr 30th, 2014
661
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.08 KB | None | 0 0
  1. #==============================================================================
  2. # ** Smooth_Slide
  3. #------------------------------------------------------------------------------
  4. #  This module provides basic module for smooth sliding. It can be implemented
  5. # to any classes as long as they have x and y.
  6. #==============================================================================
  7. module Smooth_Slide
  8.   # ---------------------------------------------------------------------------
  9.   # Initialize smooth movement data
  10.   # ---------------------------------------------------------------------------
  11.   def init_smove
  12.     @smooth_dur = 0     # Travel duration
  13.     @accel_x = 0.0      # Acceleration X
  14.     @accel_y = 0.0      # Acceleration Y
  15.     @vel_x = 0.0        # Velocity X
  16.     @vel_y = 0.0        # Velocity Y
  17.     @sreal_x = self.x   # Real position X
  18.     @sreal_y = self.y   # Real Position Y
  19.     @starget_x = 0.0    # Target X
  20.     @starget_y = 0.0    # Target Y
  21.     @srev = false       # Reverse Flag
  22.     @smove = false      # Moving Flag
  23.    
  24.     # The reason why I give 's' in front of most instance variables is for
  25.     # uniqueness. Well, if there is any basic module / script out there that
  26.     # also use same instance name, it may causes incompatibility
  27.    
  28.   end
  29.   # ---------------------------------------------------------------------------
  30.   # Do smooth movement
  31.   # ---------------------------------------------------------------------------
  32.   def smooth_move(x,y,dur,reverse = false)
  33.     init_smove
  34.     @srev = reverse
  35.     @smove = true
  36.     @smooth_dur = dur
  37.     @sreal_x = self.x.to_f
  38.     @sreal_y = self.y.to_f
  39.     @starget_x = x
  40.     @starget_y = y
  41.     calc_accel
  42.   end
  43.   # ---------------------------------------------------------------------------
  44.   # Do smooth slide
  45.   # ---------------------------------------------------------------------------
  46.   def smooth_slide(x,y,dur,reverse = false)
  47.     tx = x + self.x
  48.     ty = y + self.y
  49.     smooth_move(tx,ty,dur,reverse)
  50.   end
  51.   # ---------------------------------------------------------------------------
  52.   # Calculate acceleration
  53.   # ---------------------------------------------------------------------------
  54.   def calc_accel
  55.     # Get travel distance
  56.     dist_x = @starget_x - @sreal_x
  57.     dist_y = @starget_y - @sreal_y
  58.    
  59.     # Calculate acceleration
  60.     @accel_x = (dist_x/((@smooth_dur**2)/2.0))
  61.     @accel_y = (dist_y/((@smooth_dur**2)/2.0))
  62.    
  63.     # If reverse, velocity started on max speed
  64.     if @srev
  65.       @vel_x = (@accel_x * @smooth_dur)
  66.       @vel_y = (@accel_y * @smooth_dur)
  67.     end
  68.   end
  69.   # ---------------------------------------------------------------------------
  70.   # Execute end smooth
  71.   # ---------------------------------------------------------------------------
  72.   def end_smooth_slide
  73.     init_smove
  74.     puts "#{self.x} -- #{self.y}"
  75.   end
  76.   # ---------------------------------------------------------------------------
  77.   # Update Smooth movement
  78.   # ---------------------------------------------------------------------------
  79.   def update_smove
  80.     if @smove
  81.       # If not reversed. Increase velocity on the first time executed
  82.       if !@srev
  83.         @vel_x += @accel_x
  84.         @vel_y += @accel_y
  85.       end
  86.      
  87.       # Give increment / decrement upon real position
  88.       @sreal_x = (@starget_x > @sreal_x ?
  89.         [@sreal_x + @vel_x, @starget_x].min :
  90.         [@sreal_x + @vel_x, @starget_x].max)
  91.       @sreal_y = (@starget_y > @sreal_y ?
  92.         [@sreal_y + @vel_y, @starget_y].min :
  93.         [@sreal_y + @vel_y, @starget_y].max)
  94.      
  95.       # Rounded real position
  96.       self.x = @sreal_x.round
  97.       self.y = @sreal_y.round
  98.      
  99.       # If reversed, decrease velocity after make a change to real position
  100.       if @srev
  101.         @vel_x -= @accel_x
  102.         @vel_y -= @accel_y
  103.       end
  104.      
  105.       # If target has reach it's destination, call end execution
  106.       if (self.x == @starget_x && self.y == @starget_y) || @smooth_dur == 0
  107.         end_smooth_slide
  108.       end
  109.     end
  110.   end
  111.   # ---------------------------------------------------------------------------
  112.   # Get smooth moving flag
  113.   # ---------------------------------------------------------------------------
  114.   def smoving?
  115.     @smove
  116.   end
  117.  
  118. end
  119.  
  120. class Sprite
  121.   include Smooth_Slide
  122.  
  123.   alias smooth_init initialize
  124.   def initialize(v=nil)
  125.     smooth_init(v)
  126.     init_smove
  127.   end
  128.  
  129.   alias smooth_update update
  130.   def update
  131.     smooth_update
  132.     update_smove
  133.   end
  134.  
  135. end
  136.  
  137. class Window
  138.   include Smooth_Slide
  139.  
  140.   alias smooth_init initialize
  141.   def initialize(*args)
  142.     smooth_init(*args)
  143.     init_smove
  144.   end
  145.  
  146.   alias smooth_update update
  147.   def update
  148.     smooth_update
  149.     update_smove
  150.   end
  151.  
  152. end
  153.  
  154. def updates
  155.   Graphics.update
  156.   Input.update
  157.   @spr.update
  158. #~   puts "#{@spr.x} -- #{@spr.y}"
  159.   if Input.trigger?(:C)
  160.     @spr.smooth_slide(300,100,24,true)
  161.   elsif Input.trigger?(:B)
  162.     @spr.smooth_move(50,100,60)
  163.   end
  164. end
  165.  
  166. @spr = Sprite.new
  167. @spr.bitmap = Cache.character("Actor1")
  168. @spr.src_rect.set(Rect.new(0,0,32,32))
  169.  
  170. updates while true
Advertisement
Add Comment
Please, Sign In to add comment