Zeriab

[RGSS] Sprite pool

Sep 3rd, 2021 (edited)
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.27 KB | None | 0 0
  1. Pool = Struct.new(:name, :size, :config, :z)
  2.  
  3. # Example config
  4. module Pool1
  5.   START_TIMER = 100
  6.   SPAWN_X = 72
  7.   SPAWN_Y = 44
  8.   FADEOUT = 50
  9.   # Float range
  10.   SPAWN_WAIT = 0.05..0.1
  11.   DELTA_X = 1.5..4.0
  12.   DELTA_Y = -0.65..0.65
  13.   # Integer range
  14.   LIFETIME = 150..300
  15. end
  16.  
  17. class SpritePool
  18.   def initialize(pool)
  19.     @bitmap = RPG::Cache.picture(pool.name)
  20.     @size = pool.size
  21.     @z = pool.z
  22.     @config = pool.config
  23.     @start_timer = @config::START_TIMER
  24.     create_pool
  25.     @tick = 0
  26.     @spawntimer = float_random(@config::SPAWN_WAIT)
  27.   end
  28.  
  29.   def create_pool
  30.     @pool = []
  31.     @active = []
  32.     @available = []
  33.     for i in 0...@size
  34.       @pool << create_sprite
  35.       @available << i
  36.     end
  37.   end
  38.  
  39.   def create_sprite
  40.     sprite = SimpleSprite.new
  41.     sprite.fadeout = @config::FADEOUT
  42.     sprite.bitmap = @bitmap
  43.     sprite.z = @z
  44.     return sprite
  45.   end
  46.  
  47.   def spawn_sprite
  48.     index = find_next!
  49.     unless index.nil?
  50.       sprite = @pool[index]
  51.       @active << index
  52.       start_sprite(sprite)
  53.     end
  54.   end
  55.  
  56.   def start_sprite(sprite)
  57.     sprite.dx = float_random(@config::DELTA_X)
  58.     sprite.dy = float_random(@config::DELTA_Y)
  59.     sprite.x = @config::SPAWN_X
  60.     sprite.y = @config::SPAWN_Y
  61.     sprite.opacity = 255
  62.     sprite.visible = true
  63.     sprite.start(float_random(@config::LIFETIME))
  64.   end
  65.  
  66.   def float_random(range)
  67.     multiplier = range.last - range.first
  68.     offset = range.first
  69.     return rand * multiplier + offset
  70.   end
  71.  
  72.   def dispose
  73.     @bitmap = nil
  74.     @pool.each {|sprite| sprite.dispose}
  75.   end
  76.  
  77.   def disposed?
  78.     return @bitmap.nil?
  79.   end
  80.  
  81.   def visible=(value)
  82.   end
  83.    
  84.   def update
  85.     if @start_timer > 0
  86.       @start_timer -= 1
  87.     else
  88.       update_active
  89.       tick
  90.     end
  91.   end
  92.  
  93.   def update_active
  94.     for index in @active
  95.       sprite = @pool[index]
  96.       sprite.update
  97.       if sprite.dead?
  98.         dead_sprite(index)
  99.       end
  100.     end
  101.   end
  102.  
  103.   def dead_sprite(index)
  104.     @active.delete(index)
  105.     @available.push(index)
  106.   end
  107.  
  108.   def tick
  109.     @tick += 1
  110.     while @tick > @spawntimer
  111.       @tick -= @spawntimer
  112.       @spawntimer = float_random(@config::SPAWN_WAIT)
  113.       spawn_sprite
  114.     end
  115.   end
  116.  
  117.   ##
  118.   # Destructivily find an available index
  119.   # Returns nil if the pool is fully in use
  120.   #
  121.   def find_next!
  122.     return @available.pop
  123.   end
  124. end
  125.  
  126. class SimpleSprite < Sprite
  127.   attr_sec_accessor :lifetime, 0
  128.   attr_sec_accessor :fadeout, 20
  129.   attr_sec_accessor :dx, 0.0
  130.   attr_sec_accessor :dy, 0.0
  131.  
  132.   def initialize(*args)
  133.     super(*args)
  134.     self.visible = false
  135.   end
  136.  
  137.   def start(lifetime)
  138.     @fx = self.x.to_f
  139.     @fy = self.y.to_f
  140.     self.lifetime = lifetime
  141.     self.visible = true
  142.   end
  143.  
  144.   def update
  145.     if lifetime > 0
  146.       tick
  147.     end
  148.   end
  149.  
  150.   def dead?
  151.     self.lifetime <= 0
  152.   end
  153.  
  154.   def tick
  155.     update_opacity if lifetime < fadeout
  156.     update_movement
  157.     self.lifetime -= 1
  158.     if self.lifetime <= 0
  159.       self.visible = false
  160.     end
  161.   end
  162.  
  163.   def update_opacity
  164.     new_opacity = (lifetime.to_f / fadeout) * 255
  165.     self.opacity = new_opacity.to_i
  166.   end
  167.  
  168.   def update_movement
  169.     @fx += @dx
  170.     @fy += @dy
  171.     self.x = @fx.to_i
  172.     self.y = @fy.to_i
  173.   end
  174. end
  175.  
Add Comment
Please, Sign In to add comment