cosmic/scripts/Ship/ship.gd

54 lines
1.6 KiB
GDScript

extends Node2D
class_name Ship
# TODO: add weapons
## Emits when hull hp reaches zero.
signal destroyed
## Reference to the engine module
@onready var engine: ShipEngine = $Engine
## Reference to the hull module
@onready var hull: Hull = $HullHolder/Hull
## Reference to the shield module
@onready var shield: Shield = $Shield
## Reference to weapons node
@onready var weapons: Node2D = $Weapons
## Faction which this ship belongs to
var faction : Game.Faction = Game.Faction.Player
func _ready() -> void:
hull.global_position += global_position
destroyed.connect(destroy)
## Reset all required variables
func destroy() -> void:
hull.hp = hull.max_hp
shield.capacity = shield.max_capacity
## Swaps old hull with the new one
func change_hull(new_hull_id: String) -> void:
var new_hull: Hull = Game.get_module(new_hull_id, 'hull').instantiate()
var old_hull: Hull = hull
add_child(new_hull)
hull = new_hull
hull.hp = old_hull.hp
get_tree().create_timer(0.05).timeout.connect(old_hull.queue_free)
## Swaps old engine with the new one
func change_engine(new_engine_id: String) -> void:
var new_engine: ShipEngine = Game.get_module(new_engine_id, 'engine').instantiate()
var old_engine: ShipEngine = engine
add_child(new_engine)
engine = new_engine
get_tree().create_timer(0.05).timeout.connect(old_engine.queue_free)
## Swaps old shield with the new one
func change_shield(new_shield_id: String) -> void:
var new_shield: Shield = Game.get_module(new_shield_id, 'shield').instantiate()
var old_shield: Shield = shield
add_child(new_shield)
shield = new_shield
get_tree().create_timer(0.05).timeout.connect(old_shield.queue_free)