auto mode and burst mode refactor

This commit is contained in:
Rendo 2025-07-14 21:20:02 +05:00
commit 63e8211b6c
3 changed files with 59 additions and 36 deletions

View file

@ -0,0 +1,49 @@
extends BaseFireMode
## Firemode that fires automatically
class_name AutoFireMode
## Delay between shots [br]
@export var fire_delay : float
## Is fire mode on delay?
var cooldown : bool = false
## Reference to cooldown scene timer
var cooldown_timer : SceneTreeTimer = null
func _on_fire_begin(_tree : SceneTree) -> void:
cooldown = false
check_unfinished_timer(cooldown_timer,on_cooldown_timeout)
func _process(tree : SceneTree) -> void:
if can_fire() :
return
if _fire():
on_fire(tree)
## Invoked when cooldown has ended
func on_cooldown_timeout() -> void:
cooldown = false
cooldown_timer = null
## Checks if timer is unfinished and unbounds to avoid errors
func check_unfinished_timer(timer : SceneTreeTimer, bounded_callable : Callable) -> void:
if timer == null:
return
timer.timeout.disconnect(bounded_callable)
## Checks if can fire
func can_fire() -> bool:
return cooldown
## Invoked when fired
func on_fire(tree : SceneTree) -> void:
cooldown = true
cooldown_timer = tree.create_timer(fire_delay)
cooldown_timer.timeout.connect(on_cooldown_timeout)

View file

@ -0,0 +1 @@
uid://dsvgbyeerw1ld

View file

@ -1,4 +1,4 @@
extends BaseFireMode extends AutoFireMode
## Fire mode that fires in bursts ## Fire mode that fires in bursts
class_name BurstFireMode class_name BurstFireMode
@ -10,9 +10,6 @@ class_name BurstFireMode
## Time to reload a burst ## Time to reload a burst
@export var reload_time : float @export var reload_time : float
## Delay between shots [br]
@export var fire_delay : float
## Amount of bullets currently [br] ## Amount of bullets currently [br]
## Resets in _on_fire_end ## Resets in _on_fire_end
var current_amount : int = 0 var current_amount : int = 0
@ -20,34 +17,23 @@ var current_amount : int = 0
## Is burst currently reloading ## Is burst currently reloading
var reloading : bool = false var reloading : bool = false
## Is fire mode on delay?
var cooldown : bool = false
## Reference to reload scene timer ## Reference to reload scene timer
var reload_timer : SceneTreeTimer = null var reload_timer : SceneTreeTimer = null
## Reference to cooldown scene timer
var cooldown_timer : SceneTreeTimer = null
func _on_fire_begin(_tree : SceneTree) -> void: func _on_fire_begin(_tree : SceneTree) -> void:
super._on_fire_begin(_tree)
current_amount = 0 current_amount = 0
reloading = false reloading = false
cooldown = false
check_unfinished_timer(reload_timer,on_reload_timeout) check_unfinished_timer(reload_timer,on_reload_timeout)
check_unfinished_timer(cooldown_timer,on_cooldown_timeout)
func _process(_tree : SceneTree) -> void: func on_fire(tree : SceneTree) -> void:
if reloading or cooldown: super.on_fire(tree)
return current_amount += 1
if _fire(): if current_amount >= burst_amount:
current_amount += 1 reloading = true
cooldown_timer = _tree.create_timer(fire_delay) reload_timer = tree.create_timer(reload_time)
cooldown_timer.timeout.connect(on_cooldown_timeout) reload_timer.timeout.connect(on_reload_timeout)
if current_amount >= burst_amount:
reloading = true
reload_timer = _tree.create_timer(reload_time)
reload_timer.timeout.connect(on_reload_timeout)
## Invoked when reload has ended ## Invoked when reload has ended
@ -55,16 +41,3 @@ func on_reload_timeout() -> void:
reloading = false reloading = false
current_amount = 0 current_amount = 0
reload_timer = null reload_timer = null
## Invoked when cooldown has ended
func on_cooldown_timeout() -> void:
cooldown = false
cooldown_timer = null
## Checks if timer is unfinished and unbounds to avoid errors
func check_unfinished_timer(timer : SceneTreeTimer, bounded_callable : Callable) -> void:
if timer == null:
return
timer.timeout.disconnect(bounded_callable)