40 lines
896 B
GDScript
40 lines
896 B
GDScript
extends RigidBody2D
|
|
|
|
|
|
@export var drag_spring: float = 100.0
|
|
|
|
|
|
var held: bool = false
|
|
|
|
var _drag_point: Vector2 = Vector2.ZERO
|
|
|
|
|
|
func _init() -> void:
|
|
freeze_mode = FREEZE_MODE_KINEMATIC
|
|
input_pickable = true
|
|
input_event.connect(_on_input_event)
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if held:
|
|
global_position = get_global_mouse_position() - (to_global(_drag_point) - global_position)
|
|
|
|
|
|
func _on_input_event(viewport: Node, event: InputEvent, shape_idx: int) -> void:
|
|
if event is InputEventMouseButton:
|
|
if event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
|
_start_drag()
|
|
if not event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
|
_end_drag()
|
|
|
|
|
|
func _start_drag() -> void:
|
|
held = true
|
|
freeze = true
|
|
_drag_point = get_local_mouse_position()
|
|
|
|
|
|
func _end_drag() -> void:
|
|
held = false
|
|
freeze = false
|
|
apply_central_impulse(Input.get_last_mouse_velocity())
|