49 lines
1.5 KiB
GDScript
49 lines
1.5 KiB
GDScript
extends Node2D
|
|
|
|
class_name Weapon
|
|
|
|
@export var projectile : PackedScene
|
|
@export var spread : float = 0
|
|
@export var ammo_type : String = "n/a"
|
|
@export var ammo_consumption : float = 0
|
|
@export var shoot_timer : Timer
|
|
@export var shoot_action : String = ""
|
|
@export var spawner_points : Array[Node2D]
|
|
@export var id : String = "SingleLaserMk1"
|
|
@onready var ship = $"../.."
|
|
@onready var slot = $".."
|
|
|
|
var deviation : float = deg_to_rad(spread)
|
|
|
|
signal weapon_shooted
|
|
|
|
func _ready():
|
|
randomize()
|
|
if ship is MainShip:
|
|
match slot.name:
|
|
"PrimaryWeapon":
|
|
shoot_action = "shootprimary"
|
|
"SecondaryWeapon":
|
|
shoot_action = "shootsecondary"
|
|
weapon_shooted.connect(ship.quest._restriction_no_weapon)
|
|
elif ship is NPCShip:
|
|
shoot_action = "npc"
|
|
|
|
func _process(_delta):
|
|
var can_shoot = ship.hull.ammunition[ammo_type] >= ammo_consumption and shoot_timer.is_stopped() and ship.allow_shooting
|
|
if can_shoot and (Input.get_action_strength(shoot_action) and shoot_action != "npc" or shoot_action == "npc" and ship.shooting):
|
|
shoot()
|
|
ship.hull.ammunition[ammo_type] -= ammo_consumption
|
|
shoot_timer.start()
|
|
if ammo_type == "Laser Energy":
|
|
ship.shield.laser_timer.start()
|
|
|
|
func shoot():
|
|
for spawner in spawner_points:
|
|
var proj_inst = projectile.instantiate()
|
|
ProjectileContainer.instance.add_child(proj_inst)
|
|
proj_inst.global_position = spawner.global_position
|
|
proj_inst.global_rotation = spawner.global_rotation + randf_range(-deviation/2, deviation/2)
|
|
proj_inst.faction = ship.faction
|
|
proj_inst.modulate = ship.modulate
|
|
weapon_shooted.emit()
|