Class: Salamander::Actor

Inherits:
Object
  • Object
show all
Defined in:
lib/salamander/actor.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas) ⇒ Actor

Returns a new instance of Actor.



15
16
17
18
19
20
21
# 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

Class Method Details

.draw(canvas, filename = nil, &blk) ⇒ Object

Executes code within the context of an Actor.

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



6
7
8
9
10
11
12
13
# 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

Instance Method Details

#angleObject

The current angle, in degrees.



49
50
51
# File 'lib/salamander/actor.rb', line 49

def angle
  @angle
end

#canvasObject

The drawing canvas being used



90
91
92
# File 'lib/salamander/actor.rb', line 90

def canvas
  @canvas
end

#color(color = nil) ⇒ Object

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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) ⇒ Object

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.



64
65
66
67
# 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) ⇒ Object

Move a given distance



30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object

Move to a specific point



43
44
45
# File 'lib/salamander/actor.rb', line 43

def move_to (x, y)
  @x, @y = x, y
end

#positionObject

Get the current position as an array.



25
26
27
# File 'lib/salamander/actor.rb', line 25

def position
  [@x, @y]
end

#turn(theta) ⇒ Object

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.



56
57
58
59
# File 'lib/salamander/actor.rb', line 56

def turn (theta)
  theta = DIRECTIONS[theta] if theta.is_a? Symbol
  face(theta + angle)
end