Class: RRobotsGameWindow

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/rrobots/gui/gosuarena.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(battlefield, xres, yres) ⇒ RRobotsGameWindow

Returns a new instance of RRobotsGameWindow.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rrobots/gui/gosuarena.rb', line 18

def initialize(battlefield, xres, yres)
  super(xres, yres, false, 16)
  self.caption = 'RRobots'
  @font = Gosu::Font.new(self, BIG_FONT, 24)
  @small_font = Gosu::Font.new(self, SMALL_FONT, 24) #xres/100
  @background_image = Gosu::Image.new(self, File.join(File.dirname(__FILE__),"../images/space.png"), true)
  @battlefield = battlefield
  @xres, @yres = xres, yres
  @on_game_over_handlers = []
  init_window
  init_simulation
  @leaderboard = Leaderboard.new(self, @robots)
end

Instance Attribute Details

#battlefieldObject (readonly)

Returns the value of attribute battlefield.



15
16
17
# File 'lib/rrobots/gui/gosuarena.rb', line 15

def battlefield
  @battlefield
end

#boomObject

Returns the value of attribute boom.



16
17
18
# File 'lib/rrobots/gui/gosuarena.rb', line 16

def boom
  @boom
end

#bulletsObject

Returns the value of attribute bullets.



16
17
18
# File 'lib/rrobots/gui/gosuarena.rb', line 16

def bullets
  @bullets
end

#explosionsObject

Returns the value of attribute explosions.



16
17
18
# File 'lib/rrobots/gui/gosuarena.rb', line 16

def explosions
  @explosions
end

#on_game_over_handlersObject

Returns the value of attribute on_game_over_handlers.



16
17
18
# File 'lib/rrobots/gui/gosuarena.rb', line 16

def on_game_over_handlers
  @on_game_over_handlers
end

#robotsObject

Returns the value of attribute robots.



16
17
18
# File 'lib/rrobots/gui/gosuarena.rb', line 16

def robots
  @robots
end

#xresObject (readonly)

Returns the value of attribute xres.



15
16
17
# File 'lib/rrobots/gui/gosuarena.rb', line 15

def xres
  @xres
end

#yresObject (readonly)

Returns the value of attribute yres.



15
16
17
# File 'lib/rrobots/gui/gosuarena.rb', line 15

def yres
  @yres
end

Instance Method Details

#drawObject



47
48
49
50
51
52
53
54
# File 'lib/rrobots/gui/gosuarena.rb', line 47

def draw
  simulate
  draw_battlefield
  @leaderboard.draw
  if button_down? Gosu::Button::KbEscape
    self.close
  end
end

#draw_battlefieldObject



56
57
58
59
60
# File 'lib/rrobots/gui/gosuarena.rb', line 56

def draw_battlefield
  draw_robots
  draw_bullets
  draw_explosions
end

#draw_bulletsObject



109
110
111
112
113
114
# File 'lib/rrobots/gui/gosuarena.rb', line 109

def draw_bullets
  @battlefield.bullets.each do |bullet|
    @bullets[bullet] ||= @bullet_image
    @bullets[bullet].draw(bullet.x / 2, bullet.y / 2, ZOrder::Explosions)
  end
end

#draw_explosionsObject



116
117
118
119
120
121
# File 'lib/rrobots/gui/gosuarena.rb', line 116

def draw_explosions
  @battlefield.explosions.each do |explosion|
    @explosions[explosion] = boom[explosion.t % 14]
    @explosions[explosion].draw_rot(explosion.x / 2, explosion.y / 2, ZOrder::Explosions, 0)
  end
end

#draw_robotsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rrobots/gui/gosuarena.rb', line 84

def draw_robots
  @battlefield.robots.each_with_index do |ai, i|
    next if ai.dead
    col = COLORS[i % COLORS.size]
    font_col = FONT_COLORS[i % FONT_COLORS.size]
    @robots[ai] ||= GosuRobot.new(
      Gosu::Image.new(self, File.join(File.dirname(__FILE__),"../images/#{col}_body000.bmp")),
      Gosu::Image.new(self, File.join(File.dirname(__FILE__),"../images/#{col}_turret000.bmp")),
      Gosu::Image.new(self, File.join(File.dirname(__FILE__),"../images/#{col}_radar000.bmp")),
      @small_font,
      @small_font,
      @small_font,
      col,
      font_col
    )
    @robots[ai].body.draw_rot(ai.x / 2, ai.y / 2, ZOrder::Robot, (-(ai.heading-90)) % 360)
    @robots[ai].gun.draw_rot(ai.x / 2, ai.y / 2, ZOrder::Robot, (-(ai.gun_heading-90)) % 360)
    @robots[ai].radar.draw_rot(ai.x / 2, ai.y / 2, ZOrder::Robot, (-(ai.radar_heading-90)) % 360)
    
    @robots[ai].speech.draw_rel(ai.speech.to_s, ai.x / 2, ai.y / 2 - 40, ZOrder::UI, 0.5, 0.5, 1, 1, font_col)
    @robots[ai].info.draw_rel("#{ai.name}", ai.x / 2, ai.y / 2 + 30, ZOrder::UI, 0.5, 0.5, 1, 1, font_col)
    @robots[ai].info.draw_rel("#{ai.energy.to_i}", ai.x / 2, ai.y / 2 + 50, ZOrder::UI, 0.5, 0.5, 1, 1, font_col)
  end
end

#init_simulationObject



43
44
45
# File 'lib/rrobots/gui/gosuarena.rb', line 43

def init_simulation
  @robots, @bullets, @explosions = {}, {}, {}
end

#init_windowObject



36
37
38
39
40
41
# File 'lib/rrobots/gui/gosuarena.rb', line 36

def init_window
  @boom = (0..14).map do |i|
    Gosu::Image.new(self, File.join(File.dirname(__FILE__),"../images/explosion#{i.to_s.rjust(2, '0')}.bmp"))
  end
  @bullet_image = Gosu::Image.new(self, File.join(File.dirname(__FILE__),"../images/bullet.png"))
end

#on_game_over(&block) ⇒ Object



32
33
34
# File 'lib/rrobots/gui/gosuarena.rb', line 32

def on_game_over(&block)
  @on_game_over_handlers << block
end

#simulate(ticks = 1) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rrobots/gui/gosuarena.rb', line 62

def simulate(ticks=1)
  @explosions.reject!{|e,tko| e.dead }
  @bullets.reject!{|b,tko| b.dead }
  @robots.reject! { |ai,tko| ai.dead}
  ticks.times do
    if @battlefield.game_over
      @on_game_over_handlers.each{|h| h.call(@battlefield) }
        winner = @robots.keys.first
        whohaswon = if winner.nil?
          "Draw!"
        elsif @battlefield.teams.all?{|k,t|t.size<2}
          "#{winner.name} won!"
        else
          "Team #{winner.team} won!"
        end
        text_color = winner ? winner.team : 7
        @font.draw_rel("#{whohaswon}", xres/2, yres/2, ZOrder::UI, 0.5, 0.5, 1, 1, 0xffffff00)
    end
    @battlefield.tick
  end
end