Burst now supports fire delay and has validation check
This commit is contained in:
parent
db6544b6eb
commit
64127797fb
1 changed files with 35 additions and 11 deletions
|
@ -8,39 +8,63 @@ class_name BurstFireMode
|
||||||
@export_range(1,25,1,"or_greater") var burst_amount : int = 1
|
@export_range(1,25,1,"or_greater") var burst_amount : int = 1
|
||||||
|
|
||||||
## Time to reload a burst
|
## Time to reload a burst
|
||||||
@export var burst_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
|
||||||
|
|
||||||
## Controls burst internal reload
|
## Is burst currently reloading
|
||||||
var reloading : bool = false
|
var reloading : bool = false
|
||||||
|
|
||||||
## Reference to scene timer
|
## Is fire mode on delay?
|
||||||
var timer : SceneTreeTimer = null
|
var cooldown : bool = false
|
||||||
|
|
||||||
|
## Reference to reload scene timer
|
||||||
|
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:
|
||||||
current_amount = 0
|
current_amount = 0
|
||||||
if timer != null:
|
reloading = false
|
||||||
timer.timeout.disconnect(on_reload_timeout)
|
cooldown = false
|
||||||
timer = null
|
check_unfinished_timer(reload_timer,on_reload_timeout)
|
||||||
|
check_unfinished_timer(cooldown_timer,on_cooldown_timeout)
|
||||||
|
|
||||||
|
|
||||||
func _process(_tree : SceneTree) -> void:
|
func _process(_tree : SceneTree) -> void:
|
||||||
if reloading:
|
if reloading or cooldown:
|
||||||
return
|
return
|
||||||
if _fire():
|
if _fire():
|
||||||
current_amount += 1
|
current_amount += 1
|
||||||
|
cooldown_timer = _tree.create_timer(fire_delay)
|
||||||
|
cooldown_timer.timeout.connect(on_cooldown_timeout)
|
||||||
if current_amount >= burst_amount:
|
if current_amount >= burst_amount:
|
||||||
reloading = true
|
reloading = true
|
||||||
timer = _tree.create_timer(burst_time)
|
reload_timer = _tree.create_timer(reload_time)
|
||||||
timer.timeout.connect(on_reload_timeout)
|
reload_timer.timeout.connect(on_reload_timeout)
|
||||||
|
|
||||||
|
|
||||||
## Invoked when reload has ended
|
## Invoked when reload has ended
|
||||||
func on_reload_timeout() -> void:
|
func on_reload_timeout() -> void:
|
||||||
reloading = false
|
reloading = false
|
||||||
current_amount = 0
|
current_amount = 0
|
||||||
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)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue