Class: GUI::RRobotsGameWindow

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/GUI/gosu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.caption = '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

#battlefieldObject (readonly)

Returns the value of attribute battlefield.



63
64
65
# File 'lib/GUI/gosu.rb', line 63

def battlefield
  @battlefield
end

#boomObject

Returns the value of attribute boom.



65
66
67
# File 'lib/GUI/gosu.rb', line 65

def boom
  @boom
end

#bulletsObject

Returns the value of attribute bullets.



65
66
67
# File 'lib/GUI/gosu.rb', line 65

def bullets
  @bullets
end

#explosionsObject

Returns the value of attribute explosions.



65
66
67
# File 'lib/GUI/gosu.rb', line 65

def explosions
  @explosions
end

#minesObject

Returns the value of attribute mines.



65
66
67
# File 'lib/GUI/gosu.rb', line 65

def mines
  @mines
end

#on_game_over_handlersObject

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

#robotsObject

Returns the value of attribute robots.



65
66
67
# File 'lib/GUI/gosu.rb', line 65

def robots
  @robots
end

#toolboxesObject

Returns the value of attribute toolboxes.



65
66
67
# File 'lib/GUI/gosu.rb', line 65

def toolboxes
  @toolboxes
end

#xresObject (readonly)

Returns the value of attribute xres.



63
64
65
# File 'lib/GUI/gosu.rb', line 63

def xres
  @xres
end

#yresObject (readonly)

Returns the value of attribute yres.



63
64
65
# File 'lib/GUI/gosu.rb', line 63

def yres
  @yres
end

Instance Method Details

#drawObject



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 button_down? Gosu::Button::KbEscape
    self.close
  end
end

#draw_battlefieldObject



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_bulletsObject



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_explosionsObject



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_minesObject



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_robotsObject



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_toolboxesObject



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_simulationObject



102
103
104
# File 'lib/GUI/gosu.rb', line 102

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

#init_windowObject



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_soundsObject



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_keysObject



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 button_down?(b)
  end
  @battlefield.robots.each do |ai|
    ai.pressed_button(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