space_empire/Manager.gd
2025-10-17 17:39:19 +02:00

115 lines
3.5 KiB
GDScript

extends Node
# Globalne ustawienia
@export var SECTOR_COUNT: int = 15
@export var PLANETS_PER_SECTOR_RANGE: Vector2 = Vector2(1, 5) # min i max planet
# 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 := 30.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():
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.0
_update_planets()
# --- 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)
# --- Aktualizacja planet dla wszystkich sektorów ---
func _update_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
func get_max_population(size: int) -> int:
match size:
1: return 1000
2: return 5000
3: return 15000
4: return 30000
return 1000
# --- Pobranie planety po nazwie w bieżącym sektorze ---
func get_planet_by_name(name: String) -> Dictionary:
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