cosmic/scripts/objects/Shield.gd
2023-11-05 21:51:47 +03:00

44 lines
1.2 KiB
GDScript

extends Node2D
class_name Shield
@export var max_capacity : int = 8
@export var shield_charge_rate : float = 1
@export var recharge_timer : Timer
@export var laser_timer : Timer
@export var laser_charge_rate : float = 20
@onready var ship = get_parent()
@onready var capacity : float = max_capacity
var can_recharge : bool = false
var laser_recharge : bool = true
func _ready():
recharge_timer.timeout.connect(recharging_timer_out)
laser_timer.timeout.connect(laser_timer_out)
func deal_damage(damage : float):
capacity -= damage
if capacity < 0:
ship.hull.hp += capacity
capacity = 0
can_recharge = false
recharge_timer.start()
laser_timer.start()
func recharging_timer_out():
can_recharge = true
func _physics_process(delta):
if can_recharge:
capacity += shield_charge_rate * delta
if capacity > max_capacity:
capacity = max_capacity
can_recharge = false
if laser_recharge:
ship.hull.ammunition["Laser Energy"] += laser_charge_rate * delta
if ship.hull.ammunition["Laser Energy"] > ship.hull.max_ammunition["Laser Energy"]:
ship.hull.ammunition["Laser Energy"] = ship.hull.max_ammunition["Laser Energy"]
laser_recharge = false
func laser_timer_out():
laser_recharge = true