Class: Gembots::Robot

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

Overview

Class used for creating a robot.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ Robot

Creates a new instance of the robot.



16
17
18
19
20
21
22
23
# File 'lib/gembots/bot.rb', line 16

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

Instance Attribute Details

#actionsObject (readonly)

Array containing arrays that represent actions that need to be completed. The format is like: ‘[[:move, 10], [:turn, 90]]`, meaning the robot will move 10 forward, then turn 90 degrees clockwise.



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

def actions
  @actions
end

#angleObject (readonly)

Current angle of robot.



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

def angle
  @angle
end

#xObject (readonly)

X and Y positions



6
7
8
# File 'lib/gembots/bot.rb', line 6

def x
  @x
end

#yObject (readonly)

X and Y positions



6
7
8
# File 'lib/gembots/bot.rb', line 6

def y
  @y
end

Instance Method Details

#drawObject

Method called via the arena.



85
86
87
88
# File 'lib/gembots/bot.rb', line 85

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

#fireObject

Appends a fire action to the actions array. The update method will call the arena’s spawn_proj method.



92
93
94
# File 'lib/gembots/bot.rb', line 92

def fire
  @actions << [:fire]
end

#move(dist = 10) ⇒ Object

Appends a move action to the actions array. The update method will move the robot forward the distance specified. If the distance is not specified, it defaults to 10. Use a negative value to move in reverse.



44
45
46
# File 'lib/gembots/bot.rb', line 44

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

#turn(angle = 10) ⇒ Object

Appends a turn action to the actions array. The update method will turn the robot clockwise for the degree specified. If the degree is not specified, it defaults to 10. Use a negative value to rotate counter-clockwise.



36
37
38
# File 'lib/gembots/bot.rb', line 36

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

#updateObject

Method called via the arena. This attempts to preform the first action in the actions array. If it finishes the action, it will pop that actions from the actions array, allowing it to preform the next.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gembots/bot.rb', line 51

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

  when :fire
    @window.spawn_proj self
    @actions.shift
  end
end

#warp(x, y) ⇒ Object

Method called via the arena. Warps the robot to the position specified.



27
28
29
30
# File 'lib/gembots/bot.rb', line 27

def warp x, y
  @x, @y = x, y unless @warped
  @warped = true
end