Class: RRobotsGameWindow
- Inherits:
-
Gosu::Window
- Object
- Gosu::Window
- RRobotsGameWindow
- Defined in:
- lib/rrobots/gui/gosuarena.rb
Instance Attribute Summary collapse
-
#battlefield ⇒ Object
readonly
Returns the value of attribute battlefield.
-
#boom ⇒ Object
Returns the value of attribute boom.
-
#bullets ⇒ Object
Returns the value of attribute bullets.
-
#explosions ⇒ Object
Returns the value of attribute explosions.
-
#on_game_over_handlers ⇒ Object
Returns the value of attribute on_game_over_handlers.
-
#robots ⇒ Object
Returns the value of attribute robots.
-
#xres ⇒ Object
readonly
Returns the value of attribute xres.
-
#yres ⇒ Object
readonly
Returns the value of attribute yres.
Instance Method Summary collapse
- #draw ⇒ Object
- #draw_battlefield ⇒ Object
- #draw_bullets ⇒ Object
- #draw_explosions ⇒ Object
- #draw_robots ⇒ Object
- #init_simulation ⇒ Object
- #init_window ⇒ Object
-
#initialize(battlefield, xres, yres) ⇒ RRobotsGameWindow
constructor
A new instance of RRobotsGameWindow.
- #on_game_over(&block) ⇒ Object
- #simulate(ticks = 1) ⇒ Object
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. = '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
#battlefield ⇒ Object (readonly)
Returns the value of attribute battlefield.
15 16 17 |
# File 'lib/rrobots/gui/gosuarena.rb', line 15 def battlefield @battlefield end |
#boom ⇒ Object
Returns the value of attribute boom.
16 17 18 |
# File 'lib/rrobots/gui/gosuarena.rb', line 16 def boom @boom end |
#bullets ⇒ Object
Returns the value of attribute bullets.
16 17 18 |
# File 'lib/rrobots/gui/gosuarena.rb', line 16 def bullets @bullets end |
#explosions ⇒ Object
Returns the value of attribute explosions.
16 17 18 |
# File 'lib/rrobots/gui/gosuarena.rb', line 16 def explosions @explosions end |
#on_game_over_handlers ⇒ Object
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 |
#robots ⇒ Object
Returns the value of attribute robots.
16 17 18 |
# File 'lib/rrobots/gui/gosuarena.rb', line 16 def robots @robots end |
#xres ⇒ Object (readonly)
Returns the value of attribute xres.
15 16 17 |
# File 'lib/rrobots/gui/gosuarena.rb', line 15 def xres @xres end |
#yres ⇒ Object (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
#draw ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/rrobots/gui/gosuarena.rb', line 47 def draw simulate draw_battlefield @leaderboard.draw if Gosu::Button::KbEscape self.close end end |
#draw_battlefield ⇒ Object
56 57 58 59 60 |
# File 'lib/rrobots/gui/gosuarena.rb', line 56 def draw_battlefield draw_robots draw_bullets draw_explosions end |
#draw_bullets ⇒ Object
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_explosions ⇒ Object
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_robots ⇒ Object
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_simulation ⇒ Object
43 44 45 |
# File 'lib/rrobots/gui/gosuarena.rb', line 43 def init_simulation @robots, @bullets, @explosions = {}, {}, {} end |
#init_window ⇒ Object
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 |