Class: Bullet

Inherits:
Chingu::GameObject
  • Object
show all
Includes:
GamespacePersistence
Defined in:
lib/prkwars/bullet.rb

Overview

Class representing a bullet shot by the player. Destroys itself if is present out of bounds.

Instance Method Summary collapse

Methods included from GamespacePersistence

#correct_coords, #in_bounds

Constructor Details

#initialize(gamespace, options = {}) ⇒ Bullet

Initializes the gamespace where the bullet is present in. Velocity is passed as a paremeter in the options hash



18
19
20
21
22
23
24
# File 'lib/prkwars/bullet.rb', line 18

def initialize(gamespace, options = {})
  super(options)
  @image = Image['media/bullet.png']
  @gamespace = gamespace

  cache_bounding_box
end

Instance Method Details

#updateObject

Updating position is not necessary thanks to the velocity trait. In case the bullet is out of bounds, it gets destroyed and explosion particles are spawned.



31
32
33
34
35
36
37
38
39
40
# File 'lib/prkwars/bullet.rb', line 31

def update
  return if in_bounds(self, @gamespace)

  2.times do
    ExplosionParticle.create(@gamespace,
                             x: @x, y: @y,
                             zorder: ZOrder::GAMEOBJECT)
    destroy!
  end
end