1
0
Fork 0

Refactoring

This commit is contained in:
Fabio Iotti 2024-08-08 23:24:30 +02:00
parent 9e6b648bd6
commit f4d271a6cc
Signed by: bruce965
GPG key ID: 4EC13D9158A96B4C
6 changed files with 81 additions and 46 deletions

View file

@ -20,13 +20,6 @@ var player: Player :
func _ready() -> void: func _ready() -> void:
is_selected = false is_selected = false
func _on_game_start(game: GameLogic) -> void:
pass
func _on_game_tick(game: GameLogic, tick: int) -> void:
if player != game.neutral_player and tick % 10 == 0:
population += 1
@export_category("UI") @export_category("UI")
signal selected(planet: Planet) signal selected(planet: Planet)

View file

@ -5,6 +5,8 @@ class_name Player
@export var icon: Texture2D @export var icon: Texture2D
var pending_operations: Array[GameOperation] = []
func _on_game_start(game: GameLogic) -> void: func _on_game_start(game: GameLogic) -> void:
pass pass

View file

@ -1,8 +1,8 @@
extends Node extends Node
class_name GameLogic class_name GameLogic
const LOGIC_TICKS_PER_SECOND: float = 20.; const LOGIC_TICKS_PER_SECOND: float = 20.
const LOGIC_SECONDS_PER_TICK = 1. / LOGIC_TICKS_PER_SECOND; const LOGIC_SECONDS_PER_TICK = 1. / LOGIC_TICKS_PER_SECOND
## Neutral player owning all non-conquered planets. ## Neutral player owning all non-conquered planets.
@export var neutral_player: Player @export var neutral_player: Player
@ -23,6 +23,14 @@ func _ready() -> void:
_game_init() _game_init()
game_start.emit(self) game_start.emit(self)
func _process(delta: float) -> void:
extra_time += delta
while extra_time > LOGIC_SECONDS_PER_TICK:
extra_time -= LOGIC_SECONDS_PER_TICK
_game_tick(ticks_count)
ticks_count += 1
func _game_init() -> void: func _game_init() -> void:
ticks_count = 0 ticks_count = 0
extra_time = 0. extra_time = 0.
@ -35,13 +43,27 @@ func _game_init() -> void:
var planet := planets[i] var planet := planets[i]
planet.player = players[i] if i < players.size() else neutral_player planet.player = players[i] if i < players.size() else neutral_player
planet.is_selected = false planet.is_selected = false
game_start.connect(planet._on_game_start)
game_tick.connect(planet._on_game_tick)
func _process(delta: float) -> void: func _game_tick(tick: int) -> void:
extra_time += delta for planet in planets:
if planet.player != neutral_player and tick % 10 == 0:
while extra_time > LOGIC_SECONDS_PER_TICK: planet.population += 1
extra_time -= LOGIC_SECONDS_PER_TICK
game_tick.emit(self, ticks_count) for player in players:
ticks_count += 1 for op in player.pending_operations:
if op is GameOperationSendFleet:
var count = min(op.max_count, op.from.population)
op.from.population -= count
if op.from.player == op.to.player:
op.to.population += count
else:
op.to.population -= count
if op.to.population <= 0:
op.to.player = op.from.player
op.to.population = abs(op.from.population)
else:
assert(false, "Unknown GameOperation: %s" % [op])
player.pending_operations.clear()
game_tick.emit(self, tick)

View file

@ -0,0 +1,2 @@
extends RefCounted
class_name GameOperation

View file

@ -0,0 +1,8 @@
extends GameOperation
class_name GameOperationSendFleet
var from: Planet
var to: Planet
var max_count: int

64
ui.gd
View file

@ -5,10 +5,6 @@ class_name GameUI
var game: GameLogic var game: GameLogic
var selected_planet: Planet
var pointed_planet: Planet
var dragging_from_planet: bool
var last_cursor_position: Vector2
func _on_game_started(game: GameLogic) -> void: func _on_game_started(game: GameLogic) -> void:
self.game = game self.game = game
@ -18,7 +14,41 @@ func _on_game_started(game: GameLogic) -> void:
planet.pointer_entered.connect(_on_planet_pointer_entered) planet.pointer_entered.connect(_on_planet_pointer_entered)
planet.pointer_exited.connect(_on_planet_pointer_exited) planet.pointer_exited.connect(_on_planet_pointer_exited)
#trail.color = game.player func _process(delta: float) -> void:
_update_trail(delta, false)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouse:
_handle_mouse_input(event)
#region Mouse input
var last_cursor_position: Vector2
func _handle_mouse_input(event: InputEventMouse) -> void:
if event is InputEventMouseMotion:
# The trail follows the cursor.
last_cursor_position = event.global_position
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.is_released():
# The trail disappears when no longer dragging.
dragging_from_planet = false
# Detect mouse released on a planet and spawn a fleet.
if selected_planet != null and selected_planet.player is LocalPlayer and pointed_planet != null:
var op := GameOperationSendFleet.new()
op.from = selected_planet
op.to = pointed_planet
op.max_count = ceil(selected_planet.population / 2)
selected_planet.player.pending_operations.push_front(op)
#endregion
#region Planets
var selected_planet: Planet
var pointed_planet: Planet
var dragging_from_planet: bool
func _on_planet_selected(planet: Planet) -> void: func _on_planet_selected(planet: Planet) -> void:
if selected_planet != null: if selected_planet != null:
@ -37,26 +67,7 @@ func _on_planet_pointer_exited(planet: Planet) -> void:
if pointed_planet == planet: if pointed_planet == planet:
pointed_planet = null pointed_planet = null
func _unhandled_input(event: InputEvent) -> void: #endregion
if event is InputEventMouseMotion:
# The trail follows the cursor.
last_cursor_position = event.global_position
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.is_released():
# The trail disappears when no longer dragging.
dragging_from_planet = false
# Detect mouse released on a planet and spawn a fleet.
if selected_planet != null and selected_planet.player is LocalPlayer and pointed_planet != null:
var count = ceil(selected_planet.population / 2)
selected_planet.population -= count
if selected_planet.player == pointed_planet.player:
pointed_planet.population += count
else:
pointed_planet.population -= count
if pointed_planet.population <= 0:
pointed_planet.player = selected_planet.player
pointed_planet.population = abs(pointed_planet.population)
#region Trail #region Trail
@ -75,6 +86,3 @@ func _update_trail(delta: float, snap_position: bool) -> void:
trail.end_position = target_position if snap_position else Util.damp(trail.end_position, target_position, 1e-20, delta) trail.end_position = target_position if snap_position else Util.damp(trail.end_position, target_position, 1e-20, delta)
#endregion #endregion
func _process(delta: float) -> void:
_update_trail(delta, false)