Class: Gembots::Arena

Inherits:
Gosu::Window
  • Object
show all
Defined in:
lib/gembots/arena.rb

Overview

Class used for initializing the arena and opening the window.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*bots) ⇒ Arena

Returns a new instance of Arena.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gembots/arena.rb', line 9

def initialize *bots
  super 640, 480, false
  self.caption = "Gembots battle"
  @bots = []
  @projs = []

  bots.each do |bot_class|
    bot = bot_class.new self
    bot.warp 320, 240
    @bots << bot
  end
end

Instance Attribute Details

#botsObject (readonly)

Array containing an instance of each bot in the arena



7
8
9
# File 'lib/gembots/arena.rb', line 7

def bots
  @bots
end

Instance Method Details

#button_down(id) ⇒ Object

Method called via Gosu. If the escape key is pressed, then the window closes.



50
51
52
# File 'lib/gembots/arena.rb', line 50

def button_down id
  close if id == Gosu::KbEscape
end

#drawObject

Method called via Gosu. It simply calls the draw method for each projectile and bot.



24
25
26
27
# File 'lib/gembots/arena.rb', line 24

def draw
  @bots.each &:draw
  @projs.each &:draw
end

#spawn_proj(bot) ⇒ Object

Spawns a Projectile instance at bot’s position and angle.



55
56
57
# File 'lib/gembots/arena.rb', line 55

def spawn_proj bot
  @projs << Gembots::Projectile.new(self, bot.x, bot.y, bot.angle)
end

#updateObject

Method called via Gosu. It calls the update method for each projectile and bot. It also checks to see if a projectile has hit a bot, and deletes the bot and projectile if it has.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gembots/arena.rb', line 32

def update
  @bots.each &:update
  @projs.each &:update

  @bots.each do |bot|
    bot.on_idle if bot.actions.empty?

    @projs.each do |proj|
      if (Gosu::distance bot.x, bot.y, proj.x, proj.y) < 10
        @bots.delete bot
        @projs.delete proj
      end
    end
  end
end