Class: GUI::RRobotsGameWindow
- Inherits:
-
Gosu::Window
- Object
- Gosu::Window
- GUI::RRobotsGameWindow
- Defined in:
- lib/GUI/gosu.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.
-
#mines ⇒ Object
Returns the value of attribute mines.
-
#on_game_over_handlers ⇒ Object
Returns the value of attribute on_game_over_handlers.
-
#robots ⇒ Object
Returns the value of attribute robots.
-
#toolboxes ⇒ Object
Returns the value of attribute toolboxes.
-
#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_mines ⇒ Object
- #draw_robots ⇒ Object
- #draw_toolboxes ⇒ Object
- #init_simulation ⇒ Object
- #init_window ⇒ Object
-
#initialize(battlefield, xres, yres) ⇒ RRobotsGameWindow
constructor
A new instance of RRobotsGameWindow.
- #on_game_over(&block) ⇒ Object
- #play_sounds ⇒ Object
- #robot_keys ⇒ Object
- #simulate(ticks = 1) ⇒ Object
Constructor Details
#initialize(battlefield, xres, yres) ⇒ RRobotsGameWindow
Returns a new instance of RRobotsGameWindow.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/GUI/gosu.rb', line 67 def initialize(battlefield, xres, yres) super(xres,yres, false, 16) self. = 'RRobots - GOSU POWERED' @font = Gosu::Font.new(self, BIG_FONT, xres / 20) @small_font = Gosu::Font.new(self, SMALL_FONT, xres/100) @engine = 'gosu' @path_prefix = "#{gem_path}/medias/#{@engine}" @background_image = Gosu::Image.new(self, "#{@path_prefix}/images/space.png", true) @battlefield = battlefield @xres, @yres = xres, yres @on_game_over_handlers = [] init_window init_simulation @leaderboard = LeaderBoard.new(self, @robots, xres, yres, :left_top) # for ultimate win #@theme = Gosu::Song.new(self, 'music/song.mod') #@theme.play @sound_boom = Gosu::Sample.new(self, "#{@path_prefix}/sounds/sunexp.wav") @sound_gun = Gosu::Sample.new(self, "#{@path_prefix}/sounds/shotborn.wav") end |
Instance Attribute Details
#battlefield ⇒ Object (readonly)
Returns the value of attribute battlefield.
63 64 65 |
# File 'lib/GUI/gosu.rb', line 63 def battlefield @battlefield end |
#boom ⇒ Object
Returns the value of attribute boom.
65 66 67 |
# File 'lib/GUI/gosu.rb', line 65 def boom @boom end |
#bullets ⇒ Object
Returns the value of attribute bullets.
65 66 67 |
# File 'lib/GUI/gosu.rb', line 65 def bullets @bullets end |
#explosions ⇒ Object
Returns the value of attribute explosions.
65 66 67 |
# File 'lib/GUI/gosu.rb', line 65 def explosions @explosions end |
#mines ⇒ Object
Returns the value of attribute mines.
65 66 67 |
# File 'lib/GUI/gosu.rb', line 65 def mines @mines end |
#on_game_over_handlers ⇒ Object
Returns the value of attribute on_game_over_handlers.
64 65 66 |
# File 'lib/GUI/gosu.rb', line 64 def on_game_over_handlers @on_game_over_handlers end |
#robots ⇒ Object
Returns the value of attribute robots.
65 66 67 |
# File 'lib/GUI/gosu.rb', line 65 def robots @robots end |
#toolboxes ⇒ Object
Returns the value of attribute toolboxes.
65 66 67 |
# File 'lib/GUI/gosu.rb', line 65 def toolboxes @toolboxes end |
#xres ⇒ Object (readonly)
Returns the value of attribute xres.
63 64 65 |
# File 'lib/GUI/gosu.rb', line 63 def xres @xres end |
#yres ⇒ Object (readonly)
Returns the value of attribute yres.
63 64 65 |
# File 'lib/GUI/gosu.rb', line 63 def yres @yres end |
Instance Method Details
#draw ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/GUI/gosu.rb', line 107 def draw robot_keys simulate draw_battlefield play_sounds @leaderboard.draw if Gosu::Button::KbEscape self.close end end |
#draw_battlefield ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/GUI/gosu.rb', line 142 def draw_battlefield draw_robots draw_bullets draw_explosions draw_mines draw_toolboxes end |
#draw_bullets ⇒ Object
206 207 208 209 210 211 |
# File 'lib/GUI/gosu.rb', line 206 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
213 214 215 216 217 218 |
# File 'lib/GUI/gosu.rb', line 213 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_mines ⇒ Object
227 228 229 230 231 232 |
# File 'lib/GUI/gosu.rb', line 227 def draw_mines @battlefield.mines.each do |mine| @mines[mine] ||= @mine_image @mines[mine].draw(mine.x / 2, mine.y / 2, ZOrder::Mines) end end |
#draw_robots ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/GUI/gosu.rb', line 174 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, "#{@path_prefix}/images/#{col}_body000.bmp"), Gosu::Image.new(self, "#{@path_prefix}/images/#{col}_turret000.bmp"), Gosu::Image.new(self, "#{@path_prefix}/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) if @talkative @robots[ai].info.draw("#{ai.name}\n#{'|' * (ai.energy / 5)}", ai.x / 2 - 50, ai.y / 2 + 30, ZOrder::UI, 1.0, 1.0, font_col) @robots[ai].status.draw("#{ai.name.ljust(20)} #{'%.1f' % ai.energy}", ai.x / 2 - 50, ai.y / 2 + 30, ZOrder::UI, 1.0, 1.0, font_col) else @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) end end end |
#draw_toolboxes ⇒ Object
220 221 222 223 224 225 |
# File 'lib/GUI/gosu.rb', line 220 def draw_toolboxes @battlefield.toolboxes.each do |toolbox| @toolboxes[toolbox] ||= @toolbox_image @toolboxes[toolbox].draw(toolbox.x / 2, toolbox.y / 2, ZOrder::Toolboxes) end end |
#init_simulation ⇒ Object
102 103 104 |
# File 'lib/GUI/gosu.rb', line 102 def init_simulation @robots, @bullets, @explosions, @toolboxes, @mines = {}, {}, {}, {}, {} end |
#init_window ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/GUI/gosu.rb', line 93 def init_window @boom = (0..14).map do |i| Gosu::Image.new(self, "#{@path_prefix}/images/explosion#{i.to_s.rjust(2, '0')}.bmp") end @bullet_image = Gosu::Image.new(self, "#{@path_prefix}/images/bullet.png") @mine_image = Gosu::Image.new(self, "#{@path_prefix}/images/mine.png") @toolbox_image = Gosu::Image.new(self, "#{@path_prefix}/images/toolbox.png") end |
#on_game_over(&block) ⇒ Object
89 90 91 |
# File 'lib/GUI/gosu.rb', line 89 def on_game_over(&block) @on_game_over_handlers << block end |
#play_sounds ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/GUI/gosu.rb', line 128 def play_sounds @battlefield.robots.each do |ai| if !ai.robot.events['got_hit'].empty? pan = -1.0 + (2.0 * ai.x / @battlefield.width) @sound_boom.play_pan(pan, 0.4) end if ai.actions[:fire] > 0 && ai.gun_heat <= 0 pan = -1.0 + (2.0 * ai.x / @battlefield.width) @sound_gun.play_pan(pan, [ai.actions[:fire]*3, 1].min) end end end |
#robot_keys ⇒ Object
118 119 120 121 122 123 124 125 126 |
# File 'lib/GUI/gosu.rb', line 118 def robot_keys pressed = [] BUTTONS.each do |b| pressed << b if (b) end @battlefield.robots.each do |ai| ai.(pressed) end end |
#simulate(ticks = 1) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/GUI/gosu.rb', line 150 def simulate(ticks=1) @explosions.reject!{|e,tko| e.dead } @bullets.reject!{|b,tko| b.dead } @robots.reject! { |ai,tko| ai.dead} @mines.reject! { |m,tko| m.dead} @toolboxes.reject! { |t,tko| t.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? I18n.t('gui.draw') elsif @battlefield.teams.all?{|k,t|t.size<2} I18n.t('gui.no_team_won', :winner_name => winner.name) else I18n.t('gui.team_won', :winner_team => winner.team) 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 |