class Salamander::Actor

Public Class Methods

draw(canvas, filename=nil, &blk) click to toggle source

Executes code within the context of an Actor.

If given a filename, reads the contents and evaluates it.

# File lib/salamander/actor.rb, line 6
def self.draw (canvas, filename=nil, &blk)
  actor = new(canvas)
  if filename
    actor.instance_eval(File.open(filename).read, filename)
  else
    actor.instance_eval(&blk)
  end
end
new(canvas) click to toggle source
# File lib/salamander/actor.rb, line 15
def initialize (canvas)
  @canvas = canvas
  
  move_to(canvas.width/2, canvas.height/2)
  face :north
  color "#FFFFFF"
end

Public Instance Methods

angle() click to toggle source

The current angle, in degrees.

# File lib/salamander/actor.rb, line 49
def angle
  @angle
end
canvas() click to toggle source

The drawing canvas being used

# File lib/salamander/actor.rb, line 90
def canvas
  @canvas
end
color(color=nil) click to toggle source

Gets or sets the current drawing color in numeric RGBA format.

If ‘color’ is nil, gets the current color. Otherwise, ‘color’ must be in “#RRGGBB” notation.

# File lib/salamander/actor.rb, line 73
def color (color=nil)
  if not color then
    @color
  else
    color = COLORS[color] if color.is_a? Symbol
    color = color.to_str
    if color[0] == ?# and color.length == 7
      color = color[1..-1].to_i(16)
      @color = color * (2**8) + 255
    else
      raise "color must be a valid string in the format #RRGGBB"
    end
  end
end
face(theta) click to toggle source

Face a specific direction.

‘theta’ may be :north, :northeast, :east, etc. or a number of degrees. 0 degrees points east, and increasing amounts go clockwise.

# File lib/salamander/actor.rb, line 64
def face (theta)
  theta = COMPASS[theta] if theta.is_a? Symbol
  @angle = theta % 360
end
move(distance, direction=nil) click to toggle source

Move a given distance

# File lib/salamander/actor.rb, line 30
def move (distance, direction=nil)
  if direction
    theta = (direction.is_a?(Symbol) ? COMPASS[direction] : direction)
  else
    theta = angle
  end
  theta = theta / 180.0 * Math::PI
  x = distance * Math.cos(theta) + @x
  y = distance * Math.sin(theta) + @y
  move_to(x, y)
end
move_to(x, y) click to toggle source

Move to a specific point

# File lib/salamander/actor.rb, line 43
def move_to (x, y)
  @x, @y = x, y
end
position() click to toggle source

Get the current position as an array.

# File lib/salamander/actor.rb, line 25
def position
  [@x, @y]
end
turn(theta) click to toggle source

Turn towards a new direction.

‘theta’ may be :right, :left, :around, or a number of degrees. Positive amounts of degrees turn right, while negative amounts go to the left.

# File lib/salamander/actor.rb, line 56
def turn (theta)
  theta = DIRECTIONS[theta] if theta.is_a? Symbol
  face(theta + angle)
end