Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pool = Struct.new(:name, :size, :config, :z)
- # Example config
- module Pool1
- START_TIMER = 100
- SPAWN_X = 72
- SPAWN_Y = 44
- FADEOUT = 50
- # Float range
- SPAWN_WAIT = 0.05..0.1
- DELTA_X = 1.5..4.0
- DELTA_Y = -0.65..0.65
- # Integer range
- LIFETIME = 150..300
- end
- class SpritePool
- def initialize(pool)
- @bitmap = RPG::Cache.picture(pool.name)
- @size = pool.size
- @z = pool.z
- @config = pool.config
- @start_timer = @config::START_TIMER
- create_pool
- @tick = 0
- @spawntimer = float_random(@config::SPAWN_WAIT)
- end
- def create_pool
- @pool = []
- @active = []
- @available = []
- for i in 0...@size
- @pool << create_sprite
- @available << i
- end
- end
- def create_sprite
- sprite = SimpleSprite.new
- sprite.fadeout = @config::FADEOUT
- sprite.bitmap = @bitmap
- sprite.z = @z
- return sprite
- end
- def spawn_sprite
- index = find_next!
- unless index.nil?
- sprite = @pool[index]
- @active << index
- start_sprite(sprite)
- end
- end
- def start_sprite(sprite)
- sprite.dx = float_random(@config::DELTA_X)
- sprite.dy = float_random(@config::DELTA_Y)
- sprite.x = @config::SPAWN_X
- sprite.y = @config::SPAWN_Y
- sprite.opacity = 255
- sprite.visible = true
- sprite.start(float_random(@config::LIFETIME))
- end
- def float_random(range)
- multiplier = range.last - range.first
- offset = range.first
- return rand * multiplier + offset
- end
- def dispose
- @bitmap = nil
- @pool.each {|sprite| sprite.dispose}
- end
- def disposed?
- return @bitmap.nil?
- end
- def visible=(value)
- end
- def update
- if @start_timer > 0
- @start_timer -= 1
- else
- update_active
- tick
- end
- end
- def update_active
- for index in @active
- sprite = @pool[index]
- sprite.update
- if sprite.dead?
- dead_sprite(index)
- end
- end
- end
- def dead_sprite(index)
- @active.delete(index)
- @available.push(index)
- end
- def tick
- @tick += 1
- while @tick > @spawntimer
- @tick -= @spawntimer
- @spawntimer = float_random(@config::SPAWN_WAIT)
- spawn_sprite
- end
- end
- ##
- # Destructivily find an available index
- # Returns nil if the pool is fully in use
- #
- def find_next!
- return @available.pop
- end
- end
- class SimpleSprite < Sprite
- attr_sec_accessor :lifetime, 0
- attr_sec_accessor :fadeout, 20
- attr_sec_accessor :dx, 0.0
- attr_sec_accessor :dy, 0.0
- def initialize(*args)
- super(*args)
- self.visible = false
- end
- def start(lifetime)
- @fx = self.x.to_f
- @fy = self.y.to_f
- self.lifetime = lifetime
- self.visible = true
- end
- def update
- if lifetime > 0
- tick
- end
- end
- def dead?
- self.lifetime <= 0
- end
- def tick
- update_opacity if lifetime < fadeout
- update_movement
- self.lifetime -= 1
- if self.lifetime <= 0
- self.visible = false
- end
- end
- def update_opacity
- new_opacity = (lifetime.to_f / fadeout) * 255
- self.opacity = new_opacity.to_i
- end
- def update_movement
- @fx += @dx
- @fy += @dy
- self.x = @fx.to_i
- self.y = @fy.to_i
- end
- end
Add Comment
Please, Sign In to add comment