Files

101 lines
2.1 KiB
GDScript3
Raw Permalink Normal View History

2025-07-01 22:05:29 +03:00
extends Area2D
@onready var Cursor = $Cursor
2025-07-04 13:50:16 +03:00
@onready var MouseOn = true
@export var ShowMouse = false
var old_mouse_pos = Vector2.ZERO
var mouse_velocity = Vector2.ZERO
2025-07-04 13:50:16 +03:00
@export var vector_length = 360
2025-07-01 22:05:29 +03:00
@export var vector_width = 5
2025-07-04 13:50:16 +03:00
2025-07-01 22:05:29 +03:00
var line_2d : Line2D
var is_touchingCube = false
var collision_normal = Vector2.ZERO
var collision_point = Vector2.ZERO
#var speed = 5000.0
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
2025-07-01 22:05:29 +03:00
#show/hide mouse
if not ShowMouse:
Input.mouse_mode = Input.MOUSE_MODE_HIDDEN
old_mouse_pos = get_viewport().get_mouse_position()
pass # Replace with function body.
2025-07-01 22:05:29 +03:00
#setup line & properties
line_2d = $Line2D
line_2d.width = vector_width
line_2d.default_color = Color(Color.RED)
line_2d.clear_points()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
2025-07-01 22:05:29 +03:00
if is_touchingCube:
2025-07-04 13:50:16 +03:00
# draw_vector()
pass
2025-07-01 22:05:29 +03:00
else:
line_2d.clear_points()
2025-07-04 13:50:16 +03:00
if MouseOn:
var current_mouse_pos = get_viewport().get_mouse_position()
mouse_velocity = current_mouse_pos - old_mouse_pos
if mouse_velocity.length_squared()>0.1:
rotation = lerp_angle(rotation, mouse_velocity.angle(), 80 * delta)
2025-06-29 22:19:02 +03:00
2025-07-04 13:50:16 +03:00
var direction_to_mouse = (current_mouse_pos - global_position).normalized()
global_position = current_mouse_pos
2025-07-04 13:50:16 +03:00
old_mouse_pos = current_mouse_pos
pass
2025-07-01 22:05:29 +03:00
func _on_area_entered(area: Area2D) -> void:
2025-07-04 13:50:16 +03:00
# print("AAAA " + str(area.name))
2025-07-01 22:05:29 +03:00
if area.name == "Node2D":
2025-07-04 13:50:16 +03:00
#freeze cursor on touch block
MouseOn = true
2025-07-01 22:05:29 +03:00
is_touchingCube = true
draw_vector()
pass # Replace with function body.
func _on_area_exited(area: Area2D) -> void:
if area.name == "Node2D":
is_touchingCube = false
pass # Replace with function body.
func draw_vector():
2025-07-02 07:44:47 +12:00
var vector_direction = Vector2.RIGHT.rotated(rotation)
2025-07-01 22:18:04 +03:00
var start_point = line_2d.to_local(global_position)
2025-07-02 07:44:47 +12:00
var end_point = line_2d.to_local(global_position + vector_direction * vector_length)
2025-07-01 22:18:04 +03:00
2025-07-01 22:05:29 +03:00
line_2d.add_point(start_point,1)
line_2d.add_point(end_point,2)
2025-07-04 13:50:16 +03:00
#print("Draws the vector\nStart %s\nEnd: %s\nDirection %s" % [start_point, end_point, vector_direction])
2025-07-01 22:05:29 +03:00
pass