137 lines
3.5 KiB
GDScript
137 lines
3.5 KiB
GDScript
extends Node2D
|
|
class_name Planet
|
|
@onready var label = $Label
|
|
|
|
@onready var info_label = $CanvasLayer/Info
|
|
@onready var area = $Area2D
|
|
# --- Timery ---
|
|
var pop_timer: float = 0.0
|
|
var res_timer: float = 0.0
|
|
var cons_timer: float = 0.0
|
|
|
|
var pname: String = ""
|
|
var orbit_radius: float = 30.0
|
|
var orbit_speed: float = 0.01
|
|
var biosfere: float = 0.1
|
|
var is_colonized = false
|
|
var size = 1
|
|
var max_pop = 1
|
|
var population: int = 0
|
|
var res_rich: float = 0.0
|
|
var resources := {}
|
|
|
|
var texture: Texture2D
|
|
var angle = randf() * TAU
|
|
func _ready():
|
|
|
|
add_to_group("planets")
|
|
|
|
# Podłącz sygnały
|
|
area.mouse_entered.connect(_on_mouse_entered)
|
|
area.mouse_exited.connect(_on_mouse_exited)
|
|
area.connect("input_event", Callable(self, "_on_area_input"))
|
|
#str(round(biosfere * 100) / 100.0)
|
|
_update_position()
|
|
|
|
|
|
func _process(delta):
|
|
angle += orbit_speed * delta # obrót w czasie
|
|
angle = fmod(angle, TAU) # utrzymanie kąta w 0..2π
|
|
_update_position()
|
|
if is_colonized:
|
|
_pop_growth(delta)
|
|
_res_growth(delta)
|
|
_res_consumption(delta)
|
|
print_data()
|
|
label.text = str(pname)
|
|
|
|
func _update_position():
|
|
# pozycja lokalna względem gwiazdy
|
|
position = Vector2(cos(angle), sin(angle)) * orbit_radius
|
|
|
|
|
|
func _on_mouse_entered():
|
|
info_label.visible = true
|
|
print_data()
|
|
|
|
|
|
|
|
func _on_area_input(viewport, event, shape_idx):
|
|
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
|
_on_mouse_click()
|
|
|
|
|
|
func _on_mouse_exited():
|
|
info_label.visible = false
|
|
func _on_mouse_click():
|
|
colonize()
|
|
|
|
func colonize():
|
|
if is_colonized == false:
|
|
is_colonized = true
|
|
label.add_theme_color_override("font_color", Color(0.0, 0.454, 0.0, 1.0))
|
|
|
|
population = 100
|
|
print_data()
|
|
|
|
func print_data():
|
|
var text = "🌍 %s\n" % pname
|
|
text += "Biosfera: %.2f\n" % biosfere
|
|
text += "Zasobność surowców: %.2f\n" % res_rich
|
|
text += "Limit populacji: " + str(int(max_pop)).pad_decimals(0) + "\n"
|
|
text += "Populacja: " + str(int(population)).pad_decimals(0) + "\n"
|
|
text += "Status: " + ("Skolonizowana" if is_colonized else "Nieskolonizowana") + "\n"
|
|
var has_any_resource = false
|
|
for key in resources.keys():
|
|
var res = resources[key]
|
|
if res.has("exist") and res["exist"]:
|
|
if not has_any_resource:
|
|
text += "Zasoby:\n"
|
|
has_any_resource = true
|
|
|
|
var display_name = tr(key)
|
|
text += "%s: %.2f +(%s)\n" % [display_name, round(res.get("amount", 0) * 100) / 100.0, check_growth()]
|
|
|
|
|
|
info_label.text = text
|
|
|
|
func _pop_growth(delta):
|
|
pop_timer += delta
|
|
if pop_timer >= Settings.UPDATE_INTERVAL:
|
|
if resources["water"]["amount"] > 0:
|
|
var growth = int(max(1, population * 0.05 * biosfere))
|
|
population = min(population + growth, max_pop)
|
|
pop_timer = 0.0
|
|
else:
|
|
population = max(population - 10, 100)
|
|
pop_timer = 0.0
|
|
func _res_growth(delta):
|
|
res_timer += delta
|
|
if res_timer < Settings.UPDATE_INTERVAL:
|
|
return
|
|
res_timer = 0.0
|
|
|
|
for res_name in resources.keys():
|
|
var res = resources[res_name]
|
|
if res.exist:
|
|
var growth = check_growth()
|
|
res.amount += growth
|
|
resources[res_name] = res
|
|
|
|
|
|
func check_growth() -> float:
|
|
# Zwraca przyrost zaokrąglony do 2 miejsc po przecinku
|
|
var growth = res_rich * (population / 1000.0)
|
|
return round(growth * 100) / 100.0
|
|
|
|
func _res_consumption(delta):
|
|
|
|
var water_usage = population * 0.0002 # np. 0.002 jednostki wody na osobę
|
|
cons_timer += delta
|
|
if cons_timer >= Settings.UPDATE_INTERVAL:
|
|
if "water" in resources:
|
|
resources["water"].amount -= water_usage
|
|
if resources["water"].amount < 0:
|
|
resources["water"].amount = 0
|
|
cons_timer = 0.0
|