Class: AuthorEngine::TouchJoystick
- Inherits:
-
Object
- Object
- AuthorEngine::TouchJoystick
- Defined in:
- lib/author_engine/game/opal/touch_joystick.rb
Instance Attribute Summary collapse
-
#radius ⇒ Object
Returns the value of attribute radius.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #circles_collide?(x, y, radius, x2, y2, radius2) ⇒ Boolean
- #draw ⇒ Object
-
#initialize(x: 0, y: 0, radius:, 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: 0, y: 0, radius:, background: nil, color: nil) ⇒ TouchJoystick
Returns a new instance of TouchJoystick.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 4 def initialize(x: 0, y: 0, radius:, background: nil, color: nil) @x, @y, @radius, @background, @color = x, y, radius, 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}` @color = @game.dark_gray unless @color @background = @game.light_gray unless @background @joystick_x, @joystick_y, @joystick_radius = @x, @y, @radius/2 end |
Instance Attribute Details
#radius ⇒ Object
Returns the value of attribute radius.
3 4 5 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 3 def radius @radius end |
#x ⇒ Object
Returns the value of attribute x.
3 4 5 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 3 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
3 4 5 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 3 def y @y end |
Instance Method Details
#circles_collide?(x, y, radius, x2, y2, radius2) ⇒ Boolean
40 41 42 43 44 45 46 47 48 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 40 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
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 20 def draw #Clear combo = @radius + @joystick_radius `#{@game.}.clearRect(#{@x - combo}, #{@y - combo}, #{combo + combo}, #{combo + combo})` # 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
114 115 116 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 114 def set(key, boolean) @key_states[@buttons[key]] = boolean end |
#trigger_input(threshold = 0.35) ⇒ Object
78 79 80 81 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 |
# File 'lib/author_engine/game/opal/touch_joystick.rb', line 78 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
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 |