Class: Bullet

Inherits:
GameObject show all
Defined in:
lib/entities/bullet.rb

Overview

class Bullet

Instance Attribute Summary collapse

Attributes inherited from GameObject

#collider, #x, #y

Instance Method Summary collapse

Methods inherited from GameObject

#collide?, #draw, #hitted, #solid?

Constructor Details

#initialize(tank, powerful = false) ⇒ Bullet

Returns a new instance of Bullet.



9
10
11
12
13
14
15
# File 'lib/entities/bullet.rb', line 9

def initialize(tank, powerful = false)
  super(tank.canoonX, tank.canoonY, 1, 1, true)
  @direction = tank.direction
  @done = false
  @powerful = powerful
  @sprite = get_sprite
end

Instance Attribute Details

#powerfulObject (readonly)

Returns the value of attribute powerful.



7
8
9
# File 'lib/entities/bullet.rb', line 7

def powerful
  @powerful
end

Instance Method Details

#collision_detect(objects) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/entities/bullet.rb', line 27

def collision_detect(objects)
  objects.each do |o|
    return nil if o.is_a?(Enemy) && o.immortal?
    if collide?(o) && !o.is_a?(Water) && !o.is_a?(Upgrade)
      @done = true
      o.hitted(self)
      return Explosion.new(@x, @y)
    end
  end
  nil
end

#done?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/entities/bullet.rb', line 23

def done?
  @done
end

#get_spriteObject



51
52
53
54
55
56
# File 'lib/entities/bullet.rb', line 51

def get_sprite
  return Sprite.new(:bullet_left, false) if @direction == :left
  return Sprite.new(:bullet_up, false) if @direction == :up
  return Sprite.new(:bullet_down, false) if @direction == :down
  return Sprite.new(:bullet_right, false) if @direction == :right
end

#moveObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/entities/bullet.rb', line 39

def move
  @x -= 2 if @direction == :left
  @x += 2 if @direction == :right
  @y += 2 if @direction == :down
  @y -= 2 if @direction == :up
  @done = true if @x < 0
  @done = true if @y < 0
  @done = true if @x > 240
  @done = true if @y > 208
  @collider.update(@x, @y)
end

#updateObject



17
18
19
20
21
# File 'lib/entities/bullet.rb', line 17

def update
  return if done?
  move
  @sprite.update
end