class FeedBo::Level
Constants
- FIELD_SIZE
- HEIGHT
- STEP_DUR
- TOTAL_STEPS
- WIDTH
Attributes
elements[RW]
hide_grid[RW]
points[RW]
robot[RW]
step[RW]
target_points[RW]
Public Class Methods
advanced_distance(pos1, pos2)
click to toggle source
# File lib/feed_bo/level.rb, line 92 def self.advanced_distance(pos1, pos2) # todo end
distance(pos1, pos2)
click to toggle source
# File lib/feed_bo/level.rb, line 88 def self.distance(pos1, pos2) return (pos1[0] - pos2[0]).abs + (pos1[1] - pos2[1]).abs end
new()
click to toggle source
# File lib/feed_bo/level.rb, line 12 def initialize @elements = [] @step = 0 @frame = 0 @points = 0 @target_points = 1 @hide_grid = false @svg = ::Builder::XmlMarkup.new end
vector_sum(vec1, vec2)
click to toggle source
# File lib/feed_bo/level.rb, line 96 def self.vector_sum(vec1, vec2) [vec1, vec2].transpose.map { |e| e.reduce(:+) } end
Public Instance Methods
add(element)
click to toggle source
# File lib/feed_bo/level.rb, line 41 def add(element) element.level = self @elements << element end
build()
click to toggle source
# File lib/feed_bo/level.rb, line 22 def build end
draw()
click to toggle source
# File lib/feed_bo/level.rb, line 51 def draw # @svg.instruct! :xml, version: '1.0', encoding: 'UTF-8', standalone: 'no' # @svg.declare! :DOCTYPE, :svg, :PUBLIC, "-//W3C//DTD SVG 1.1//EN", "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" @svg.svg width: WIDTH*FIELD_SIZE, height: HEIGHT*FIELD_SIZE, version: "1.1", xmlns: "http://www.w3.org/2000/svg", :'xmlns:xlink' => "http://www.w3.org/1999/xlink" do draw_grid unless @hide_grid @svg.rect x: 0, y: 0, width: WIDTH*FIELD_SIZE, height: HEIGHT*FIELD_SIZE, style: "stroke: black; fill: none" (@elements + [@robot]).each do |e| @svg.g transform: "translate(#{e.start_position[0]*FIELD_SIZE},#{e.start_position[1]*FIELD_SIZE})" do @svg << e.svg.target! end end if @points == @target_points @svg.g transform: "translate(#{WIDTH*FIELD_SIZE/2-200},#{HEIGHT*FIELD_SIZE/2-100})", visibility: "hidden" do @svg.rect width: 400, height: 200, stroke: 'blue', fill: 'white' @svg.text 'Level completed!', x: 25, y: 120, :'font-family' => "Verdana", :'font-size' => 40, fill: "blue" @svg.set attributeName: "visibility", to: "visible", begin: @step*STEP_DUR end end end return @svg.target! end
finish?()
click to toggle source
# File lib/feed_bo/level.rb, line 84 def finish? return (@points == @target_points or @step == TOTAL_STEPS) end
perform_step()
click to toggle source
# File lib/feed_bo/level.rb, line 36 def perform_step @elements.each { |e| e.perform_step } @step += 1 end
position_blocked?(pos)
click to toggle source
# File lib/feed_bo/level.rb, line 77 def position_blocked?(pos) return true if pos[0] < 0 or pos[1] < 0 or pos[0] > WIDTH-1 or pos[1] > HEIGHT-1 return @elements.any? do |e| e.block_position and Level.distance(e.position, pos) == 0 end end
robot=(robot)
click to toggle source
# File lib/feed_bo/level.rb, line 46 def robot=(robot) @robot = robot @robot.level = self end
run()
click to toggle source
# File lib/feed_bo/level.rb, line 25 def run @robot.setup (@elements + [@robot]).each do |e| e.start_position = e.position.dup e.draw end last_activity = @robot.run @robot.continue last_activity return draw end
Protected Instance Methods
draw_grid()
click to toggle source
# File lib/feed_bo/level.rb, line 102 def draw_grid (WIDTH-1).times do |i| stroke_width = (i+1) % 5 == 0 ? 0.3 : 0.2 @svg.line x1: FIELD_SIZE*(i+1), y1: 0, x2: FIELD_SIZE*(i+1), y2: HEIGHT * FIELD_SIZE, style: "stroke: grey; stroke-width: #{stroke_width}" end (HEIGHT-1).times do |i| stroke_width = (i+1) % 5 == 0 ? 0.3 : 0.2 @svg.line x1: 0, y1: FIELD_SIZE*(i+1), x2: WIDTH * FIELD_SIZE, y2: FIELD_SIZE*(i+1), style: "stroke: grey; stroke-width: #{stroke_width}" end end