cosmic/scripts/Ship/weapon.gd
2024-05-31 11:23:15 +03:00

56 lines
2 KiB
GDScript

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()
ship.shield.can_recharge_laser = false
func shoot() -> void:
if ship is PlayerShip:
if ship.quest != null:
if Quest.Restriction.NoWeapon in ship.quest.restrictions:
ship.quest.fail()
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()