Class: Bullet

Inherits:
Object
  • Object
show all
Defined in:
lib/rrobots/bullet.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/rrobots/bullet.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/rrobots/bullet.rb', line 7

def dead
  @dead
end

#energyObject

Returns the value of attribute energy.



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

def energy
  @energy
end

#headingObject

Returns the value of attribute heading.



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

def heading
  @heading
end

#originObject

Returns the value of attribute origin.



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

def origin
  @origin
end

#speedObject

Returns the value of attribute speed.



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

def speed
  @speed
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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

def y
  @y
end

Instance Method Details

#stateObject



16
17
18
# File 'lib/rrobots/bullet.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
# File 'lib/rrobots/bullet.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
end