Enhancements: add documentation
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
extends Control
|
extends Control
|
||||||
|
# INFO name can be anything, doesn't need to be "SpaceNode"
|
||||||
class_name SpaceNode
|
class_name SpaceNode
|
||||||
|
|
||||||
# adapted from pc_tile code (project-elysium-main)
|
# adapted from pc_tile code (project-elysium-main)
|
||||||
var initial_pos: Vector2
|
|
||||||
var offset: Vector2
|
|
||||||
var draggable: bool = false
|
var draggable: bool = false
|
||||||
var velocity: float = 0
|
var velocity: float = 0
|
||||||
|
|
||||||
@@ -26,25 +25,44 @@ func _ready() -> void:
|
|||||||
return
|
return
|
||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
|
# Every frame, update the position based upon the velocity
|
||||||
|
# setting
|
||||||
global_position.x += velocity
|
global_position.x += velocity
|
||||||
|
|
||||||
|
# if the current item is being dragged
|
||||||
if draggable:
|
if draggable:
|
||||||
|
# when the item is first clicked
|
||||||
if (Input.is_action_just_pressed("click")):
|
if (Input.is_action_just_pressed("click")):
|
||||||
mouse_first_location = get_global_mouse_position()
|
mouse_first_location = get_global_mouse_position()
|
||||||
offset = get_local_mouse_position() - global_position
|
|
||||||
|
|
||||||
|
# basically a "while" the click is held
|
||||||
if Input.is_action_pressed("click"):
|
if Input.is_action_pressed("click"):
|
||||||
|
# update the mouse location
|
||||||
mouse_first_location = get_global_mouse_position()
|
mouse_first_location = get_global_mouse_position()
|
||||||
|
# then update global position, for mouse tracking
|
||||||
|
# this is based upon the last location the mouse was, subtracked by the current
|
||||||
|
# location
|
||||||
global_position.x += -(mouse_last_location.x - mouse_first_location.x)
|
global_position.x += -(mouse_last_location.x - mouse_first_location.x)
|
||||||
|
|
||||||
|
# when the click is released
|
||||||
elif Input.is_action_just_released("click"):
|
elif Input.is_action_just_released("click"):
|
||||||
time_since_first_location += delta
|
# make note of where the mouse was released
|
||||||
|
# since we're using a sample of N, N-1 frames, rather than an
|
||||||
|
# average over N frames, we use the in-built
|
||||||
|
# `delta` parameter
|
||||||
mouse_release_location = get_global_mouse_position()
|
mouse_release_location = get_global_mouse_position()
|
||||||
|
|
||||||
|
# simple if/else to compare which direction to go
|
||||||
if mouse_release_location.x < mouse_first_location.x:
|
if mouse_release_location.x < mouse_first_location.x:
|
||||||
|
# displacement calc
|
||||||
var distance = mouse_release_location.x - mouse_first_location.x
|
var distance = mouse_release_location.x - mouse_first_location.x
|
||||||
velocity = (distance /
|
velocity = (distance /
|
||||||
|
# velocity coefficient to adjust sensitivity
|
||||||
(velocity_coeffecient * delta))
|
(velocity_coeffecient * delta))
|
||||||
|
# update to not draggable, to let tween resolve
|
||||||
draggable = false
|
draggable = false
|
||||||
|
|
||||||
|
# other side of the if/else
|
||||||
elif mouse_release_location.x > mouse_first_location.x:
|
elif mouse_release_location.x > mouse_first_location.x:
|
||||||
var distance = abs(mouse_first_location.x
|
var distance = abs(mouse_first_location.x
|
||||||
- mouse_release_location.x)
|
- mouse_release_location.x)
|
||||||
@@ -55,20 +73,28 @@ func _process(delta: float) -> void:
|
|||||||
# when the drag is complete, create a tween to ease out towards zero
|
# 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()
|
||||||
|
|
||||||
|
# ease out the velocity value to zero.
|
||||||
tween.tween_property(self, "velocity",
|
tween.tween_property(self, "velocity",
|
||||||
0, ease_out_time).set_ease(Tween.EASE_OUT)
|
0, ease_out_time).set_ease(Tween.EASE_OUT)
|
||||||
|
|
||||||
|
# reset location values
|
||||||
mouse_first_location = Vector2(0,0)
|
mouse_first_location = Vector2(0,0)
|
||||||
mouse_release_location = Vector2(0,0)
|
mouse_release_location = Vector2(0,0)
|
||||||
|
|
||||||
|
# able to be dragged again
|
||||||
draggable = true
|
draggable = true
|
||||||
|
|
||||||
|
# if/else to handle reaching the "end" of the canvas
|
||||||
|
# as notated by max_x and min_x (see export vars)
|
||||||
if global_position.x >= max_x && velocity > 0:
|
if global_position.x >= max_x && velocity > 0:
|
||||||
if bounce:
|
if bounce:
|
||||||
|
# if bounce is enable, half the velocity and negate it
|
||||||
velocity = -(velocity)/2
|
velocity = -(velocity)/2
|
||||||
print("velocity negated, bouncing")
|
print("velocity negated, bouncing")
|
||||||
|
# set the position to be within the right bounds
|
||||||
global_position.x = max_x - 1
|
global_position.x = max_x - 1
|
||||||
time_since_first_location = -1
|
time_since_first_location = -1
|
||||||
|
# draggable again, possibly redundant
|
||||||
draggable = true
|
draggable = true
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -80,5 +106,6 @@ func _process(delta: float) -> void:
|
|||||||
time_since_first_location = -1
|
time_since_first_location = -1
|
||||||
draggable = true
|
draggable = true
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# finally, update the last known position of the mouse
|
||||||
mouse_last_location = get_global_mouse_position()
|
mouse_last_location = get_global_mouse_position()
|
||||||
|
|||||||
Reference in New Issue
Block a user