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

View file

@ -20,7 +20,7 @@ var acceleration_axis: float = 0.0
## Rotation control variable
var rotation_axis: float = 0.0
func _physics_process(_delta):
func _physics_process(_delta) -> void:
# apply movement and rotation
ship.hull.apply_central_force(Vector2.from_angle(ship.hull.rotation) * acceleration_speed * acceleration_axis)
ship.hull.apply_torque(rotation_speed * rotation_axis)

View file

@ -14,14 +14,25 @@ class_name Hull
## Maximum amount of ammunition to be carried
@export var max_ammunition: Dictionary = {
"n/a": 0, # don't change this k thx
"Laser Energy": 100,
"Laser Energy": 100.0,
"Rockets": 10,
}
## Current ammunition. Change this with set_ammunition
@onready var ammunition: Dictionary = max_ammunition
## Current HP of the hull. If it reaches zero, it emits parent's destroyed signal
var hp: float = max_hp:
@onready var hp: float = max_hp:
set(new_hp):
if new_hp > 0:
hp = new_hp
else:
ship.destroyed.emit()
## 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] += value;
if ammunition[which] > max_ammunition[which]:
ammunition[which] = max_ammunition[which]
return true

View file

@ -3,6 +3,6 @@ extends Node2D
## Shortcut to get_parent()
@onready var ship: Ship = get_parent()
func _physics_process(_delta):
func _physics_process(_delta) -> void:
ship.engine.acceleration_axis = Input.get_axis("deccelerate", "accelerate")
ship.engine.rotation_axis = Input.get_axis("rotateleft", "rotateright")

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)

View file

@ -13,25 +13,22 @@ signal destroyed
@onready var hull: Hull = $Hull
## Reference to the shield module
@onready var shield: Shield = $Shield
## Reference to primary weapon
#@onready var primary_weapon: Weapon = $PrimaryWeapon
## Reference to secondary weapon
#@onready var secondary_weapon: Weapon = $SecondaryWeapon
## Reference to weapons node
@onready var weapons: Node2D = $Weapons
## Faction which this ship belongs to
var faction : Game.Faction = Game.Faction.Player
func _ready():
#shield.material = material
func _ready() -> void:
destroyed.connect(destroy)
## Reset all required variables
func destroy():
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):
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)
@ -40,7 +37,7 @@ func change_hull(new_hull_id: String):
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):
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)
@ -48,7 +45,7 @@ func change_engine(new_engine_id: String):
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):
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)

25
scripts/Ship/weapons.gd Normal file
View file

@ -0,0 +1,25 @@
extends Node2D
# TODO: implement add_weapon
## Shortcut to get_parent()
@onready var ship = get_parent()
## List of weapons
var list: Array[Weapon] = get_children() as Array[Weapon]
## Updates list with actual children of this node
func update_weapon_list() -> void:
list = get_children() as Array[Weapon]
## Removes weapon with given ID and returns true if successful
func remove_weapon(id: String) -> bool:
for weapon in list:
if weapon.id == id:
list.erase(weapon)
weapon.free()
return true
return false
## Adds weapon with given ID to list
func add_weapon(id: String) -> void:
pass