45 lines
1.8 KiB
GDScript
45 lines
1.8 KiB
GDScript
extends Control
|
|
|
|
var pressed: bool = false
|
|
var previous_position: Vector2 = Vector2(0, 0)
|
|
var touch_delta: Vector2 = Vector2(0, 0)
|
|
var current_delta: Vector2 = Vector2(0, 0)
|
|
var dampening_factor: float = 0.8 # Lower values = more dampening (0-1 range)
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(delta: float) -> void:
|
|
var t = clamp(dampening_factor * delta, 0.0, 1.0) # Clamp to 0-1 range
|
|
current_delta = lerp(current_delta, Vector2(0, 0), ease(t, -0.5)) # Use easing with lerp
|
|
|
|
# Simulate joystick motion using the dampened delta.
|
|
simulate_joystick_motion(JOY_AXIS_RIGHT_X, -current_delta.x * 0.1, 0)
|
|
|
|
# Debug: See the current delta
|
|
#print(current_delta)
|
|
|
|
func _on_gui_input(event: InputEvent) -> void:
|
|
# Filter touch or drag events only.
|
|
if event is not InputEventScreenTouch and event is not InputEventScreenDrag:
|
|
return
|
|
|
|
if event is InputEventScreenTouch:
|
|
if event.is_pressed():
|
|
pressed = true
|
|
previous_position = event.position # Initialize previous position on touch start
|
|
else:
|
|
pressed = false
|
|
touch_delta = Vector2(0, 0) # Reset delta on touch release
|
|
|
|
if pressed and event is InputEventScreenDrag:
|
|
# Calculate the delta between the current and previous touch position.
|
|
touch_delta = event.position - previous_position
|
|
previous_position = event.position
|
|
current_delta = touch_delta * 0.5 # Update current delta with the new drag value
|
|
|
|
func simulate_joystick_motion(axis: int, value: float, device_id: int = 0) -> void:
|
|
var joystick_event = InputEventJoypadMotion.new()
|
|
joystick_event.device = device_id
|
|
joystick_event.axis = axis # Axis index (e.g., 0 for X-axis, 1 for Y-axis)
|
|
joystick_event.axis_value = value # Value of the axis, typically between -1 and 1
|
|
|
|
Input.parse_input_event(joystick_event) # Process the event
|