Class: Bullet

Inherits:
Object
  • Object
show all
Defined in:
lib/bullets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bf, x, y, heading, speed, energy, origin) ⇒ Bullet

Returns a new instance of Bullet.



10
11
12
13
14
# File 'lib/bullets.rb', line 10

def initialize bf, x, y, heading, speed, energy, origin
  @x, @y, @heading, @origin = x, y, heading, origin
  @speed, @energy = speed, energy
  @battlefield, dead = bf, false
end

Instance Attribute Details

#deadObject

Returns the value of attribute dead.



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

def dead
  @dead
end

#energyObject

Returns the value of attribute energy.



6
7
8
# File 'lib/bullets.rb', line 6

def energy
  @energy
end

#headingObject

Returns the value of attribute heading.



4
5
6
# File 'lib/bullets.rb', line 4

def heading
  @heading
end

#originObject

Returns the value of attribute origin.



8
9
10
# File 'lib/bullets.rb', line 8

def origin
  @origin
end

#speedObject

Returns the value of attribute speed.



5
6
7
# File 'lib/bullets.rb', line 5

def speed
  @speed
end

#xObject

Returns the value of attribute x.



2
3
4
# File 'lib/bullets.rb', line 2

def x
  @x
end

#yObject

Returns the value of attribute y.



3
4
5
# File 'lib/bullets.rb', line 3

def y
  @y
end

Instance Method Details

#stateObject



16
17
18
# File 'lib/bullets.rb', line 16

def state
  {:x=>x, :y=>y, :energy=>energy}
end

#tickObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bullets.rb', line 20

def tick
  return if @dead
  @x += Math::cos(@heading.to_rad) * @speed
  @y -= Math::sin(@heading.to_rad) * @speed

  @dead ||= (@x < 0) || (@x >= @battlefield.width)
  @dead ||= (@y < 0) || (@y >= @battlefield.height)

  @battlefield.robots.each do |other|
    if (other != origin) && (Math.hypot(@y - other.y, other.x - @x) < 40) && (!other.dead)
      explosion = Explosion.new(@battlefield, other.x, other.y)
      @battlefield << explosion
      damage = other.hit(self)
      origin.damage_given += damage
      origin.kills += 1 if other.dead
      @dead = true
    end
  end
  
  @battlefield.mines.each do |mine|
    if (Math.hypot(@y - mine.y, mine.x - @x) < 10) && (!mine.dead) && (mine.origin != origin) then
      explosion = Explosion.new(@battlefield, mine.x, mine.y)
      @battlefield << explosion
      mine.destroy if self.energy > @battlefield.config.mines[:bullet_energy_resistance]
      origin.destroyed_mines += 1
      @dead = true
    end
  end
  
end