151 lines
3.5 KiB
GDScript
151 lines
3.5 KiB
GDScript
extends Node2D
|
|
|
|
@onready var area = $Area2D
|
|
|
|
# Grafika
|
|
var sprite: Sprite2D = null
|
|
var colonized_icon: Sprite2D = null
|
|
|
|
var base_scale: Vector2 = Vector2(0.3, 0.3)
|
|
|
|
# Orbita
|
|
var orbit_center: Vector2
|
|
var orbit_radius: float
|
|
var rotation_speed: float = 0.02
|
|
var angle: float = 0.0
|
|
|
|
# Statystyki planety
|
|
var population: int = 0
|
|
var mining_rate: float = 0.0
|
|
var resources: float = 0
|
|
var planet_name: String = "Planet"
|
|
|
|
# Rozmiar i potencjał
|
|
var planet_size: int = 1
|
|
var max_population: int = 0
|
|
var is_colonized: bool = false
|
|
|
|
# Czas od ostatniego przyrostu
|
|
var pop_timer: float = 0.0
|
|
var pop_interval: float = 30.0
|
|
|
|
# Sygnały
|
|
signal show_stats(planet)
|
|
signal hide_stats()
|
|
|
|
func _ready():
|
|
_ensure_sprite()
|
|
angle = randf() * TAU
|
|
|
|
# Ikona kolonizacji
|
|
colonized_icon = Sprite2D.new()
|
|
colonized_icon.texture = load("res://assets/icons/col_icon.png") # <-- podaj własną ścieżkę
|
|
colonized_icon.centered = true
|
|
colonized_icon.visible = false
|
|
colonized_icon.scale = Vector2(0.02, 0.02)
|
|
add_child(colonized_icon)
|
|
|
|
if area:
|
|
area.mouse_entered.connect(Callable(self, "_on_mouse_entered"))
|
|
area.mouse_exited.connect(Callable(self, "_on_mouse_exited"))
|
|
area.input_event.connect(Callable(self, "_on_area_input"))
|
|
|
|
func _on_area_input(viewport, event, shape_idx):
|
|
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
|
|
colonize()
|
|
|
|
func _ensure_sprite():
|
|
if sprite == null:
|
|
sprite = Sprite2D.new()
|
|
add_child(sprite)
|
|
sprite.centered = true
|
|
|
|
func set_texture(tex: Texture2D):
|
|
_ensure_sprite()
|
|
sprite.texture = tex
|
|
_apply_scale_for_size()
|
|
|
|
func set_size(new_size: int):
|
|
planet_size = clamp(new_size, 1, 4)
|
|
_apply_scale_for_size()
|
|
|
|
func _apply_scale_for_size():
|
|
match planet_size:
|
|
1:
|
|
base_scale = Vector2(0.20, 0.20)
|
|
max_population = 1000
|
|
2:
|
|
base_scale = Vector2(0.30, 0.30)
|
|
max_population = 5000
|
|
3:
|
|
base_scale = Vector2(0.42, 0.42)
|
|
max_population = 15000
|
|
4:
|
|
base_scale = Vector2(0.60, 0.60)
|
|
max_population = 30000
|
|
if sprite:
|
|
sprite.scale = base_scale
|
|
|
|
func _process(delta):
|
|
# Ruch planety po orbicie
|
|
angle += rotation_speed * delta
|
|
if orbit_center and orbit_radius:
|
|
position = orbit_center + Vector2(cos(angle), sin(angle)) * orbit_radius
|
|
|
|
# Przyrost populacji i surowców
|
|
if is_colonized:
|
|
pop_timer += delta
|
|
if pop_timer >= pop_interval:
|
|
pop_timer = 0.0
|
|
_grow_population()
|
|
_mine_resources()
|
|
|
|
# Pozycja ikonki kolonizacji
|
|
if colonized_icon:
|
|
var y_offset = 0
|
|
if sprite and sprite.texture:
|
|
y_offset = -sprite.texture.get_size().y * 0.13
|
|
colonized_icon.position = Vector2(0, y_offset)
|
|
|
|
func _on_mouse_entered():
|
|
var hover_mul = 1.15
|
|
if sprite:
|
|
sprite.scale = base_scale * hover_mul
|
|
emit_signal("show_stats", self)
|
|
|
|
func _on_mouse_exited():
|
|
if sprite:
|
|
sprite.scale = base_scale
|
|
emit_signal("hide_stats")
|
|
|
|
func colonize():
|
|
if not is_colonized:
|
|
is_colonized = true
|
|
population = 10
|
|
print(planet_name + " has been colonized!")
|
|
emit_signal("show_stats", self)
|
|
if colonized_icon:
|
|
colonized_icon.visible = true
|
|
|
|
func add_population(amount: int):
|
|
population = min(population + amount, max_population)
|
|
|
|
func spend_resources(amount: int) -> bool:
|
|
if resources >= amount:
|
|
resources -= amount
|
|
return true
|
|
return false
|
|
|
|
func _grow_population():
|
|
var growth = int(population / 10) # +10%
|
|
add_population(growth)
|
|
|
|
func _mine_resources():
|
|
var mined = get_mined_per_cycle()
|
|
resources += mined
|
|
|
|
# --- NOWA FUNKCJA ---
|
|
# Zwraca ile surowców planeta wydobywa na jeden cykl (np. co 30 sekund)
|
|
func get_mined_per_cycle() -> float:
|
|
return mining_rate * pow(population, 0.5) * 0.05
|