1
0
Fork 0
space-capture/scenes/ships_fleet/ships_fleet.gd

71 lines
1.5 KiB
GDScript3
Raw Normal View History

2024-08-06 22:46:05 +02:00
extends Node2D
2024-08-15 21:45:07 +02:00
class_name ShipsFleet
2024-08-06 22:46:05 +02:00
2024-08-15 21:45:07 +02:00
@export var departed_at: float # Time.get_ticks_msec()
2024-08-06 22:46:05 +02:00
2024-08-15 21:45:07 +02:00
@export var arrives_at: float # Time.get_ticks_msec()
2024-08-15 11:18:54 +02:00
@export var from: Vector2
@export var to: Vector2
2024-08-06 22:46:05 +02:00
2024-08-15 21:45:07 +02:00
@export var player: Player
2024-08-06 22:46:05 +02:00
@export var count: int :
set(value):
count = value
2024-08-15 21:45:07 +02:00
_update__instances_count()
@export var trail: Trail
var _game: GameState
func register_game(game: GameState) -> void:
_game = game
_game.game_ticked.connect(_update_position)
_update_position(game.current_tick)
func _exit_tree() -> void:
if _game != null:
_game.game_ticked.disconnect(_update_position)
_game = null
2024-08-15 11:18:54 +02:00
#region Graphics
2024-08-15 21:45:07 +02:00
var _instances: Array[Node2D] = []
2024-08-15 11:18:54 +02:00
@export var ship_template: PackedScene
2024-08-15 21:45:07 +02:00
func _update__instances_count() -> void:
var diff = count - _instances.size()
2024-08-06 22:46:05 +02:00
2024-08-15 11:18:54 +02:00
for i in range(diff):
2024-08-15 21:45:07 +02:00
var j = float(1 + _instances.size())
2024-08-15 11:18:54 +02:00
var ship := ship_template.instantiate()
add_child(ship)
ship.position = Vector2.from_angle(j) * j * 2.
2024-08-15 21:45:07 +02:00
_instances.push_back(ship)
2024-08-06 22:46:05 +02:00
2024-08-15 11:18:54 +02:00
for i in range(-diff):
2024-08-15 21:45:07 +02:00
var ship: Node2D = _instances.pop_back()
2024-08-15 11:18:54 +02:00
remove_child(ship)
2024-08-06 22:46:05 +02:00
2024-08-15 21:45:07 +02:00
var _arrived := false
func _update_position(tick: int) -> void:
if not _arrived:
var clamped_tick := clampi(tick, departed_at, arrives_at)
var progress := float(clamped_tick - departed_at) / float(arrives_at - departed_at)
position = lerp(from, to, smoothstep(0., 1., progress))
if tick >= arrives_at:
_arrived = true
trail.show = false
for ship in _instances:
ship.emitting = false
await get_tree().create_timer(5.).timeout
queue_free()
2024-08-15 11:18:54 +02:00
#endregion