Poprawki Autoloader
Poprawiono, zapamietywanie danych planet
This commit is contained in:
parent
097efe3a52
commit
1861a4d94c
113
Manager.gd
113
Manager.gd
@ -1,44 +1,74 @@
|
||||
# Manager.gd
|
||||
extends Node
|
||||
|
||||
# Globalna lista wszystkich planet (słowniki)
|
||||
var planets_data: Array = []
|
||||
# Globalne ustawienia
|
||||
@export var SECTOR_COUNT: int = 15
|
||||
@export var PLANETS_PER_SECTOR_RANGE: Vector2 = Vector2(1, 5) # min i max planet
|
||||
|
||||
# Czas do aktualizacji
|
||||
# Lista sektorów (każdy sektor to słownik z planetami)
|
||||
var sectors_data: Array = []
|
||||
|
||||
# Aktualnie wybrany sektor
|
||||
var current_sector: int = -1
|
||||
|
||||
# Timer aktualizacji
|
||||
var update_timer := 0.0
|
||||
const UPDATE_INTERVAL := 5.0 # co 5 sekund
|
||||
const UPDATE_INTERVAL := 1.0
|
||||
|
||||
# Nazwy planet
|
||||
var planet_names = [
|
||||
"Arcturus","Betelgeuse","Canopus","Deneb","Elnath",
|
||||
"Fomalhaut","Gacrux","Hadar","Izar","Jabbah",
|
||||
"Kaus","Lesath","Menkent","Nunki","Okul",
|
||||
"Pollux","Rigel","Sargas","Toliman","Unukalhai",
|
||||
"Vega","Wezen","Xamidimura","Yildun","Zosma",
|
||||
"Alkaid","Baten","Caph","Diphda","Electra"
|
||||
]
|
||||
|
||||
func _ready():
|
||||
print("[Manager] Initialized global manager")
|
||||
randomize()
|
||||
_initialize_sectors()
|
||||
print("[Manager] Initialized global manager with %d sectors" % SECTOR_COUNT)
|
||||
|
||||
func _process(delta):
|
||||
update_timer += delta
|
||||
if update_timer >= UPDATE_INTERVAL:
|
||||
update_timer = 0
|
||||
update_timer = 0.0
|
||||
_update_planets()
|
||||
|
||||
# --- Tworzenie planety globalnie ---
|
||||
func register_planet(planet_name: String, size: int, mining_rate: float):
|
||||
var planet = {
|
||||
"name": planet_name,
|
||||
"size": size,
|
||||
"population": 0,
|
||||
"max_population": get_max_population(size),
|
||||
"resources": 0.0,
|
||||
"mining_rate": mining_rate,
|
||||
"is_colonized": false
|
||||
}
|
||||
planets_data.append(planet)
|
||||
return planet
|
||||
# --- Inicjalizacja wszystkich sektorów z losowymi planetami ---
|
||||
func _initialize_sectors():
|
||||
for i in range(SECTOR_COUNT):
|
||||
if sectors_data.size() <= i:
|
||||
sectors_data.resize(i + 1)
|
||||
if sectors_data[i] == null:
|
||||
sectors_data[i] = {"planets": []}
|
||||
var planet_count = randi_range(PLANETS_PER_SECTOR_RANGE.x, PLANETS_PER_SECTOR_RANGE.y)
|
||||
for j in range(planet_count):
|
||||
var size = randi_range(1, 3)
|
||||
var planet = {
|
||||
"name": planet_names[randi() % planet_names.size()],
|
||||
"size": size,
|
||||
"population": 0,
|
||||
"max_population": get_max_population(size),
|
||||
"resources": 0.0,
|
||||
"mining_rate": randf_range(0.1, 1.0),
|
||||
"is_colonized": false,
|
||||
"angle": randf() * TAU,
|
||||
"rotation_speed": randf_range(0.01, 0.04),
|
||||
"texture_path": "res://assets/planets/p%d.png" % randi_range(1,18)
|
||||
}
|
||||
sectors_data[i]["planets"].append(planet)
|
||||
|
||||
# --- Symulacja wzrostu i wydobycia ---
|
||||
# --- Aktualizacja planet dla wszystkich sektorów ---
|
||||
func _update_planets():
|
||||
for planet in planets_data:
|
||||
if planet["is_colonized"]:
|
||||
var growth = int(planet["population"] / 10) # +10%
|
||||
planet["population"] = min(planet["population"] + growth, planet["max_population"])
|
||||
planet["resources"] += get_mined_per_cycle(planet)
|
||||
print("[Manager] Updated planets")
|
||||
for sector in sectors_data:
|
||||
if sector == null:
|
||||
continue
|
||||
for planet in sector["planets"]:
|
||||
if planet["is_colonized"]:
|
||||
var growth = int(planet["population"] / 10)
|
||||
planet["population"] = min(planet["population"] + growth, planet["max_population"])
|
||||
planet["resources"] += get_mined_per_cycle(planet)
|
||||
|
||||
func get_mined_per_cycle(planet: Dictionary) -> float:
|
||||
return planet["mining_rate"] * pow(planet["population"], 0.5) * 0.05
|
||||
@ -51,9 +81,34 @@ func get_max_population(size: int) -> int:
|
||||
4: return 30000
|
||||
return 1000
|
||||
|
||||
# --- Funkcje pomocnicze ---
|
||||
# --- Pobranie planety po nazwie w bieżącym sektorze ---
|
||||
func get_planet_by_name(name: String) -> Dictionary:
|
||||
for planet in planets_data:
|
||||
if current_sector < 0 or current_sector >= sectors_data.size():
|
||||
return {}
|
||||
var sector = sectors_data[current_sector]
|
||||
for planet in sector["planets"]:
|
||||
if planet["name"] == name:
|
||||
return planet
|
||||
return {}
|
||||
|
||||
# --- Rejestracja nowej planety w danym sektorze ---
|
||||
func register_planet(sector_index: int, planet_name: String, size: int, mining_rate: float) -> Dictionary:
|
||||
if sectors_data.size() <= sector_index:
|
||||
sectors_data.resize(sector_index + 1)
|
||||
if sectors_data[sector_index] == null:
|
||||
sectors_data[sector_index] = {"planets": []}
|
||||
|
||||
var planet = {
|
||||
"name": planet_name,
|
||||
"size": size,
|
||||
"population": 0,
|
||||
"max_population": get_max_population(size),
|
||||
"resources": 0.0,
|
||||
"mining_rate": mining_rate,
|
||||
"is_colonized": false,
|
||||
"angle": randf() * TAU,
|
||||
"rotation_speed": randf_range(0.01, 0.04),
|
||||
"texture_path": "res://assets/planets/p%d.png" % randi_range(1,18)
|
||||
}
|
||||
sectors_data[sector_index]["planets"].append(planet)
|
||||
return planet
|
||||
|
||||
13
scenes/galaxy.tscn
Normal file
13
scenes/galaxy.tscn
Normal file
@ -0,0 +1,13 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://krtvmtme8s5f"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://2x1k6uk86wqj" path="res://scripts/galaxy.gd" id="1_n7ltq"]
|
||||
|
||||
[node name="galaxy" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 0
|
||||
script = ExtResource("1_n7ltq")
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="."]
|
||||
layout_mode = 0
|
||||
offset_right = 40.0
|
||||
offset_bottom = 40.0
|
||||
@ -41,3 +41,17 @@ offset_left = 10.0
|
||||
offset_top = 118.0
|
||||
offset_right = 260.0
|
||||
offset_bottom = 143.0
|
||||
|
||||
[node name="BackButton" type="Button" parent="."]
|
||||
anchors_preset = -1
|
||||
anchor_left = 1.0
|
||||
anchor_top = 1.0
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
offset_left = 1028.0
|
||||
offset_top = 595.0
|
||||
offset_right = 1114.0
|
||||
offset_bottom = 626.0
|
||||
grow_horizontal = 0
|
||||
grow_vertical = 0
|
||||
text = "Galaktyka"
|
||||
|
||||
26
scripts/galaxy.gd
Normal file
26
scripts/galaxy.gd
Normal file
@ -0,0 +1,26 @@
|
||||
extends Control
|
||||
|
||||
#@export var SECTOR_COUNT: int = 15
|
||||
@export var SectorScenePath: String = "res://scenes/Sector.tscn"
|
||||
@onready var container = $VBoxContainer
|
||||
|
||||
func _ready():
|
||||
_create_sector_buttons()
|
||||
|
||||
func _create_sector_buttons():
|
||||
for child in container.get_children():
|
||||
child.queue_free()
|
||||
for i in range(Manager.SECTOR_COUNT):
|
||||
var button = Button.new()
|
||||
button.text = "Sector %d" % (i+1)
|
||||
button.custom_minimum_size = Vector2(200, 40)
|
||||
var index = i
|
||||
button.pressed.connect(func() -> void:
|
||||
_on_sector_selected(index)
|
||||
)
|
||||
container.add_child(button)
|
||||
|
||||
func _on_sector_selected(index: int):
|
||||
Manager.current_sector = index
|
||||
var sector_scene = load(SectorScenePath)
|
||||
get_tree().change_scene_to_packed(sector_scene)
|
||||
1
scripts/galaxy.gd.uid
Normal file
1
scripts/galaxy.gd.uid
Normal file
@ -0,0 +1 @@
|
||||
uid://2x1k6uk86wqj
|
||||
@ -1,25 +1,33 @@
|
||||
extends Node2D
|
||||
class_name Planet
|
||||
|
||||
@onready var area = $Area2D
|
||||
|
||||
var sprite: Sprite2D
|
||||
var colonized_icon: Sprite2D
|
||||
|
||||
var orbit_center: Vector2
|
||||
var orbit_radius: float
|
||||
# Dane orbitalne
|
||||
var orbit_center: Vector2 = Vector2.ZERO
|
||||
var orbit_radius: float = 0.0
|
||||
var rotation_speed: float = 0.02
|
||||
var angle: float = 0.0
|
||||
|
||||
# Połączenie z globalnymi danymi
|
||||
var data: Dictionary
|
||||
# Dane planety (przypisywane z Managera)
|
||||
var data: Dictionary = {}
|
||||
|
||||
signal show_stats(planet)
|
||||
signal hide_stats()
|
||||
|
||||
func _ready():
|
||||
_ensure_sprite()
|
||||
angle = randf() * TAU
|
||||
|
||||
# Jeśli zapisano pozycję i kąt w danych, użyj ich
|
||||
if data.has("angle"):
|
||||
angle = data["angle"]
|
||||
else:
|
||||
angle = randf() * TAU
|
||||
|
||||
# Ikona kolonizacji
|
||||
colonized_icon = Sprite2D.new()
|
||||
colonized_icon.texture = load("res://assets/icons/col_icon.png")
|
||||
colonized_icon.centered = true
|
||||
@ -41,21 +49,25 @@ func _ensure_sprite():
|
||||
func _process(delta):
|
||||
# Ruch orbitalny
|
||||
angle += rotation_speed * delta
|
||||
if orbit_center and orbit_radius:
|
||||
if orbit_center != Vector2.ZERO and orbit_radius > 0.0:
|
||||
position = orbit_center + Vector2(cos(angle), sin(angle)) * orbit_radius
|
||||
# zapisujemy kąt do danych, żeby pamiętać pozycję
|
||||
if data != null:
|
||||
data["angle"] = angle
|
||||
|
||||
# Aktualizacja ikonki kolonizacji
|
||||
if colonized_icon:
|
||||
if colonized_icon != null and data != null:
|
||||
colonized_icon.visible = data["is_colonized"]
|
||||
var y_offset = 0
|
||||
if sprite and sprite.texture:
|
||||
var y_offset = 0.0
|
||||
if sprite != null and sprite.texture != null:
|
||||
y_offset = -sprite.texture.get_size().y * 0.13
|
||||
colonized_icon.position = Vector2(0, y_offset)
|
||||
|
||||
func set_texture(tex: Texture2D):
|
||||
_ensure_sprite()
|
||||
sprite.texture = tex
|
||||
sprite.scale = get_scale_for_size(data["size"])
|
||||
if data != null and data.has("size"):
|
||||
sprite.scale = get_scale_for_size(data["size"])
|
||||
|
||||
func get_scale_for_size(size: int) -> Vector2:
|
||||
match size:
|
||||
@ -70,18 +82,18 @@ func _on_area_input(viewport, event, shape_idx):
|
||||
colonize()
|
||||
|
||||
func colonize():
|
||||
if not data["is_colonized"]:
|
||||
if data != null and not data["is_colonized"]:
|
||||
data["is_colonized"] = true
|
||||
data["population"] = 10
|
||||
print(data["name"] + " has been colonized!")
|
||||
emit_signal("show_stats", self)
|
||||
|
||||
func _on_mouse_entered():
|
||||
if sprite:
|
||||
if sprite != null and data != null:
|
||||
sprite.scale = get_scale_for_size(data["size"]) * 1.15
|
||||
emit_signal("show_stats", self)
|
||||
|
||||
func _on_mouse_exited():
|
||||
if sprite:
|
||||
if sprite != null and data != null:
|
||||
sprite.scale = get_scale_for_size(data["size"])
|
||||
emit_signal("hide_stats")
|
||||
|
||||
@ -3,6 +3,7 @@ extends Node2D
|
||||
var StarScene = preload("res://scenes/Star.tscn")
|
||||
var PlanetScene = preload("res://scenes/Planet.tscn")
|
||||
@onready var PlanetStatPanel = $PlanetStatPanel
|
||||
@onready var BackButton = $BackButton
|
||||
|
||||
const ORBIT_STEP = 120
|
||||
var tracked_planet: Node = null
|
||||
@ -12,45 +13,87 @@ func _ready():
|
||||
_init_background()
|
||||
spawn_star()
|
||||
spawn_planets()
|
||||
BackButton.pressed.connect(_on_back_pressed)
|
||||
|
||||
func _process(delta):
|
||||
if tracked_planet:
|
||||
_update_panel()
|
||||
func _on_back_pressed():
|
||||
get_tree().change_scene_to_file("res://scenes/Galaxy.tscn")
|
||||
|
||||
# --- Planet spawning ---
|
||||
func spawn_planets():
|
||||
var center = get_viewport_rect().size / 2
|
||||
var sector_index = Manager.current_sector
|
||||
|
||||
if sector_index < 0:
|
||||
push_error("Current sector is not set!")
|
||||
return
|
||||
|
||||
# przygotuj sektor jeśli jeszcze nie istnieje
|
||||
if Manager.sectors_data.size() <= sector_index:
|
||||
Manager.sectors_data.resize(sector_index + 1)
|
||||
if Manager.sectors_data[sector_index] == null:
|
||||
Manager.sectors_data[sector_index] = {"planets": [], "star": {}}
|
||||
elif not Manager.sectors_data[sector_index].has("planets"):
|
||||
Manager.sectors_data[sector_index]["planets"] = []
|
||||
|
||||
var sector_planets = Manager.sectors_data[sector_index]["planets"]
|
||||
|
||||
for i in range(sector_planets.size()):
|
||||
var planet_data = sector_planets[i]
|
||||
var planet_node = PlanetScene.instantiate()
|
||||
add_child(planet_node)
|
||||
|
||||
planet_node.data = planet_data
|
||||
planet_node.orbit_center = center
|
||||
planet_node.orbit_radius = planet_data.get("orbit_radius", ORBIT_STEP * (i + 1))
|
||||
planet_node.angle = planet_data.get("angle", randf() * TAU)
|
||||
planet_node.rotation_speed = planet_data.get("rotation_speed", randf_range(0.01, 0.04))
|
||||
|
||||
# Tekstura
|
||||
if planet_data.has("texture_path"):
|
||||
planet_node.set_texture(load(planet_data["texture_path"]))
|
||||
else:
|
||||
var tex_path = "res://assets/planets/p%d.png" % randi_range(1,18)
|
||||
planet_node.set_texture(load(tex_path))
|
||||
planet_data["texture_path"] = tex_path
|
||||
|
||||
planet_node.connect("show_stats", Callable(self, "_on_show_stats"))
|
||||
planet_node.connect("hide_stats", Callable(self, "_on_hide_stats"))
|
||||
|
||||
# Zapis aktualnych parametrów do Managera
|
||||
planet_data["orbit_radius"] = planet_node.orbit_radius
|
||||
planet_data["angle"] = planet_node.angle
|
||||
planet_data["rotation_speed"] = planet_node.rotation_speed
|
||||
|
||||
# --- Star ---
|
||||
func spawn_star():
|
||||
var sector_index = Manager.current_sector
|
||||
if sector_index < 0:
|
||||
push_error("Current sector is not set!")
|
||||
return
|
||||
|
||||
if not Manager.sectors_data[sector_index].has("star"):
|
||||
Manager.sectors_data[sector_index]["star"] = {}
|
||||
|
||||
var star_data = Manager.sectors_data[sector_index]["star"]
|
||||
|
||||
var star = StarScene.instantiate()
|
||||
add_child(star)
|
||||
star.position = get_viewport_rect().size / 2
|
||||
star.set_texture(random_star_texture())
|
||||
|
||||
func spawn_planets():
|
||||
var center = get_viewport_rect().size / 2
|
||||
# jeśli gwiazda ma zapisaną teksturę – użyj jej
|
||||
if star_data.has("texture_path"):
|
||||
star.set_texture(load(star_data["texture_path"]))
|
||||
else:
|
||||
var tex_path = random_star_texture_path()
|
||||
star.set_texture(load(tex_path))
|
||||
star_data["texture_path"] = tex_path
|
||||
Manager.sectors_data[sector_index]["star"] = star_data
|
||||
|
||||
# jeśli Manager nie ma jeszcze planet — utwórz kilka losowych
|
||||
if Manager.planets_data.size() == 0:
|
||||
var names = ["Arcturus","Betelgeuse","Canopus","Deneb","Elnath"]
|
||||
for name in names:
|
||||
var planet = Manager.register_planet(
|
||||
name,
|
||||
randi_range(1, 3),
|
||||
randf_range(0.1, 1.0)
|
||||
)
|
||||
|
||||
# stwórz instancje wizualne
|
||||
for i in range(Manager.planets_data.size()):
|
||||
var planet_data = Manager.planets_data[i]
|
||||
var planet = PlanetScene.instantiate()
|
||||
add_child(planet)
|
||||
|
||||
planet.orbit_center = center
|
||||
planet.orbit_radius = (i + 1) * ORBIT_STEP
|
||||
planet.rotation_speed = randf_range(0.01, 0.04)
|
||||
planet.data = planet_data
|
||||
planet.set_texture(random_planet_texture())
|
||||
|
||||
planet.connect("show_stats", Callable(self, "_on_show_stats"))
|
||||
planet.connect("hide_stats", Callable(self, "_on_hide_stats"))
|
||||
func random_star_texture_path() -> String:
|
||||
var id = randi_range(1,16)
|
||||
return "res://assets/stars/s%d.png" % id
|
||||
|
||||
# --- Background ---
|
||||
func _init_background():
|
||||
var background = Sprite2D.new()
|
||||
background.texture = load("res://assets/background/space1.jpg")
|
||||
@ -63,12 +106,6 @@ func _init_background():
|
||||
add_child(background)
|
||||
background.z_index = -10
|
||||
|
||||
func random_star_texture() -> Texture2D:
|
||||
return load("res://assets/stars/s%d.png" % randi_range(1, 16))
|
||||
|
||||
func random_planet_texture() -> Texture2D:
|
||||
return load("res://assets/planets/p%d.png" % randi_range(1, 18))
|
||||
|
||||
# --- GUI ---
|
||||
func _on_show_stats(planet):
|
||||
tracked_planet = planet
|
||||
@ -76,18 +113,15 @@ func _on_show_stats(planet):
|
||||
_update_panel()
|
||||
|
||||
func _on_hide_stats():
|
||||
PlanetStatPanel.visible = false
|
||||
tracked_planet = null
|
||||
PlanetStatPanel.visible = false
|
||||
|
||||
func _update_panel():
|
||||
if tracked_planet == null:
|
||||
if tracked_planet == null or tracked_planet.data == null:
|
||||
return
|
||||
var d = tracked_planet.data
|
||||
var colonized_text = ""
|
||||
if d["is_colonized"]:
|
||||
colonized_text = " (Skolonizowana)"
|
||||
|
||||
PlanetStatPanel.get_node("PlanetName").text = "Name: " + d["name"] + colonized_text
|
||||
PlanetStatPanel.get_node("PopulationPanel").text = "Populacja: %d / %d" % [d["population"], d["max_population"]]
|
||||
PlanetStatPanel.get_node("MiningRate").text = "Wydobycie: %.2f" % d["mining_rate"]
|
||||
PlanetStatPanel.get_node("ResourcePlanet").text = "Surowce: %.2f" % d["resources"]
|
||||
var colonized_text = " (Skolonizowana)" if tracked_planet.data["is_colonized"] else ""
|
||||
PlanetStatPanel.get_node("PlanetName").text = "Name: " + tracked_planet.data["name"] + colonized_text
|
||||
PlanetStatPanel.get_node("PopulationPanel").text = "Populacja: %d / %d" % [tracked_planet.data["population"], tracked_planet.data["max_population"]]
|
||||
PlanetStatPanel.get_node("MiningRate").text = "Współczynnik wydobycia: %.2f" % tracked_planet.data["mining_rate"]
|
||||
var mined_per_cycle = Manager.get_mined_per_cycle(tracked_planet.data)
|
||||
PlanetStatPanel.get_node("ResourcePlanet").text = "Surowce: %.2f (+%.2f)" % [tracked_planet.data["resources"], mined_per_cycle]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user