Chelimbalo/scripts/weapon_system/gun.gd
2025-11-25 23:18:50 +05:00

93 lines
2.4 KiB
GDScript

extends Usable
@export var max_ammo: int
@export var semi_auto: bool
@export var emptyable: bool
@export var damage: int
@export var firerate: float
@export var prefix: String
@export var raycast: RayCast3D
var ammo_amount: int
var fire_timer: Timer
var state_locked: bool
func _ready() -> void:
fire_timer = Timer.new()
fire_timer.wait_time = firerate
ammo_amount = max_ammo
add_child(fire_timer)
if not semi_auto:
fire_timer.timeout.connect(fire)
@rpc("authority","call_local","reliable")
func use_begin() -> void:
if fire_timer.is_stopped() == false:
return
fire_timer.start()
fire_timer.one_shot = true if semi_auto else false
fire()
@rpc("authority","call_local","reliable")
func use_end() -> void:
fire_timer.one_shot = true
func fire() -> void:
if ammo_amount == 0 or state_locked:
if not semi_auto:
fire_timer.stop()
return
ammo_amount -= 1
system.animation_player.stop()
system.animation_player.play(prefix + with_empty_suffix("_shoot"))
system.animation_player.queue(prefix + with_empty_suffix("_idle"))
if raycast.is_colliding():
raycast.get_collider().hp -= damage
func alternate_state() -> void:
pass
func switch_mode() -> void:
pass
func enter() -> void:
system.animation_player.animation_changed.connect(on_animation_changed)
state_locked = true
system.animation_player.stop()
system.animation_player.play(prefix+with_empty_suffix("_intro"))
system.animation_player.queue(prefix+with_empty_suffix("_idle"))
func exit() -> void:
system.animation_player.animation_changed.disconnect(on_animation_changed)
func on_animation_changed(old_animation: StringName,_new_animation: StringName) -> void:
if old_animation == prefix + with_empty_suffix("_reload"):
state_locked = false
ammo_amount = max_ammo
if old_animation == prefix + with_empty_suffix("_intro"):
state_locked = false
func _input(event: InputEvent) -> void:
if not system.is_multiplayer_authority() or not in_use: return
if event.is_action_pressed("plr_reload"):
init_reload.rpc()
@rpc("call_local","authority","reliable")
func init_reload():
if ammo_amount == max_ammo or state_locked:
return
state_locked = true
system.animation_player.play(prefix + with_empty_suffix("_reload"))
system.animation_player.queue(prefix + "_idle")
ammo_amount = max_ammo
func with_empty_suffix(animation):
return (animation+"_empty") if emptyable and ammo_amount == 0 else animation