refactor: dragon instantiation

This commit is contained in:
Gerard Gascón 2025-04-03 20:46:07 +02:00
parent c75800aaa8
commit 8983b84731
6 changed files with 76 additions and 35 deletions

View file

@ -2,13 +2,23 @@
[ext_resource type="Script" uid="uid://3kyt3shje5r1" path="res://scripts/window.gd" id="1_3vi8l"]
[ext_resource type="Texture2D" uid="uid://fdqnc2qrrvn1" path="res://icon.svg" id="1_rpg24"]
[ext_resource type="PackedScene" uid="uid://c7nfcgjxqeg7l" path="res://dragon.tscn" id="2_8rhti"]
[ext_resource type="PackedScene" uid="uid://c7nfcgjxqeg7l" path="res://dragon_popup.tscn" id="2_8rhti"]
[node name="Base" type="Node2D"]
[node name="Base" type="Node2D" node_paths=PackedStringArray("dragon_spots")]
script = ExtResource("1_3vi8l")
dragon_template = ExtResource("2_8rhti")
dragon_spots = [NodePath("DragonSpot1"), NodePath("DragonSpot2"), NodePath("DragonSpot3")]
[node name="Icon" type="Sprite2D" parent="."]
position = Vector2(576, 324)
scale = Vector2(9, 5.0625)
texture = ExtResource("1_rpg24")
[node name="DragonSpot1" type="Node2D" parent="."]
position = Vector2(259, 450)
[node name="DragonSpot2" type="Node2D" parent="."]
position = Vector2(557, 239)
[node name="DragonSpot3" type="Node2D" parent="."]
position = Vector2(850, 360)

View file

@ -1,26 +1,9 @@
[gd_scene load_steps=4 format=3 uid="uid://c7nfcgjxqeg7l"]
[gd_scene load_steps=2 format=3 uid="uid://baa8gpicw2yg0"]
[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/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
transparent_bg = true
position = Vector2i(0, 36)
size = Vector2i(128, 128)
unresizable = true
borderless = true
transparent = true
script = ExtResource("1_n6spy")
[node name="Dragon" type="Node2D"]
[node name="DragDropDetector" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0
offset_right = 40.0
offset_bottom = 40.0
script = ExtResource("2_qxdfn")
[node name="Icon" type="Sprite2D" parent="."]
position = Vector2(64, 65)
[node name="Sprite" type="Sprite2D" parent="."]
position = Vector2(0, 1)
texture = ExtResource("1_ixu8j")

25
dragon_popup.tscn Normal file
View file

@ -0,0 +1,25 @@
[gd_scene load_steps=4 format=3 uid="uid://c7nfcgjxqeg7l"]
[ext_resource type="Script" uid="uid://bmlkcni4km614" path="res://scripts/dragon/dragon.gd" id="1_ctdir"]
[ext_resource type="Script" uid="uid://ch7d3wo8ucskb" path="res://scripts/dragon/draggable.gd" id="2_2r6si"]
[ext_resource type="PackedScene" uid="uid://baa8gpicw2yg0" path="res://dragon.tscn" id="3_ctdir"]
[node name="DragonPopup" type="Window"]
disable_3d = true
transparent_bg = true
position = Vector2i(0, 36)
size = Vector2i(128, 128)
unresizable = true
borderless = true
transparent = true
script = ExtResource("1_ctdir")
[node name="DragDropDetector" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0
offset_right = 40.0
offset_bottom = 40.0
script = ExtResource("2_2r6si")
[node name="Dragon" parent="." instance=ExtResource("3_ctdir")]
position = Vector2(64, 64)

View file

@ -0,0 +1,27 @@
class_name DragonInstantiator
var _dragon_template: PackedScene
var _positions: Array[Vector2i] = []
func _init(dragon: PackedScene, positions: Array[Node2D]) -> void:
_dragon_template = dragon
for pos in positions:
_positions.push_back((Vector2i)(pos.position))
func instantiate() -> Node:
var position: Vector2i = _positions[0]
return _instantiate_dragon(position)
func _instantiate_dragon(relative_position: Vector2i) -> Node:
var dragon: Window = _dragon_template.instantiate()
var window_position: Vector2i = DisplayServer.window_get_position()
var window_size: Vector2i = DisplayServer.window_get_size()
dragon.position = window_position + relative_position - dragon.size / 2
dragon.main_window_rect = Rect2i(window_position, window_size)
dragon.show()
return dragon

View file

@ -0,0 +1 @@
uid://bevot7ykr7hv2

View file

@ -2,9 +2,14 @@ extends Node
@export var dragon_template: PackedScene
@export var dragon_spots: Array[Node2D]
var _instantiator: DragonInstantiator
func _ready():
_instantiator = DragonInstantiator.new(dragon_template, dragon_spots)
await get_tree().process_frame
move_window_to_bottom_right()
@ -24,15 +29,5 @@ func move_window_to_bottom_right():
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed("ui_accept"):
_instantiate_dragon(Vector2i(0, 0))
func _instantiate_dragon(relative_position: Vector2i) -> void:
var dragon = dragon_template.instantiate()
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()
var dragon = _instantiator.instantiate()
add_child(dragon)