Class: Bullet
- Inherits:
-
Object
- Object
- Bullet
- Defined in:
- lib/rrobots/bullet.rb
Instance Attribute Summary collapse
-
#dead ⇒ Object
Returns the value of attribute dead.
-
#energy ⇒ Object
Returns the value of attribute energy.
-
#heading ⇒ Object
Returns the value of attribute heading.
-
#origin ⇒ Object
Returns the value of attribute origin.
-
#speed ⇒ Object
Returns the value of attribute speed.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
-
#initialize(bf, x, y, heading, speed, energy, origin) ⇒ Bullet
constructor
A new instance of Bullet.
- #state ⇒ Object
- #tick ⇒ Object
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
#dead ⇒ Object
Returns the value of attribute dead.
7 8 9 |
# File 'lib/rrobots/bullet.rb', line 7 def dead @dead end |
#energy ⇒ Object
Returns the value of attribute energy.
6 7 8 |
# File 'lib/rrobots/bullet.rb', line 6 def energy @energy end |
#heading ⇒ Object
Returns the value of attribute heading.
4 5 6 |
# File 'lib/rrobots/bullet.rb', line 4 def heading @heading end |
#origin ⇒ Object
Returns the value of attribute origin.
8 9 10 |
# File 'lib/rrobots/bullet.rb', line 8 def origin @origin end |
#speed ⇒ Object
Returns the value of attribute speed.
5 6 7 |
# File 'lib/rrobots/bullet.rb', line 5 def speed @speed end |
#x ⇒ Object
Returns the value of attribute x.
2 3 4 |
# File 'lib/rrobots/bullet.rb', line 2 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
3 4 5 |
# File 'lib/rrobots/bullet.rb', line 3 def y @y end |
Instance Method Details
#state ⇒ Object
16 17 18 |
# File 'lib/rrobots/bullet.rb', line 16 def state {:x=>x, :y=>y, :energy=>energy} end |
#tick ⇒ Object
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 |