Godot Version
4.4
Question
Hello!
I am completely new to making video games and trying to make a top down survival shooter. My problem is that I can’t seem to figure out logic for shotgun reloading and hoping someone can give me a hint.
The way how I’m trying to do this now:
- check how many bullets missing from current mag
- create a timer that will not allow to shoot while animation for reloading is playing
That works fine if I let full reload to complete, but I want to have an ability to interrupt reload in between each shell and allow to resume shooting with proper timer + animation and amount of shell in the magazine.
Thanks in advanced!
@onready var body: AnimatedSprite2D = $body
@onready var _ammo: int = 32 # total ammo available
@onready var _magazine_capacity: int = 8 # total ammo in a magazine
var _current_magazine: int = 0 # ammo in current mag
var _have_ammo: bool = false # does the player have ammo?
var _not_reloading: bool = true # waiting for reloading timer?
@onready var reload_delay: Timer = $reload_delay # timer that tracks reload of 1 bullet
func _ready():
body.play("idle")
_have_ammo = true
_current_magazine = _magazine_capacity
func _process(_delta):
if Input.is_action_just_pressed("shoot"):
print("somehow interrupt animation")
func shoot():
if is_ready_to_fire:
if _not_reloading:
if _have_ammo: # only shoot if we have ammo
_current_magazine -= 1 # take a bullet out of the magazine
print("Bang! Ammo left: ", _current_magazine)
if local_raycast.is_colliding() and local_raycast.get_collider().has_method("kill"):
local_raycast.get_collider().kill()
if _current_magazine < 1: # if we've used the last bullet, reload.
reload()
else:
print("out of ammo")
else:
print("reloading")
func reload() -> void:
var missing_ammo = 8 - _current_magazine
for bullet in missing_ammo:
if _ammo > 0:
reload_delay.set_wait_time(missing_ammo * 1.1)
reload_delay.start()
print("Ammo in clip:", _current_magazine)
_have_ammo = true # we still have ammo
_ammo -= 1 # but we have 1 less magazine
_current_magazine += 1
print("Reload - Ammo total left ", _ammo)
print("Reload - Current mag ", _current_magazine)
body.play("reload")
_not_reloading = false
else:
_have_ammo = false # no more ammo
func _on_reload_delay_timeout() -> void:
_not_reloading = true
body.play("idle")