1
0
Fork 0
space-capture/scripts/game_logic.gd

78 lines
2.2 KiB
GDScript3
Raw Normal View History

2024-08-06 22:46:05 +02:00
class_name GameLogic
2024-08-15 11:18:54 +02:00
extends Node
const LOGIC_TICKS_PER_SECOND := 20.
const LOGIC_SECONDS_PER_TICK := 1. / LOGIC_TICKS_PER_SECOND
2024-08-06 22:46:05 +02:00
2024-08-15 11:18:54 +02:00
## Templates for spawnable stuff.
@export var templates: TemplateScenes
2024-08-06 22:46:05 +02:00
2024-08-15 11:18:54 +02:00
## Container for planets.
@export var planets_container: Node2D
## Interface to interact with planets.
@export var planets_ui: PlanetsUI
2024-08-06 22:46:05 +02:00
## Game players.
2024-08-15 11:18:54 +02:00
@export var players: Array[Player]
2024-08-06 22:46:05 +02:00
## Game planets.
2024-08-15 11:18:54 +02:00
@export var planets: Array[Planet]
2024-08-06 22:46:05 +02:00
2024-08-15 11:18:54 +02:00
var _game: GameState
var _planet_controls: Array[ControlPlanet]
2024-08-06 22:46:05 +02:00
2024-08-15 11:18:54 +02:00
var _extra_time: float
2024-08-06 22:46:05 +02:00
func _ready() -> void:
2024-08-15 11:18:54 +02:00
# New game.
_game = GameState.new()
# Add players.
for i in range(players.size()):
_game.add_player()
# Add planets.
for i in range(planets.size()):
# Data.
var data := GameState.PlanetData.new()
data.position = Vector2i(planets[i].location)
data.grow_every_ticks = 10
data.player_id = (i + 1) if (i < players.size() - 1) else 0
data.population = 0
_game.add_planet(data)
# UI control.
var control: ControlPlanet = templates.control_planet.instantiate()
control.position = planets[i].location
control.player = players[data.player_id]
control.population = data.population
_planet_controls.push_back(control)
planets_container.add_child(control)
planets_ui.register_planet(control)
# Register UI signals.
_game.planet_player_changed.connect(_set_planet_player)
_game.planet_population_changed.connect(_set_planet_population)
planets_ui.fleet_dispatched.connect(_dispatch_fleet)
2024-08-06 22:46:05 +02:00
2024-08-08 23:24:30 +02:00
func _process(delta: float) -> void:
2024-08-15 11:18:54 +02:00
_extra_time += delta
2024-08-08 23:24:30 +02:00
2024-08-15 11:18:54 +02:00
while _extra_time > LOGIC_SECONDS_PER_TICK:
_extra_time -= LOGIC_SECONDS_PER_TICK
_game.tick()
2024-08-08 23:24:30 +02:00
2024-08-15 11:18:54 +02:00
func _set_planet_player(planet_id: int, player_id: int) -> void:
_planet_controls[planet_id].player = players[player_id]
2024-08-06 22:46:05 +02:00
2024-08-15 11:18:54 +02:00
func _set_planet_population(planet_id: int, population: int) -> void:
_planet_controls[planet_id].population = population
2024-08-06 22:46:05 +02:00
2024-08-15 11:18:54 +02:00
func _dispatch_fleet(from: ControlPlanet, to: ControlPlanet) -> void:
var player_id := players.find(from.player)
var from_planet_id := _planet_controls.find(from)
var to_planet_id := _planet_controls.find(to)
var max_count: int = ceil(from.population / 2.)
_game.dispatch_fleet(player_id, from_planet_id, to_planet_id, max_count)