diff options
author | altaf-creator <dev@altafcreator.com> | 2024-04-13 14:55:31 +0700 |
---|---|---|
committer | altaf-creator <dev@altafcreator.com> | 2024-04-13 14:55:31 +0700 |
commit | 24f5e910dd8f575adc483f768422ab51fdf905cc (patch) | |
tree | 3486579c0949ec89f3550ee94d35dc20a111ab0d /Scripts/Player.gd |
Initial commit. Exists: NumberNode, Player, dragging and dropping, joining NumberNodes
Diffstat (limited to 'Scripts/Player.gd')
-rw-r--r-- | Scripts/Player.gd | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/Scripts/Player.gd b/Scripts/Player.gd new file mode 100644 index 0000000..e422ba8 --- /dev/null +++ b/Scripts/Player.gd @@ -0,0 +1,39 @@ +extends Node +class_name Player + +@export var max_distance : float +@export var line : Line2D +var target : NumberNode +var legal : bool + +func _ready(): + GlobalVariables.player = self + +func _input(event): + if GlobalVariables.is_snapping: + var target_pos = (line.global_position - get_viewport().get_mouse_position()) + var dist = sqrt(abs(target_pos.x) * abs(target_pos.x) + abs(target_pos.y) * abs(target_pos.y)) + + if dist > max_distance: + legal = false + else: + legal = true + + line.points[1] = Vector2(clampf(dist, 0, self.max_distance), 0) + line.rotation = atan2(target_pos.y, target_pos.x) - deg_to_rad(180) + + if event is InputEventMouseButton && !event.pressed: + GlobalVariables.is_snapping = false + + if target != null: + target.join_from($"..") + + line.points[1] = Vector2.ZERO + + if target != null: + line.rotation = 0 + line.points[1] = -(line.global_position - target.global_position) + +func _on_player_area_2d_input_event(_viewport : Node, event : InputEvent, _shape_idx : int): + if event is InputEventMouseButton && event.pressed: + GlobalVariables.is_snapping = true |