-
Notifications
You must be signed in to change notification settings - Fork 8
/
JoyStick.gd
52 lines (49 loc) · 1.71 KB
/
JoyStick.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#Author: Rodrigo Torres
#Version: 1.1.0
#For Godot 3.x
# Module used to control a joystick made of two circles.
# The position of the joystick is fixed if controlled by the mouse.
# It's under the finger if controlled by touch
extends Node2D
signal move
onready var big_circle = get_node("BigCircle")
onready var small_circle = get_node("SmallCircle")
onready var big_circle_radius = big_circle.texture.get_size().x / 2
var input_ongoing = false
func ready():
# need to test
if OS.has_touchscreen_ui_hint():
self.visible=false
func _input(event):
# If it's a touch event (pressed, released)
if event is InputEventScreenTouch or event is InputEventMouseButton:
if not event.pressed:
# Stop moving.
emit_signal('move', Vector2())
# Reset joystick position
small_circle.global_position = big_circle.global_position
# Stop tracking position
input_ongoing = false
# If touch screen, hide the control
if event is InputEventScreenTouch:
self.visible = false
return
else:
# Start tracking position
input_ongoing = true
# If touch screen, show control under the finger
if event is InputEventScreenTouch:
self.visible = true
self.position = event.position
return
# Move event: set joystick position
if (event is InputEventMouseMotion or event is InputEventScreenDrag) and input_ongoing:
var motion_vector = event.position - big_circle.get_global_position()
if motion_vector.length() > big_circle_radius:
small_circle.set_position(motion_vector.normalized() * big_circle_radius)
else:
small_circle.set_global_position(event.position)
func _process(delta):
if input_ongoing:
var vector = small_circle.position / big_circle_radius
emit_signal("move", vector*vector*vector)