ChronoSpace/scenes/spaceShip/spaceShip.gd
2025-08-02 12:27:49 +02:00

95 lines
2.2 KiB
GDScript

class_name Spaceship extends CharacterBody2D
@onready var hurtArea = $hurtArea
@onready var sprite = $Sprite2D
@onready var weaponNode = $weaponNode
@onready var invincibleTimer = $invincibleTimer
@export var minHealth: int = 0
@export var maxHealth: int = 100
@export var health: int = maxHealth
@export var speed: int = 800
@export var acceleration: float = 0.8
@export var pushback: int = 500
@export var timeGhost: TimeGhost = null
@export var weapon: Weapon = null
@export var invincible: bool = false
signal destroyed
func _ready() -> void:
invincibleTimer.wait_time = Globals.currentDelay * 3
func damage(amount: int):
if not invincible:
if health - amount <= minHealth:
if timeGhost == null:
emit_signal("destroyed")
destroy()
health = minHealth
shortInvincibility()
else:
health -= amount
func heal(amount: int):
if health + amount >= maxHealth:
health = maxHealth
else:
health += amount
func destroy():
if timeGhost == null:
queue_free()
else:
timeGhost.destroy()
func attack():
if weapon != null:
weapon.attack()
await get_tree().create_timer(Globals.currentDelay).timeout
if timeGhost != null:
timeGhost.attack()
func moveTimeGhost(_position: Vector2, _rotation: float):
await get_tree().create_timer(Globals.currentDelay).timeout
if timeGhost != null:
timeGhost.global_position = _position
timeGhost.rotation = _rotation
timeGhost.moveTimeGhost(_position, _rotation)
func changeWeapon(_weapon: PackedScene):
var newWeapon = _weapon.instantiate()
for i in weaponNode.get_children():
i.queue_free()
weaponNode.add_child(newWeapon)
weapon = newWeapon
await get_tree().create_timer(Globals.currentDelay).timeout
if timeGhost != null:
timeGhost.changeWeapon(_weapon)
func addTimeGhost(_timeGhost: TimeGhost):
if timeGhost == null:
timeGhost = _timeGhost
timeGhost.setSpaceship(self)
else:
timeGhost.addTimeGhost(_timeGhost)
func shortInvincibility():
if invincibleTimer.is_stopped():
invincible = true
invincibleTimer.start()
func _on_invincible_timer_timeout() -> void:
invincible = false
func _on_hurt_area_area_entered(area: Area2D) -> void:
if area.get_parent().is_in_group("obstacle"):
velocity = Vector2.UP.rotated(rotation) * -pushback
area.get_parent().queue_free()