Class: AuthorEngine::TouchJoystick
- Inherits:
-
Object
- Object
- AuthorEngine::TouchJoystick
- Defined in:
- lib/author_engine/game/opal/touch_joystick.rb
Instance Method Summary collapse
- #circles_collide?(x, y, radius, x2, y2, radius2) ⇒ Boolean
- #draw ⇒ Object
-
#initialize(x:, y: nil, radius:, side:, background: nil, color: nil) ⇒ TouchJoystick
constructor
A new instance of TouchJoystick.
- #set(key, boolean) ⇒ Object
- #trigger_input(threshold = 0.35) ⇒ Object
- #update(touches) ⇒ Object
Constructor Details
#initialize(x:, y: nil, radius:, side:, background: nil, color: nil) ⇒ TouchJoystick
Returns a new instance of TouchJoystick.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 3 def initialize(x:, y: nil, radius:, side:, background: nil, color: nil) @x, @y, @radius, @side, @background, @color = x, y, radius, side, background, color @buttons = AuthorEngine::Part::OpalInput::BUTTONS @key_states = AuthorEngine::Part::OpalInput::KEY_STATES @game = AuthorEngine::GameRunner.instance.game @game_width = 128 * @game. @game_x = `window.innerWidth/2 - #{@game_width/2}` if @side == :left @x = @game_x-@x elsif @side == :right @x = @game_x+@game_width+@x else raise "side must be :left or :right" end @y = `window.innerHeight/2` unless @y.is_a?(Numeric) @color = @game.dark_gray unless @color @background = @game.light_gray unless @background @joystick_x, @joystick_y, @joystick_radius = @x, @y, @radius/2 end |
Instance Method Details
#circles_collide?(x, y, radius, x2, y2, radius2) ⇒ Boolean
44 45 46 47 48 49 50 51 52 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 44 def circles_collide?(x,y, radius, x2,y2, radius2) radii = radius + radius2 if @game.distance(x,y, x2,y2) < radii return true else return false end end |
#draw ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 28 def draw # Background `#{@game.}.fillStyle = #{@background}` `#{@game.}.beginPath()` `#{@game.}.arc(#{@x}, #{@y}, #{@radius}, 0, 2 * Math.PI)` `#{@game.}.fill()` # Joystick `#{@game.}.beginPath()` `#{@game.}.fillStyle = #{@color}` `#{@game.}.arc(#{@joystick_x}, #{@joystick_y}, #{@joystick_radius}, 0, 2 * Math.PI)` `#{@game.}.fill()` return nil end |
#set(key, boolean) ⇒ Object
118 119 120 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 118 def set(key, boolean) @key_states[@buttons[key]] = boolean end |
#trigger_input(threshold = 0.35) ⇒ Object
82 83 84 85 86 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 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 82 def trigger_input(threshold = 0.35) threshold = @radius * threshold if @joystick_x != @x || @joystick_y != @y if (@x - @joystick_x) < -threshold set("right", true) else set("right", false) end if (@x - @joystick_x) > threshold set("left", true) else set("left", false) end if (@y - @joystick_y) < -threshold set("down", true) else set("down", false) end if (@y - @joystick_y) > threshold set("up", true) else set("up", false) end else set("up", false) set("down", false) set("left", false) set("right", false) end end |
#update(touches) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 54 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 |