Class: RTanque::Gui::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/rtanque/gui/bot.rb,
lib/rtanque/gui/bot/health_color_calculator.rb

Defined Under Namespace

Classes: HealthColorCalculator

Constant Summary collapse

HEALTH_BAR_HEIGHT =
3
HEALTH_BAR_WIDTH =
100

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, bot) ⇒ Bot

Returns a new instance of Bot


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rtanque/gui/bot.rb', line 14

def initialize(window, bot)
  @window = window
  @bot = bot
  @body_image = Gosu::Image.new(@window, Gui.resource_path("images/body.png"))
  @turret_image = Gosu::Image.new(@window, Gui.resource_path("images/turret.png"))
  @radar_image = Gosu::Image.new(@window, Gui.resource_path("images/radar.png"))
  @score_bar_image = TexPlay.create_blank_image(@window, HEALTH_BAR_WIDTH, HEALTH_BAR_HEIGHT)
  @name_font = Gosu::Font.new(@window, Window::FONT_NAME, Window::SMALL_FONT_SIZE)
  @x_factor = 1
  @y_factor = 1
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot


9
10
11
# File 'lib/rtanque/gui/bot.rb', line 9

def bot
  @bot
end

Instance Method Details

#drawObject


26
27
28
29
30
31
# File 'lib/rtanque/gui/bot.rb', line 26

def draw
  position = [@bot.position.x, @window.height - @bot.position.y]
  draw_bot(position)
  draw_name(position)
  draw_health(position)
end

#draw_bot(position) ⇒ Object


38
39
40
41
42
# File 'lib/rtanque/gui/bot.rb', line 38

def draw_bot(position)
  @body_image.draw_rot(position[0], position[1], ZOrder::BOT_BODY, Gosu.radians_to_degrees(@bot.heading.to_f), 0.5, 0.5, @x_factor, @y_factor)
  @turret_image.draw_rot(position[0], position[1], ZOrder::BOT_TURRET, Gosu.radians_to_degrees(@bot.turret.heading.to_f), 0.5, 0.5, @x_factor, @y_factor)
  @radar_image.draw_rot(position[0], position[1], ZOrder::BOT_RADAR, Gosu.radians_to_degrees(@bot.radar.heading.to_f), 0.5, 0.5, @x_factor, @y_factor)
end

#draw_health(position) ⇒ Object


49
50
51
52
53
54
55
56
57
58
# File 'lib/rtanque/gui/bot.rb', line 49

def draw_health(position)
  x,y = *position
  x_health = health.round(0)
  health_color = color_for_health
  @score_bar_image.paint {
    rect 0, 0, HEALTH_BAR_WIDTH, HEALTH_BAR_HEIGHT, :color => [0,0,0,0], :fill => true
    rect 0, 0, x_health, HEALTH_BAR_HEIGHT, :color => health_color, :fill => true
  }
  @score_bar_image.draw(x - (HEALTH_BAR_WIDTH/2) * @x_factor, y + (5 + RTanque::Bot::RADIUS) * @y_factor, ZOrder::BOT_HEALTH, @x_factor, @y_factor)
end

#draw_name(position) ⇒ Object


44
45
46
47
# File 'lib/rtanque/gui/bot.rb', line 44

def draw_name(position)
  x,y = *position
  @name_font.draw_rel(self.bot.name, x, y + (RTanque::Bot::RADIUS * @y_factor) + Window::SMALL_FONT_SIZE.to_i, ZOrder::BOT_NAME, 0.5, 0.5, @x_factor, @y_factor)
end

#grow(factor = 2, step = 0.002) ⇒ Object


33
34
35
36
# File 'lib/rtanque/gui/bot.rb', line 33

def grow(factor = 2, step = 0.002)
  @x_factor += step unless @x_factor > factor
  @y_factor += step unless @y_factor > factor
end