TheoAllen

RGSS3 Basic SBS

Apr 26th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.16 KB | None | 0 0
  1. class Object
  2.  
  3.   def each_var
  4.     self.instance_variables.each do |varsymb|
  5.       yield self.instance_variable_get(varsymb)
  6.     end
  7.   end
  8.  
  9. end
  10.  
  11. module Theo
  12.   module Movement
  13.     class Move_Object
  14.      
  15.       def initialize(obj)
  16.         @obj = obj
  17.         clear_move_info
  18.       end
  19.    
  20.       def clear_move_info
  21.         @to_x = -1
  22.         @to_y = -1
  23.         @real_x = 0.0
  24.         @real_y = 0.0
  25.         @x_speed = 0.0
  26.         @y_speed = 0.0
  27.         @jump = 0.0
  28.         @jump_interval = 0.0
  29.         @offset = 0
  30.       end
  31.    
  32.       def move_to(x,y,jump,duration)
  33.         @to_x = x
  34.         @to_y = y
  35.         @real_x = @obj.x.to_f
  36.         @real_y = @obj.y.to_f
  37.         determine_speed(duration,jump)
  38.       end
  39.    
  40.       def determine_speed(duration,jump)
  41.         @x_speed = (@to_x - @obj.x) / duration.to_f
  42.         @y_speed = (@to_y - @obj.y) / duration.to_f
  43.         @jump = jump.to_f
  44.         @jump_interval = @jump/(duration/2.0)
  45.       end
  46.    
  47.       def moving?
  48.         return false if @to_x == -1 && @to_y == -1
  49.         result = @obj.x != @to_x || @obj.y != @to_y
  50.         return result
  51.       end
  52.    
  53.       def update_move
  54.         @real_x += @x_speed
  55.         @real_y += @y_speed
  56.         @obj.x = @real_x.round
  57.         @obj.y = @real_y.round + @offset.round
  58.         @jump -= @jump_interval
  59.         @offset -= @jump
  60.         clear_move_info unless moving?
  61.       end
  62.    
  63.     end
  64.  
  65.     def initialize(*args)
  66.       super(*args)
  67.       init_move_obj
  68.     end
  69.  
  70.     def init_move_obj
  71.       @move_obj = {}
  72.     end
  73.  
  74.     def can_move?(obj)
  75.       return obj.is_a?(Window) || obj.is_a?(Sprite)
  76.     end
  77.  
  78.     def set_obj(obj)
  79.       @move_obj[obj] = Move_Object.new(obj) if can_move?(obj)
  80.     end
  81.  
  82.     def del_obj(obj)
  83.       @move_obj.delete(obj)
  84.     end
  85.  
  86.     def move_to(obj,x,y,jump = 0.0,duration = Graphics.frame_rate)
  87.       return unless can_move?(obj)
  88.       set_obj(obj) unless @move_obj.include?(obj)
  89.       @move_obj[obj].move_to(x,y,jump,duration)
  90.     end
  91.  
  92.     def update_move_obj
  93.       @move_obj.each_value do |object|
  94.         object.update_move if object.moving?
  95.       end
  96.     end
  97.  
  98.     def is_moving?(obj)
  99.       return false unless @move_obj.include?(obj)
  100.       return @move_obj[obj].moving?
  101.     end
  102.    
  103.   end
  104. end
  105.  
  106. class Scene_Base
  107.  
  108.   include Theo::Movement
  109.  
  110.   alias theolized_basic_start start
  111.   def start
  112.     theolized_basic_start
  113.     self.each_var do |ivar|
  114.       set_obj(ivar)
  115.     end
  116.   end
  117.  
  118.   def update
  119.     update_basic
  120.     update_move_obj
  121.     if Input.trigger?(:CTRL)
  122.       self.each_var do |ivar|
  123.         next unless ivar.is_a?(Window) || ivar.is_a?(Sprite)
  124.         next if is_moving?(ivar)
  125.         x_switch = Graphics.width == ivar.width
  126.         y_switch = Graphics.height == ivar.height
  127.         x = x_switch ? 0 : rand(Graphics.width - ivar.width)
  128.         y = y_switch ? 0 : rand(Graphics.height - ivar.height)
  129.         move_to(ivar,x,y,10)
  130.       end
  131.     end
  132.   end
  133.  
  134.   def system
  135.     $game_system
  136.   end
  137.  
  138.   def party
  139.     $game_party
  140.   end
  141.  
  142.   def troop
  143.     $game_troop
  144.   end
  145.  
  146.   def map
  147.     $game_map
  148.   end
  149.  
  150.   def tileset
  151.     map.tileset
  152.   end
  153.  
  154. end
Advertisement
Add Comment
Please, Sign In to add comment