facsimile-wing/scripts/weapon_component.gd

41 lines
1.2 KiB
GDScript

class_name WeaponComponent extends Node
@export var weapon_data: WeaponShot = null
@export var available_weapons: Array[WeaponShot] = []
var _current_weapon_index: int = 0
func _ready() -> void:
# Set the default weapon to be stock, i.e. index 1
if weapon_data and not available_weapons.is_empty():
for i in range(available_weapons.size()):
if available_weapons[i] == weapon_data:
_current_weapon_index = i
return
func get_bullet_scene() -> PackedScene:
return weapon_data.bullet_scene if weapon_data else null
func get_weapon_resource() -> Resource:
return weapon_data
func select_weapon_by_name(name: String) -> bool:
for i in range(available_weapons.size()):
if available_weapons[i].shot_name == name:
_current_weapon_index = i
weapon_data = available_weapons[i]
print("Switched to: ", name)
return true
return false
func cycle_weapon() -> void: # Used for testing weapon cycling
if available_weapons.is_empty():
return
_current_weapon_index = (_current_weapon_index + 1) % available_weapons.size()
weapon_data = available_weapons[_current_weapon_index]
print("Switched to: ", weapon_data.shot_name)