Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # ** Smooth_Slide
- #------------------------------------------------------------------------------
- # This module provides basic module for smooth sliding. It can be implemented
- # to any classes as long as they have x and y.
- #==============================================================================
- module Smooth_Slide
- # ---------------------------------------------------------------------------
- # Initialize smooth movement data
- # ---------------------------------------------------------------------------
- def init_smove
- @smooth_dur = 0 # Travel duration
- @accel_x = 0.0 # Acceleration X
- @accel_y = 0.0 # Acceleration Y
- @vel_x = 0.0 # Velocity X
- @vel_y = 0.0 # Velocity Y
- @sreal_x = self.x # Real position X
- @sreal_y = self.y # Real Position Y
- @starget_x = 0.0 # Target X
- @starget_y = 0.0 # Target Y
- @srev = false # Reverse Flag
- @smove = false # Moving Flag
- # The reason why I give 's' in front of most instance variables is for
- # uniqueness. Well, if there is any basic module / script out there that
- # also use same instance name, it may causes incompatibility
- end
- # ---------------------------------------------------------------------------
- # Do smooth movement
- # ---------------------------------------------------------------------------
- def smooth_move(x,y,dur,reverse = false)
- init_smove
- @srev = reverse
- @smove = true
- @smooth_dur = dur
- @sreal_x = self.x.to_f
- @sreal_y = self.y.to_f
- @starget_x = x
- @starget_y = y
- calc_accel
- end
- # ---------------------------------------------------------------------------
- # Do smooth slide
- # ---------------------------------------------------------------------------
- def smooth_slide(x,y,dur,reverse = false)
- tx = x + self.x
- ty = y + self.y
- smooth_move(tx,ty,dur,reverse)
- end
- # ---------------------------------------------------------------------------
- # Calculate acceleration
- # ---------------------------------------------------------------------------
- def calc_accel
- # Get travel distance
- dist_x = @starget_x - @sreal_x
- dist_y = @starget_y - @sreal_y
- # Calculate acceleration
- @accel_x = (dist_x/((@smooth_dur**2)/2.0))
- @accel_y = (dist_y/((@smooth_dur**2)/2.0))
- # If reverse, velocity started on max speed
- if @srev
- @vel_x = (@accel_x * @smooth_dur)
- @vel_y = (@accel_y * @smooth_dur)
- end
- end
- # ---------------------------------------------------------------------------
- # Execute end smooth
- # ---------------------------------------------------------------------------
- def end_smooth_slide
- init_smove
- puts "#{self.x} -- #{self.y}"
- end
- # ---------------------------------------------------------------------------
- # Update Smooth movement
- # ---------------------------------------------------------------------------
- def update_smove
- if @smove
- # If not reversed. Increase velocity on the first time executed
- if !@srev
- @vel_x += @accel_x
- @vel_y += @accel_y
- end
- # Give increment / decrement upon real position
- @sreal_x = (@starget_x > @sreal_x ?
- [@sreal_x + @vel_x, @starget_x].min :
- [@sreal_x + @vel_x, @starget_x].max)
- @sreal_y = (@starget_y > @sreal_y ?
- [@sreal_y + @vel_y, @starget_y].min :
- [@sreal_y + @vel_y, @starget_y].max)
- # Rounded real position
- self.x = @sreal_x.round
- self.y = @sreal_y.round
- # If reversed, decrease velocity after make a change to real position
- if @srev
- @vel_x -= @accel_x
- @vel_y -= @accel_y
- end
- # If target has reach it's destination, call end execution
- if (self.x == @starget_x && self.y == @starget_y) || @smooth_dur == 0
- end_smooth_slide
- end
- end
- end
- # ---------------------------------------------------------------------------
- # Get smooth moving flag
- # ---------------------------------------------------------------------------
- def smoving?
- @smove
- end
- end
- class Sprite
- include Smooth_Slide
- alias smooth_init initialize
- def initialize(v=nil)
- smooth_init(v)
- init_smove
- end
- alias smooth_update update
- def update
- smooth_update
- update_smove
- end
- end
- class Window
- include Smooth_Slide
- alias smooth_init initialize
- def initialize(*args)
- smooth_init(*args)
- init_smove
- end
- alias smooth_update update
- def update
- smooth_update
- update_smove
- end
- end
- def updates
- Graphics.update
- Input.update
- @spr.update
- #~ puts "#{@spr.x} -- #{@spr.y}"
- if Input.trigger?(:C)
- @spr.smooth_slide(300,100,24,true)
- elsif Input.trigger?(:B)
- @spr.smooth_move(50,100,60)
- end
- end
- @spr = Sprite.new
- @spr.bitmap = Cache.character("Actor1")
- @spr.src_rect.set(Rect.new(0,0,32,32))
- updates while true
Advertisement
Add Comment
Please, Sign In to add comment