Class: Gembots::Robot

Inherits:
Object
  • Object
show all
Defined in:
lib/gembots/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ Robot

Returns a new instance of Robot.



6
7
8
9
10
11
12
# File 'lib/gembots/bot.rb', line 6

def initialize window
  @actions = []
  @window = window
  @images = Gosu::Image::load_tiles(window, "media/tank.png", 32, 32, false)
  @image = Gosu::Image.new window, "media/cannon.png", false
  @x = @y = @angle = @cur_image = 0.0
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



4
5
6
# File 'lib/gembots/bot.rb', line 4

def actions
  @actions
end

#angleObject (readonly)

Returns the value of attribute angle.



4
5
6
# File 'lib/gembots/bot.rb', line 4

def angle
  @angle
end

#xObject (readonly)

Returns the value of attribute x.



4
5
6
# File 'lib/gembots/bot.rb', line 4

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



4
5
6
# File 'lib/gembots/bot.rb', line 4

def y
  @y
end

Instance Method Details

#drawObject



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

def draw
  @images[@cur_image].draw_rot @x, @y, 1, @angle - 90 % 360
  @image.draw_rot @x, @y, 1, @angle - 90 % 360
end

#move(dist = 10) ⇒ Object



22
23
24
# File 'lib/gembots/bot.rb', line 22

def move dist=10
  @actions << [:move, dist]
end

#turn(angle = 10) ⇒ Object



18
19
20
# File 'lib/gembots/bot.rb', line 18

def turn angle=10
  @actions << [:turn, angle]
end

#updateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gembots/bot.rb', line 26

def update
  return if @actions.empty?
  case @actions[0][0]
  when :move then
    # I probably should implement dist = @actions[0][1]; dist %= 0.5 or something
    # but right now I'm tired, and worried I will screw up the math if I try that...
    dist = @actions[0][1] <= 0.9 ? @actions[0][1] : 1.0
    @x += Gosu::offset_x @angle, dist
    @y += Gosu::offset_y @angle, dist
    @x %= 640
    @y %= 480

    @cur_image += 0.1
    @cur_image %= 7.0

    @actions[0][1] -= 0.5
    @actions.shift if @actions[0][1] == 0.0

  when :turn then
    deg = @actions[0][1] <= 9 ? @actions[0][1] : 10

    @angle += deg
    @angle %= 360
    @actions[0][1] -= deg

    @actions.shift if @actions[0][1] == 0
  end
end

#warp(x, y) ⇒ Object



14
15
16
# File 'lib/gembots/bot.rb', line 14

def warp x, y
  @x, @y = x, y
end