Rewriting ships: Added shields and weapons holder

This commit is contained in:
2ndbeam 2024-04-28 20:53:44 +03:00
commit 95274d0a5b
17 changed files with 396 additions and 24 deletions

56
scripts/Ship/shield.gd Normal file
View file

@ -0,0 +1,56 @@
extends Node2D
class_name Shield
## Shield ID to be used with Game.get_module()
@export var id: String = "shield"
## Maximum damage shield can take before running out of energy
@export var max_capacity: int = 8
## Shield is charged by this amount every second
@export var shield_charge_rate: float = 1.0
## One-shot timer which holds shield recharge cooldown
@export var shield_recharge_timer: Timer
## Laser energy is charged by this amount every second
@export var laser_charge_rate: float = 20.0
## One-shot timer which hold laser recharge cooldown
@export var laser_recharge_timer: Timer
## Shortcut to get_parent()
@onready var ship = get_parent()
## Current shield capacity
@onready var capacity: float = max_capacity:
set(new_capacity):
capacity = new_capacity
if capacity < 0:
ship.hull.hp += capacity
capacity = 0
if capacity > max_capacity:
capacity = max_capacity
## Indicates if shield will charge
var can_recharge_shield: bool = false
## Indicates if laser will charge
var can_recharge_laser: bool = true
func _ready() -> void:
shield_recharge_timer.timeout.connect(shield_timer_out)
laser_recharge_timer.timeout.connect(laser_timer_out)
## This function deals damage to the shield
func deal_damage(damage: float) -> void:
capacity -= damage
can_recharge_shield = false
shield_recharge_timer.start()
laser_recharge_timer.start()
## Allows shield to charge
func shield_timer_out() -> void:
can_recharge_shield = true
## Allows laser to charge
func laser_timer_out() -> void:
can_recharge_laser = true
func _physics_process(delta) -> void:
if can_recharge_shield:
capacity += shield_charge_rate * delta
if can_recharge_laser:
ship.hull.add_ammunition("Laser Energy", laser_charge_rate * delta)