Class: Brawl::BattleController

Inherits:
Object
  • Object
show all
Includes:
Eventable
Defined in:
lib/brawl/battle_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ BattleController

Returns a new instance of BattleController.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/brawl/battle_controller.rb', line 12

def initialize(params = {})
  super

  arena_data = params[:arena]
  make_arena(arena_data)

  bot_data = params[:bots]
  make_bots(bot_data)

  clock_data = params[:clock]
  make_clock(clock_data)

end

Instance Attribute Details

#arenaObject

Returns the value of attribute arena.



10
11
12
# File 'lib/brawl/battle_controller.rb', line 10

def arena
  @arena
end

#botsObject

Returns the value of attribute bots.



10
11
12
# File 'lib/brawl/battle_controller.rb', line 10

def bots
  @bots
end

#clockObject

Returns the value of attribute clock.



10
11
12
# File 'lib/brawl/battle_controller.rb', line 10

def clock
  @clock
end

Instance Method Details

#bot_msg_callback(*params, &block) ⇒ Object



76
77
78
# File 'lib/brawl/battle_controller.rb', line 76

def bot_msg_callback(*params, &block)
  fire_event(:bot_msg, *params, &block)
end

#make_arena(arena_data) ⇒ Object



26
27
28
29
# File 'lib/brawl/battle_controller.rb', line 26

def make_arena(arena_data)
  return unless arena_data
  @arena = Brawl::Arena.new(arena_data)
end

#make_bots(bot_data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/brawl/battle_controller.rb', line 37

def make_bots(bot_data)
  return unless bot_data
  
  @bots = []
  bot_data.each do |bot_datum|
    bot_params = bot_datum[:params]

    bot_params.merge!(arena: arena)
    bot_params.merge!(name: bot_datum[:name])
    
    location = {location: {x: rand(arena.width), y:rand(arena.length)}}
    bot_params.merge!(location)

    bot_instance  = bot_datum[:class].new(bot_params)
    
    bot_instance.register_for_event(
      event:    :bot_ran_method, 
      listener: self, 
      callback: :bot_msg_callback
    )
    bot_instance.register_for_event(
      event:    :bot_damaged, 
      listener: self, 
      callback: :bot_msg_callback
    )

    bot_proxy = BotProxy.new(
      clock:  clock,
      bot:    bot_instance,
      code:   bot_datum[:code]
    )

    @bots << {
      name:  bot_datum[:name],
      proxy: bot_proxy
    }
  end
end

#make_clock(clock_data) ⇒ Object



31
32
33
34
35
# File 'lib/brawl/battle_controller.rb', line 31

def make_clock(clock_data)
  return unless clock_data
  @clock = Clock.new(clock_data[:tick_rate])
  @arena.set_clock(clock) if @arena
end

#startObject



84
85
86
87
88
89
# File 'lib/brawl/battle_controller.rb', line 84

def start
  @bots.each do |bot|
    bot[:proxy].start
  end
  @clock.start
end

#stopObject



91
92
93
# File 'lib/brawl/battle_controller.rb', line 91

def stop
  @clock.stop
end

#victory?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/brawl/battle_controller.rb', line 80

def victory?
  arena.victory?
end