feat: create and destroy dragons
This commit is contained in:
parent
9e153b63ac
commit
c75800aaa8
7 changed files with 57 additions and 32 deletions
|
@ -1,7 +1,8 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://c7nfcgjxqeg7l"]
|
||||
[gd_scene load_steps=4 format=3 uid="uid://c7nfcgjxqeg7l"]
|
||||
|
||||
[ext_resource type="Texture2D" uid="uid://fdqnc2qrrvn1" path="res://icon.svg" id="1_ixu8j"]
|
||||
[ext_resource type="Script" uid="uid://bmlkcni4km614" path="res://scripts/dragon.gd" id="1_n6spy"]
|
||||
[ext_resource type="Script" uid="uid://bmlkcni4km614" path="res://scripts/dragon/dragon.gd" id="1_n6spy"]
|
||||
[ext_resource type="Script" uid="uid://ch7d3wo8ucskb" path="res://scripts/dragon/draggable.gd" id="2_qxdfn"]
|
||||
|
||||
[node name="Dragon" type="Window"]
|
||||
disable_3d = true
|
||||
|
@ -11,13 +12,14 @@ size = Vector2i(128, 128)
|
|||
unresizable = true
|
||||
borderless = true
|
||||
transparent = true
|
||||
script = ExtResource("1_n6spy")
|
||||
|
||||
[node name="DragDropDetector" type="Control" parent="."]
|
||||
layout_mode = 3
|
||||
anchors_preset = 0
|
||||
offset_right = 40.0
|
||||
offset_bottom = 40.0
|
||||
script = ExtResource("1_n6spy")
|
||||
script = ExtResource("2_qxdfn")
|
||||
|
||||
[node name="Icon" type="Sprite2D" parent="."]
|
||||
position = Vector2(64, 65)
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
extends Control
|
||||
|
||||
|
||||
var dragging = false
|
||||
var dragging_start_position = Vector2i()
|
||||
|
||||
@onready var window: Window = $".."
|
||||
|
||||
|
||||
func _ready():
|
||||
set_process_input(true)
|
||||
|
||||
|
||||
func _input(event):
|
||||
if event is not InputEventMouseButton:
|
||||
return
|
||||
if event.button_index != MOUSE_BUTTON_LEFT:
|
||||
return
|
||||
|
||||
if event.pressed:
|
||||
dragging = true
|
||||
dragging_start_position = Vector2i(get_global_mouse_position())
|
||||
else:
|
||||
dragging = false
|
||||
|
||||
|
||||
func _process(_delta):
|
||||
if dragging:
|
||||
window.position = window.position + Vector2i(get_global_mouse_position()) - dragging_start_position
|
44
scripts/dragon/draggable.gd
Normal file
44
scripts/dragon/draggable.gd
Normal file
|
@ -0,0 +1,44 @@
|
|||
extends Control
|
||||
|
||||
|
||||
var dragging: bool = false
|
||||
var dragging_start_position: Vector2i = Vector2i()
|
||||
|
||||
@onready var dragon: Dragon = $".."
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
set_process_input(true)
|
||||
|
||||
|
||||
func _input(event) -> void:
|
||||
if event is not InputEventMouseButton:
|
||||
return
|
||||
if event.button_index != MOUSE_BUTTON_LEFT:
|
||||
return
|
||||
|
||||
if event.pressed:
|
||||
dragging = true
|
||||
dragging_start_position = Vector2i(get_global_mouse_position())
|
||||
else:
|
||||
dragging = false
|
||||
if _is_inside_main_window():
|
||||
_destroy_dragon()
|
||||
|
||||
|
||||
func _process(_delta) -> void:
|
||||
if dragging:
|
||||
dragon.position = dragon.position + Vector2i(get_global_mouse_position()) - dragging_start_position
|
||||
|
||||
|
||||
func _is_inside_main_window() -> bool:
|
||||
var id: int = get_window().get_window_id()
|
||||
var window_position: Vector2i = DisplayServer.window_get_position(id)
|
||||
var window_size: Vector2i = DisplayServer.window_get_size(id)
|
||||
var rect: Rect2i = Rect2i(window_position, window_size)
|
||||
|
||||
return rect.intersects(dragon.main_window_rect)
|
||||
|
||||
|
||||
func _destroy_dragon() -> void:
|
||||
dragon.queue_free()
|
1
scripts/dragon/draggable.gd.uid
Normal file
1
scripts/dragon/draggable.gd.uid
Normal file
|
@ -0,0 +1 @@
|
|||
uid://ch7d3wo8ucskb
|
5
scripts/dragon/dragon.gd
Normal file
5
scripts/dragon/dragon.gd
Normal file
|
@ -0,0 +1,5 @@
|
|||
extends Window
|
||||
class_name Dragon
|
||||
|
||||
|
||||
var main_window_rect: Rect2i
|
|
@ -32,5 +32,7 @@ func _instantiate_dragon(relative_position: Vector2i) -> void:
|
|||
add_child(dragon)
|
||||
if dragon is Window:
|
||||
var window_position: Vector2i = DisplayServer.window_get_position()
|
||||
var window_size: Vector2i = DisplayServer.window_get_size()
|
||||
dragon.position = window_position + relative_position
|
||||
dragon.main_window_rect = Rect2i(window_position, window_size)
|
||||
dragon.show()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue