cosmic/scripts/Ship/hull.gd

60 lines
1.8 KiB
GDScript

extends RigidBody2D
class_name Hull
## Shortcut to get_parent().get_parent()
@onready var ship: Ship = get_parent().get_parent()
## Hull ID to use with the Game.get_module func
@export var id: String = "hull"
## Max HP of the hull
@export var max_hp: float = 30.0
## Maximum amount of ammunition to be carried
@export var max_ammunition: Dictionary = {
"n/a": 0, # don't change this k thx
"Laser Energy": 100.0,
"Rockets": 10,
}
## How much speed should ship have before collision to take damage
@export var velocity_collision_treshold: float = 200.0
## How much damage should ship take when its velocity equals to treshold
@export var collision_damage: float = 20.0
## Current ammunition. Change this with set_ammunition
@onready var ammunition: Dictionary = max_ammunition.duplicate()
## Current HP of the hull. If it reaches zero, it emits parent's destroyed signal
@onready var hp: float = max_hp:
set(new_hp):
if new_hp > 0:
hp = new_hp
else:
ship.destroyed.emit()
## Length of linear_velocity vector
var scalar_velocity: float = 0.0
func _ready():
body_entered.connect(_on_body_entered)
## Adds amount to ammunition, returns true if it was successful
func add_ammunition(which: String, value: float) -> bool:
if ammunition[which] + value < 0:
return false
ammunition[which] = snapped(ammunition[which] + value, 0.01)
ammunition[which] = min(ammunition[which], max_ammunition[which])
return true
## Update ship's position and rotation
func _physics_process(_delta):
ship.position = position
ship.rotation = rotation
scalar_velocity = linear_velocity.length()
func _on_body_entered(_body):
if scalar_velocity >= velocity_collision_treshold:
ship.shield.deal_damage(20.0 * (scalar_velocity / velocity_collision_treshold))
func warp_to_position(location: Vector2):
position = location