Class: RTanque::Match

Inherits:
Object
  • Object
show all
Defined in:
lib/rtanque/match.rb,
lib/rtanque/match/tick_group.rb

Defined Under Namespace

Classes: TickGroup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arena, max_ticks = nil, teams = false) ⇒ Match



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#arenaObject (readonly)

Returns the value of attribute arena



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

def arena
  @arena
end

#botsObject (readonly)

Returns the value of attribute bots



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

def bots
  @bots
end

#explosionsObject (readonly)

Returns the value of attribute explosions



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

def explosions
  @explosions
end

#max_ticksObject (readonly)

Returns the value of attribute max_ticks



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

def max_ticks
  @max_ticks
end

#shellsObject (readonly)

Returns the value of attribute shells



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

def shells
  @shells
end

#teamsObject

Returns the value of attribute teams



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

def teams
  @teams
end

#ticksObject (readonly)

Returns the value of attribute ticks



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

def ticks
  @ticks
end

Instance Method Details

#add_bots(*bots) ⇒ Object



32
33
34
# File 'lib/rtanque/match.rb', line 32

def add_bots(*bots)
  self.bots.add(*bots)
end

#finished?Boolean



27
28
29
30
# 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?Boolean



23
24
25
# File 'lib/rtanque/match.rb', line 23

def max_ticks_reached?
  self.max_ticks && self.ticks >= self.max_ticks
end

#post_bot_tick(bot) ⇒ Object



48
49
50
51
52
53
54
# 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) ⇒ Object



44
45
46
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
# 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

#startObject



36
37
38
# File 'lib/rtanque/match.rb', line 36

def start
  self.tick until self.finished?
end

#stopObject



40
41
42
# File 'lib/rtanque/match.rb', line 40

def stop
  @stopped = true
end

#tickObject



66
67
68
69
70
71
# File 'lib/rtanque/match.rb', line 66

def tick
  self.shells.tick
  self.bots.tick
  self.explosions.tick
  @ticks += 1
end