Class: Battlefield
- Inherits:
-
Object
- Object
- Battlefield
- Defined in:
- lib/rrobots/battlefield.rb
Instance Attribute Summary collapse
-
#bullets ⇒ Object
readonly
Returns the value of attribute bullets.
-
#explosions ⇒ Object
readonly
Returns the value of attribute explosions.
-
#game_over ⇒ Object
readonly
Returns the value of attribute game_over.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#robots ⇒ Object
readonly
Returns the value of attribute robots.
-
#seed ⇒ Object
readonly
Returns the value of attribute seed.
-
#teams ⇒ Object
readonly
Returns the value of attribute teams.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#timeout ⇒ Object
readonly
how many ticks the match can go before ending.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #<<(object) ⇒ Object
-
#initialize(width, height, timeout, seed) ⇒ Battlefield
constructor
A new instance of Battlefield.
- #state ⇒ Object
- #tick ⇒ Object
Constructor Details
#initialize(width, height, timeout, seed) ⇒ Battlefield
Returns a new instance of Battlefield.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rrobots/battlefield.rb', line 13 def initialize width, height, timeout, seed @width, @height = width, height @seed = seed @time = 0 @robots = [] @teams = Hash.new{|h,k| h[k] = [] } @bullets = [] @explosions = [] @timeout = timeout @game_over = false srand @seed end |
Instance Attribute Details
#bullets ⇒ Object (readonly)
Returns the value of attribute bullets.
6 7 8 |
# File 'lib/rrobots/battlefield.rb', line 6 def bullets @bullets end |
#explosions ⇒ Object (readonly)
Returns the value of attribute explosions.
7 8 9 |
# File 'lib/rrobots/battlefield.rb', line 7 def explosions @explosions end |
#game_over ⇒ Object (readonly)
Returns the value of attribute game_over.
11 12 13 |
# File 'lib/rrobots/battlefield.rb', line 11 def game_over @game_over end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
3 4 5 |
# File 'lib/rrobots/battlefield.rb', line 3 def height @height end |
#robots ⇒ Object (readonly)
Returns the value of attribute robots.
4 5 6 |
# File 'lib/rrobots/battlefield.rb', line 4 def robots @robots end |
#seed ⇒ Object (readonly)
Returns the value of attribute seed.
9 10 11 |
# File 'lib/rrobots/battlefield.rb', line 9 def seed @seed end |
#teams ⇒ Object (readonly)
Returns the value of attribute teams.
5 6 7 |
# File 'lib/rrobots/battlefield.rb', line 5 def teams @teams end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
8 9 10 |
# File 'lib/rrobots/battlefield.rb', line 8 def time @time end |
#timeout ⇒ Object (readonly)
how many ticks the match can go before ending.
10 11 12 |
# File 'lib/rrobots/battlefield.rb', line 10 def timeout @timeout end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
2 3 4 |
# File 'lib/rrobots/battlefield.rb', line 2 def width @width end |
Instance Method Details
#<<(object) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rrobots/battlefield.rb', line 26 def << object case object when RobotRunner @robots << object @teams[object.team] << object when Bullet @bullets << object when Explosion @explosions << object end end |
#state ⇒ Object
63 64 65 66 67 |
# File 'lib/rrobots/battlefield.rb', line 63 def state {:explosions => explosions.map{|e| e.state}, :bullets => bullets.map{|b| b.state}, :robots => robots.map{|r| r.state}} end |
#tick ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rrobots/battlefield.rb', line 38 def tick explosions.delete_if{|explosion| explosion.dead} explosions.each{|explosion| explosion.tick} bullets.delete_if{|bullet| bullet.dead} bullets.each{|bullet| bullet.tick} robots.each do |robot| begin robot.send :internal_tick unless robot.dead rescue Exception => bang puts "#{robot} made an exception:" puts "#{bang.class}: #{bang}", bang.backtrace robot.instance_eval{@energy = -1} end end @time += 1 live_robots = robots.find_all{|robot| !robot.dead} @game_over = ( (@time >= timeout) or # timeout reached (live_robots.length == 0) or # no robots alive, draw game (live_robots.all?{|r| r.team == live_robots.first.team})) # all other teams are dead not @game_over end |