class FeedBo::Robot
Attributes
level[RW]
tools[R]
Public Class Methods
new()
click to toggle source
Calls superclass method
FeedBo::Transformable::new
# File lib/feed_bo/robot.rb, line 10 def initialize super @tools = [] end
Public Instance Methods
add_tool(tool)
click to toggle source
# File lib/feed_bo/robot.rb, line 40 def add_tool(tool) tool.robot = self @tools << tool end
continue(last_activity)
click to toggle source
# File lib/feed_bo/robot.rb, line 30 def continue(last_activity) until @level.finish? if last_activity == :move move else @level.perform_step end end end
draw()
click to toggle source
# File lib/feed_bo/robot.rb, line 15 def draw s = Level::FIELD_SIZE @svg.image width: s, height: s, :'xlink:href' => h.image_path("robot.svg") @tools.each do |tool| tool.draw(@svg) end end
hit_positions()
click to toggle source
# File lib/feed_bo/robot.rb, line 45 def hit_positions positions = [Level.vector_sum(@position, movement)] if @direction.odd? positions << Level.vector_sum(@position, [movement[0],0]) positions << Level.vector_sum(@position, [0,movement[1]]) end return positions end
run()
click to toggle source
# File lib/feed_bo/robot.rb, line 27 def run end
setup()
click to toggle source
# File lib/feed_bo/robot.rb, line 24 def setup end
Protected Instance Methods
animate_move()
click to toggle source
# File lib/feed_bo/robot.rb, line 84 def animate_move @svg.animateMotion values: "0,0; #{movement[0]*Level::FIELD_SIZE},#{movement[1]*Level::FIELD_SIZE}", begin: "#{@level.step*Level::STEP_DUR}s", dur: "#{Level::STEP_DUR}s", additive: 'sum', fill: 'freeze' end
animate_turn(step)
click to toggle source
# File lib/feed_bo/robot.rb, line 108 def animate_turn(step) s = Level::FIELD_SIZE @svg.animateTransform attributeName: 'transform', type: 'rotate', values: "0,#{s/2},#{s/2}; #{step*45},#{s/2},#{s/2}", begin: "#{@level.step*Level::STEP_DUR}s", dur: "#{Level::STEP_DUR}s", additive: 'sum', fill: 'freeze' end
move(steps=1)
click to toggle source
# File lib/feed_bo/robot.rb, line 56 def move(steps=1) steps.times do if hit_positions.none? { |p| @level.position_blocked? p } @level.elements.select { |e| hit_positions.include? e.position }.each do |e| e.hit if e.respond_to? :hit end @position = Level.vector_sum(@position, movement) animate_move end @level.perform_step end return :move end
movement()
click to toggle source
# File lib/feed_bo/robot.rb, line 70 def movement x = case @direction when 0,4 then 0 when 1,2,3 then 1 when 5,6,7 then -1 end y = case @direction when 2,6 then 0 when 3,4,5 then 1 when 7,0,1 then -1 end return [x, y] end
turn(steps)
click to toggle source
# File lib/feed_bo/robot.rb, line 89 def turn(steps) step = steps > 0 ? 1 : -1 steps.abs.times do return false if @level.finish? @direction += step @direction = @direction % 8 animate_turn(step) @level.perform_step end end
turn_left(steps=1)
click to toggle source
# File lib/feed_bo/robot.rb, line 104 def turn_left(steps=1) turn -1 * steps end
turn_right(steps=1)
click to toggle source
# File lib/feed_bo/robot.rb, line 100 def turn_right(steps=1) turn steps end
wait(steps=1)
click to toggle source
# File lib/feed_bo/robot.rb, line 114 def wait(steps=1) steps.times do @level.perform_step end end