Class: Kame::Remocon::Opal::Turtle

Inherits:
Object
  • Object
show all
Defined in:
lib/kame/remocon/opal/turtle.rb

Defined Under Namespace

Classes: Forwarder, Pos

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas, wait: 0, default_color: "white") ⇒ Turtle

Returns a new instance of Turtle.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kame/remocon/opal/turtle.rb', line 27

def initialize(canvas, wait: 0, default_color: "white")
  @canvas = canvas
  @wait = wait
  @default_color = default_color
  @context = @canvas.get_context("2d");
  clear
  reset

  @kame = Kame::Remocon::Opal::Image.new
  @kame.src = "/assets/images/kame.png"
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/kame/remocon/opal/turtle.rb', line 7

def code
  @code
end

#default_colorObject

Returns the value of attribute default_color.



6
7
8
# File 'lib/kame/remocon/opal/turtle.rb', line 6

def default_color
  @default_color
end

Instance Method Details

#clearObject



137
138
139
140
141
142
# File 'lib/kame/remocon/opal/turtle.rb', line 137

def clear
  @context.clear_rect(0, 0, @canvas.width, @canvas.height)
  @path = Kame::Remocon::Opal::Canvas::Path2D.new
  @paths = [[@path, @default_color]]
  nil
end

#color(color) ⇒ Object



173
174
175
176
177
178
# File 'lib/kame/remocon/opal/turtle.rb', line 173

def color(color)
  @path = Kame::Remocon::Opal::Canvas::Path2D.new
  @path.move_to(*@pos.canvas_coordinate)
  @paths << [@path, color]
  nil
end

#draw_kameObject



67
68
69
70
71
72
73
74
75
76
# File 'lib/kame/remocon/opal/turtle.rb', line 67

def draw_kame
  (x, y) = @pos.canvas_coordinate

  c = Math.cos(@direction / 180 * Math::PI)
  s = Math.sin(@direction / 180 * Math::PI)
  transform = [c, s, -s, c, x - 10 * c + 10 * s, y - 10 * c - 10 * s]
  @context.set_transform(*transform)
  @context.draw_image(@kame, 0, 0, 20, 20)
  @context.set_transform(1, 0, 0, 1, 0, 0)
end

#exec(code, wait = 0, &callback) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/kame/remocon/opal/turtle.rb', line 78

def exec(code, wait=0, &callback)
  @code = code
  clear
  reset
  commander = Commander.new
  commander.instance_eval code
  exec_commands(commander.commands, wait: wait, &callback)
end

#exec_command(command) ⇒ Object



132
133
134
135
# File 'lib/kame/remocon/opal/turtle.rb', line 132

def exec_command(command)
  self.method(command.first).call(*command[1..-1])
  nil
end

#exec_commands(commands, wait: nil, &callback) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kame/remocon/opal/turtle.rb', line 87

def exec_commands(commands, wait: nil, &callback)
  callback ||= Proc.new {}
  wait ||= @wait
  if wait > 0
    exec = Proc.new do
      if commands.length == 0
        false
      else
        exec_command(commands.shift)
        @context.clear_rect(0, 0, @canvas.width, @canvas.height)
        @paths.each do |path, color|
          @context.stroke_style = color
          @context.stroke(path)
        end
        draw_kame
        true
      end
    end
    interval = wait * 1000
    %x(
      var timer = setInterval(function() {
        if (!exec()) {
          callback.$call();
          clearInterval(timer);
        }
      }, interval);
    )
  else
    commands.each(&self.method(:exec_command))
    @paths.each do |path, color|
      @context.stroke_style = color
      @context.stroke(path)
    end
    if @kame.complete
      draw_kame
      callback.call
    else
      @kame.onload do
        draw_kame
        callback.call
      end
    end
  end
end

#forward(dist) ⇒ Object



180
181
182
183
184
# File 'lib/kame/remocon/opal/turtle.rb', line 180

def forward(dist)
  @pos = position_to(dist)
  move
  nil
end

#image_dataObject



63
64
65
# File 'lib/kame/remocon/opal/turtle.rb', line 63

def image_data
  @canvas.image_data
end

#moveObject



192
193
194
195
196
197
198
# File 'lib/kame/remocon/opal/turtle.rb', line 192

def move
  if @pen_down
    @path.line_to(*@pos.canvas_coordinate)
  else
    @path.move_to(*@pos.canvas_coordinate)
  end
end

#move_to(x, y) ⇒ Object



186
187
188
189
190
# File 'lib/kame/remocon/opal/turtle.rb', line 186

def move_to(x, y)
  @pos = Pos.new(x, y)
  move
  nil
end

#new_commanderObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kame/remocon/opal/turtle.rb', line 39

def new_commander
  forwarder = Forwarder.new(self, *Commander::METHODS) do
    @context.clear_rect(0, 0, @canvas.width, @canvas.height)
    @paths.each do |path, color|
      @context.stroke_style = color
      @context.stroke(path)
    end
    draw_kame
  end

  DRb::DRbObject.new(Commander.new(forwarder))
end

#pen_downObject



163
164
165
166
# File 'lib/kame/remocon/opal/turtle.rb', line 163

def pen_down
  @pen_down = true
  nil
end

#pen_upObject



168
169
170
171
# File 'lib/kame/remocon/opal/turtle.rb', line 168

def pen_up
  @pen_down = false
  nil
end

#position_to(dist) ⇒ Object



200
201
202
203
204
# File 'lib/kame/remocon/opal/turtle.rb', line 200

def position_to(dist)
  dx = Math.sin(Math::PI * @direction / 180) * dist
  dy = - Math.cos(Math::PI * @direction / 180) * dist
  Pos.new(@pos.x + dx, @pos.y + dy)
end

#resetObject



144
145
146
147
148
149
150
151
# File 'lib/kame/remocon/opal/turtle.rb', line 144

def reset
  @pen_down = false
  @direction = 0
  @pos = Pos.new(0, 0)
  @path.move_to(*@pos.canvas_coordinate)
  @context.move_to(*@pos.canvas_coordinate)
  nil
end

#turn_left(digree) ⇒ Object



153
154
155
156
# File 'lib/kame/remocon/opal/turtle.rb', line 153

def turn_left(digree)
  @direction = (@direction - digree) % 360
  nil
end

#turn_right(digree) ⇒ Object



158
159
160
161
# File 'lib/kame/remocon/opal/turtle.rb', line 158

def turn_right(digree)
  @direction = (@direction + digree) % 360
  nil
end