111 lines
2.5 KiB
GDScript
111 lines
2.5 KiB
GDScript
extends Panel
|
||
|
||
|
||
var dragging := false
|
||
var drag_offset := Vector2.ZERO
|
||
|
||
func _gui_input(event):
|
||
if event is InputEventMouseButton:
|
||
if event.button_index == MOUSE_BUTTON_LEFT:
|
||
if event.pressed:
|
||
# Kliknięto na panel → zaczynamy drag
|
||
dragging = true
|
||
drag_offset = get_local_mouse_position()
|
||
else:
|
||
# Zwolniono przycisk → kończymy drag
|
||
dragging = false
|
||
elif event is InputEventMouseMotion and dragging:
|
||
# Przesuwamy panel względem pozycji myszy
|
||
position += event.relative
|
||
|
||
|
||
var current_station = null
|
||
@onready var ships_container = $ShipContainer
|
||
|
||
func _ready():
|
||
generate_ship_buttons()
|
||
|
||
func generate_ship_buttons():
|
||
# Usuwamy stare guziki
|
||
for c in ships_container.get_children():
|
||
c.queue_free()
|
||
|
||
# Pobieramy statki z autoloadu
|
||
var all_ships = ShipsType.SHIP_TYPES
|
||
|
||
# Pobieramy odblokowane technologie gracza
|
||
var unlocked = PlayerData.unlocked_techs
|
||
|
||
for ship_id in all_ships.keys():
|
||
var data = all_ships[ship_id]
|
||
|
||
# 1. Sprawdzenie technologii
|
||
if data.required_tech != null and not unlocked.has(data.required_tech):
|
||
continue
|
||
|
||
# 2. Tworzymy guzik
|
||
var btn = Button.new()
|
||
btn.text = data.name
|
||
|
||
# 3. Podpinamy akcję
|
||
btn.pressed.connect(func():
|
||
build_ship(ship_id)
|
||
)
|
||
|
||
# 4. Dodajemy do UI
|
||
ships_container.add_child(btn)
|
||
|
||
func show_for_station(station):
|
||
self.visible = true
|
||
current_station = station
|
||
|
||
|
||
func _on_button_2_pressed() -> void:
|
||
self.visible = false
|
||
|
||
|
||
#func _on_button_pressed() -> void:
|
||
#build_ship("frigate")
|
||
#
|
||
#func _on_button_3_pressed() -> void:
|
||
#build_ship("transport")
|
||
|
||
|
||
|
||
func build_ship(type: String):
|
||
if current_station == null:
|
||
print("Brak stacji – nie można budować statku")
|
||
return
|
||
|
||
var cost = ShipsType.SHIP_TYPES[type].cost
|
||
|
||
# 1. Sprawdź zasoby gracza
|
||
for res in cost.keys():
|
||
if PlayerData.resources[res] < cost[res]:
|
||
print("Brakuje:", res)
|
||
return
|
||
|
||
# 2. Odejmij zasoby gracza
|
||
for res in cost.keys():
|
||
PlayerData.resources[res] -= cost[res]
|
||
PlayerData.emit_signal("stats_changed")
|
||
# 3. Stwórz statek
|
||
var ship = preload("res://scenes/ship.tscn").instantiate()
|
||
ship.setup(type)
|
||
|
||
# POZYCJA SPAWNU — np. obok stacji
|
||
ship.position = current_station.position + Vector2(15, 15)
|
||
# Ustawiamy prędkość z listy statków
|
||
ship.speed = ShipsType.SHIP_TYPES[type].speed
|
||
|
||
# Nadajemy kierunek (np. losowy)
|
||
ship.direction = Vector2(randf() * 2 - 1, randf() * 2 - 1).normalized()
|
||
|
||
# Obrót zgodnie z kierunkiem
|
||
ship.rotation = ship.direction.angle()
|
||
|
||
# Dodaj do sceny
|
||
get_tree().root.get_node("Galaxy").add_child(ship)
|
||
|
||
print("Zbudowano statek:", type)
|