Method: AuthorEngine::TouchJoystick#update

Defined in:
lib/author_engine/game/opal/touch_joystick.rb

#update(touches) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 50

def update(touches)
  touch_detected = false

  touches.detect do |id, touch|
    if circles_collide?(@x, @y, @radius, touch.origin_x, touch.origin_y, 1)
      touch_detected = true

      _distance = @game.distance(@x,@y, touch.x,touch.y).clamp(0, @radius)
      _direction = Math.atan2(touch.y - @y, touch.x - @x)

      @joystick_x = @x +(_distance * Math.cos(_direction))
      @joystick_y = @y +(_distance * Math.sin(_direction))

      return true
    end
  end


  unless touch_detected
    @joystick_x = @x
    @joystick_y = @y
  end

  trigger_input

  return nil
end