From a0a746008e4ba23fd4d1c712173a845f36f0b7d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= Date: Fri, 4 Apr 2025 16:38:43 +0200 Subject: [PATCH] feat: windows moving --- scenes/main.gd | 7 ++++++- scenes/window/draggable.gd | 2 ++ scenes/window/dragon.gd | 35 +++++++++++++++++++++++++++++++++ scenes/window/dragon_popup.tscn | 1 + 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/scenes/main.gd b/scenes/main.gd index fa77b2b..40c3fad 100644 --- a/scenes/main.gd +++ b/scenes/main.gd @@ -30,5 +30,10 @@ func move_window_to_bottom_right(): func _unhandled_input(event: InputEvent) -> void: if event.is_action_pressed("ui_accept"): - var dragon = _instantiator.instantiate() + var dragon: Dragon = _instantiator.instantiate() + dragon.place_back.connect(_dragon_place_back) add_child(dragon) + + +func _dragon_place_back(dragon: Dragon): + print(dragon) diff --git a/scenes/window/draggable.gd b/scenes/window/draggable.gd index a084176..d19f356 100644 --- a/scenes/window/draggable.gd +++ b/scenes/window/draggable.gd @@ -1,4 +1,5 @@ extends Control +class_name Draggable var dragging: bool = false @@ -41,4 +42,5 @@ func _is_inside_main_window() -> bool: func _destroy_dragon() -> void: + dragon.on_place_back() dragon.queue_free() diff --git a/scenes/window/dragon.gd b/scenes/window/dragon.gd index 3a0aa88..109e914 100644 --- a/scenes/window/dragon.gd +++ b/scenes/window/dragon.gd @@ -1,5 +1,40 @@ extends Window class_name Dragon +@export var dragon_speed: float = 20.0 +@onready var _actual_position: Vector2 = position var main_window_rect: Rect2i +@onready var draggable: Draggable = $DragDropDetector + +var _walking: bool = false +var _target_pos: Vector2 + +signal place_back(dragon: Dragon) + + +func on_place_back() -> void: + place_back.emit(self) + + +func _process(delta: float) -> void: + if draggable.dragging: + _actual_position = position + return + + if not _walking: + _pick_random_screen_position() + + _move_to_target(delta) + + +func _move_to_target(delta: float): + if _actual_position.distance_to(_target_pos) > 10.0: + var direction: Vector2 = (_target_pos - _actual_position).normalized() + _actual_position += dragon_speed * direction * delta + position = _actual_position + + +func _pick_random_screen_position() -> void: + _walking = true + _target_pos = Vector2i(10, 10) diff --git a/scenes/window/dragon_popup.tscn b/scenes/window/dragon_popup.tscn index 8693128..ba7887d 100644 --- a/scenes/window/dragon_popup.tscn +++ b/scenes/window/dragon_popup.tscn @@ -13,6 +13,7 @@ unresizable = true borderless = true transparent = true script = ExtResource("1_ctdir") +dragon_speed = 100.0 [node name="DragDropDetector" type="Control" parent="."] layout_mode = 3