Rewriting ships: Added weapon base and changed hull parent to Node

This commit is contained in:
2ndbeam 2024-04-29 12:08:13 +03:00
commit 6957169ba5
13 changed files with 121 additions and 37 deletions

51
scripts/Ship/weapon.gd Normal file
View file

@ -0,0 +1,51 @@
extends Node2D
class_name Weapon
## Weapon ID to be used with Game.get_module()
@export var id: String = "weapon"
## Variable to use with input controller to determine which weapon to shoot
@export var action_id: String = "null"
## Projectile scene which will be created on shoot()
@export var projectile: PackedScene
## Applies additional angle (in degrees) while shooting
@export var spread: float = 0
## Ammo type that will be consumed
@export var ammo_type: String = "n/a"
## How much ammo will be consumet each shoot() call
@export var ammo_consumption: float = 0
## Timer to make cooldown between shots
@export var shoot_timer: Timer
## Points where projectiles will spawn
@export var spawner_points: Array[Node2D]
## Shortcut to get_parent().get_parent()
@onready var ship: Ship = get_parent().get_parent()
## Spread, converted to radians
var deviation: float = deg_to_rad(spread)
## Input variable to determine will the ship shoot
var shoot_request: bool = false
## Emits when shoot() is called
signal weapon_shooted
func _ready() -> void:
randomize()
func _process(_delta) -> void:
var can_shoot = ship.hull.ammunition[ammo_type] >= ammo_consumption and shoot_timer.is_stopped()
if can_shoot and shoot_request:
shoot()
ship.hull.add_ammunition(ammo_type, -ammo_consumption)
shoot_timer.start()
if ammo_type == "Laser Energy":
ship.shield.laser_recharge_timer.start()
func shoot() -> void:
for spawner in spawner_points:
var projectile_instance = projectile.instantiate()
ProjectileContainer.instance.add_child(projectile_instance)
projectile_instance.global_position = spawner.global_position
projectile_instance.global_rotation = spawner.global_rotation + randf_range(-deviation/2, deviation/2)
projectile_instance.faction = ship.faction
projectile_instance.modulate = ship.modulate
weapon_shooted.emit()