TheoAllen

Smooth Move example

May 1st, 2015
492
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.13 KB | None | 0 0
  1. module TSBS
  2.   #============================================================================
  3.   # Easing movement just in case. Thanks to Galenmereth
  4.   # ---------------------------------------------------------------------------
  5.   # time      = Current time (frame)
  6.   # start     = Start value
  7.   # change    = Desired change in value
  8.   # duration  = Duration total (frames)
  9.   # Returns: Value modified by t
  10.   # ---------------------------------------------------------------------------
  11.   # * Easing movement method : Linear
  12.   # ---------------------------------------------------------------------------
  13.   def self.linear(time, start, change, total_time)
  14.     return change * time / total_time + start
  15.   end
  16.   # ---------------------------------------------------------------------------
  17.   # * Easing movement method : Smooth in
  18.   # ---------------------------------------------------------------------------
  19.   def self.smooth_in(time, start, change, total_time)
  20.     time /= total_time.to_f
  21.     return change*time*time*time + start
  22.   end
  23.   # ---------------------------------------------------------------------------
  24.   # * Easing movement method : Smooth out
  25.   # ---------------------------------------------------------------------------
  26.   def self.smooth_out(time, start, change, total_time)
  27.     time /= total_time.to_f
  28.     time -= 1
  29.     return change*(time*time*time + 1) + start
  30.   end
  31. end
  32.  
  33. class Sprite_Njajal < Sprite
  34.  
  35.   def initialize
  36.     super()
  37.     self.bitmap = Bitmap.new(32,32)
  38.     bitmap.fill_rect(bitmap.rect, Color.new(255,255,255))
  39.     self.x = 300
  40.     self.y = 200
  41.     @dur = 0
  42.     @time = 0
  43.   end
  44.  
  45.   def update
  46.     super
  47.     if @dur != @time
  48.       self.x = TSBS.smooth_out(@time, @ori_x, @tx - @ori_x, @dur)
  49.       self.y = TSBS.smooth_out(@time, @ori_y, @ty - @ori_y, @dur)
  50.       @time += 1
  51.     end
  52.   end
  53.  
  54.   def smooth(tx,ty,dur)
  55.     @ori_x = self.x
  56.     @ori_y = self.y
  57.     @tx = tx
  58.     @ty = ty
  59.     @dur = dur
  60.     @time = 0
  61.   end
  62.  
  63. end
  64.  
  65. $sprite = Sprite_Njajal.new
  66.  
  67. loop do
  68.   Graphics.update
  69.   Input.update
  70.   $sprite.update
  71.   if Input.trigger?(:C)
  72.     $sprite.smooth(100,100,60)
  73.   end
  74. end
Advertisement
Add Comment
Please, Sign In to add comment