class RTanque::Match

Attributes

arena[R]
bots[R]
explosions[R]
max_ticks[R]
shells[R]
teams[R]
ticks[R]

Public Class Methods

new(arena, max_ticks = nil, teams = false) click to toggle source
# File lib/rtanque/match.rb, line 5
def initialize(arena, max_ticks = nil, teams = false)
  @arena = arena
  @max_ticks = max_ticks
  @teams = teams
  @ticks = 0
  @shells = TickGroup.new
  @bots = TickGroup.new
  @explosions = TickGroup.new
  @bots.pre_tick(&method(:pre_bot_tick))
  @bots.post_tick(&method(:post_bot_tick))
  @shells.pre_tick(&method(:pre_shell_tick))
  @stopped = false
end

Public Instance Methods

add_bots(*bots) click to toggle source
# File lib/rtanque/match.rb, line 32
def add_bots(*bots)
  self.bots.add(*bots)
end
finished?() click to toggle source
# File lib/rtanque/match.rb, line 27
def finished?
  @stopped || self.max_ticks_reached? || self.bots.count <= 1 ||
    (self.teams && self.bots.map(&:name).uniq.size == 1)
end
max_ticks_reached?() click to toggle source
# File lib/rtanque/match.rb, line 23
def max_ticks_reached?
  self.max_ticks && self.ticks >= self.max_ticks
end
post_bot_tick(bot) click to toggle source
# File lib/rtanque/match.rb, line 48
def post_bot_tick(bot)
  if bot.firing?
    # shell starts life at the end of the turret
    shell_position = bot.position.move(bot.turret.heading, RTanque::Bot::Turret::LENGTH)
    @shells.add(RTanque::Shell.new(bot, shell_position, bot.turret.heading.clone, bot.fire_power))
  end
end
pre_bot_tick(bot) click to toggle source
# File lib/rtanque/match.rb, line 44
def pre_bot_tick(bot)
  bot.radar.scan(self.bots.all_but(bot))
end
pre_shell_tick(shell) click to toggle source
# File lib/rtanque/match.rb, line 56
def pre_shell_tick(shell)
  shell.hits(self.bots.all_but(shell.bot)) do |origin_bot, bot_hit|
    damage = (shell.fire_power**RTanque::Shell::RATIO)
    bot_hit.reduce_health(damage)
    if bot_hit.dead?
      @explosions.add(Explosion.new(bot_hit.position))
    end
  end
end
start() click to toggle source
# File lib/rtanque/match.rb, line 36
def start
  self.tick until self.finished?
end
stop() click to toggle source
# File lib/rtanque/match.rb, line 40
def stop
  @stopped = true
end
teams=(bool) click to toggle source
# File lib/rtanque/match.rb, line 19
def teams=(bool)
  @teams = bool
end
tick() click to toggle source
# File lib/rtanque/match.rb, line 66
def tick
  self.shells.tick
  self.bots.tick
  self.explosions.tick
  @ticks += 1
end