cosmic/scripts/objects/weapon.gd
2023-11-05 21:51:47 +03:00

45 lines
1.4 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]
@onready var ship = $"../.."
@onready var slot = $".."
var deviation : float = deg_to_rad(spread)
func _ready():
randomize()
if ship is MainShip:
match slot.name:
"PrimaryWeapon":
shoot_action = "shootprimary"
"SecondaryWeapon":
shoot_action = "shootsecondary"
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