Compare commits
10 Commits
mouse_vel
...
9e5a51839f
| Author | SHA1 | Date | |
|---|---|---|---|
|
9e5a51839f
|
|||
|
bac0be6e2e
|
|||
|
f10c4aab1c
|
|||
|
8be64c01d1
|
|||
|
0f87a64d5c
|
|||
|
a6c4d56650
|
|||
|
4ebc80d7ee
|
|||
|
38235be363
|
|||
|
4190112800
|
|||
|
354a82d2f8
|
@@ -1,31 +1,84 @@
|
|||||||
extends Control
|
extends Control
|
||||||
class_name SpaceNode
|
class_name SpaceNode
|
||||||
|
|
||||||
|
# adapted from pc_tile code (project-elysium-main)
|
||||||
var initial_pos: Vector2
|
var initial_pos: Vector2
|
||||||
var offset: Vector2
|
var offset: Vector2
|
||||||
var draggable: bool = false
|
var draggable: bool = false
|
||||||
|
var velocity: float = 0
|
||||||
|
|
||||||
|
# variables for velocity calculation
|
||||||
|
var mouse_first_location: Vector2
|
||||||
|
var mouse_last_location: Vector2
|
||||||
|
var mouse_release_location: Vector2
|
||||||
|
var mouse_n_minus_one_location: Vector2
|
||||||
|
var time_since_first_location: float = -1
|
||||||
|
|
||||||
|
# Export variables, for ease of setting without going into the code
|
||||||
|
@export var velocity_coeffecient: int = 500
|
||||||
|
@export var max_x: int = 1000
|
||||||
|
@export var min_x: int = 0
|
||||||
|
@export var ease_out_time: float = 0.5
|
||||||
|
@export var bounce: bool = false
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
draggable = true
|
draggable = true
|
||||||
return
|
return
|
||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
|
global_position.x += velocity
|
||||||
if draggable:
|
if draggable:
|
||||||
|
if (Input.is_action_just_pressed("click")):
|
||||||
|
mouse_first_location = get_global_mouse_position()
|
||||||
|
offset = get_local_mouse_position() - global_position
|
||||||
|
|
||||||
if Input.is_action_pressed("click", true):
|
if Input.is_action_pressed("click"):
|
||||||
self.global_position = Vector2(get_global_mouse_position().x, self.global_position.y)
|
mouse_first_location = get_global_mouse_position()
|
||||||
|
global_position.x += -(mouse_last_location.x - mouse_first_location.x)
|
||||||
|
|
||||||
elif Input.is_action_just_released("click"):
|
elif Input.is_action_just_released("click"):
|
||||||
|
time_since_first_location += delta
|
||||||
|
mouse_release_location = get_global_mouse_position()
|
||||||
|
if mouse_release_location.x < mouse_first_location.x:
|
||||||
|
var distance = mouse_release_location.x - mouse_first_location.x
|
||||||
|
velocity = (distance /
|
||||||
|
(velocity_coeffecient * delta))
|
||||||
|
draggable = false
|
||||||
|
|
||||||
is_dragging.is_dragging = false
|
elif mouse_release_location.x > mouse_first_location.x:
|
||||||
|
var distance = abs(mouse_first_location.x
|
||||||
|
- mouse_release_location.x)
|
||||||
|
velocity = (distance /
|
||||||
|
(velocity_coeffecient * delta))
|
||||||
|
draggable = false
|
||||||
|
|
||||||
|
# when the drag is complete, create a tween to ease out towards zero
|
||||||
var tween = get_tree().create_tween()
|
var tween = get_tree().create_tween()
|
||||||
|
|
||||||
tween.tween_property(self, "global_position", Vector2(get_global_mouse_position().x, self.global_position.y), 0.5).set_ease(Tween.EASE_IN_OUT)
|
tween.tween_property(self, "velocity",
|
||||||
|
0, ease_out_time).set_ease(Tween.EASE_OUT)
|
||||||
|
|
||||||
else:
|
mouse_first_location = Vector2(0,0)
|
||||||
return
|
mouse_release_location = Vector2(0,0)
|
||||||
|
|
||||||
func reset_draggable():
|
draggable = true
|
||||||
if not is_dragging.is_dragging:
|
|
||||||
is_dragging.is_dragging = false
|
if global_position.x >= max_x && velocity > 0:
|
||||||
draggable = false
|
if bounce:
|
||||||
|
velocity = -(velocity)/2
|
||||||
|
print("velocity negated, bouncing")
|
||||||
|
global_position.x = max_x - 1
|
||||||
|
time_since_first_location = -1
|
||||||
|
draggable = true
|
||||||
|
return
|
||||||
|
|
||||||
|
elif global_position.x < min_x && velocity < 0:
|
||||||
|
if bounce:
|
||||||
|
velocity = -(velocity)/2
|
||||||
|
print("velocity negated, bouncing")
|
||||||
|
global_position.x = min_x + 1
|
||||||
|
time_since_first_location = -1
|
||||||
|
draggable = true
|
||||||
|
return
|
||||||
|
|
||||||
|
mouse_last_location = get_global_mouse_position()
|
||||||
|
|||||||
@@ -23,3 +23,4 @@ offset_top = 299.0
|
|||||||
offset_right = 455.0
|
offset_right = 455.0
|
||||||
offset_bottom = 299.0
|
offset_bottom = 299.0
|
||||||
script = ExtResource("2_mej84")
|
script = ExtResource("2_mej84")
|
||||||
|
velocity_coeffecient = 110
|
||||||
|
|||||||
Reference in New Issue
Block a user