74 lines
2.0 KiB
GDScript
74 lines
2.0 KiB
GDScript
extends Node
|
|
|
|
var cooldowns: Dictionary = {}
|
|
|
|
func _ready() -> void:
|
|
add_to_group("cooldown_system")
|
|
EventBus.role_changed.connect(_on_role_changed)
|
|
|
|
func register(entity: Node, ability_count: int) -> void:
|
|
cooldowns[entity] = {
|
|
"cds": [] as Array[float],
|
|
"max_cds": [] as Array[float],
|
|
"gcd": 0.0,
|
|
"aa": 0.0,
|
|
}
|
|
cooldowns[entity]["cds"].resize(ability_count)
|
|
cooldowns[entity]["cds"].fill(0.0)
|
|
cooldowns[entity]["max_cds"].resize(ability_count)
|
|
cooldowns[entity]["max_cds"].fill(0.0)
|
|
|
|
func deregister(entity: Node) -> void:
|
|
cooldowns.erase(entity)
|
|
|
|
func _process(delta: float) -> void:
|
|
for entity in cooldowns:
|
|
if not is_instance_valid(entity):
|
|
continue
|
|
var data: Dictionary = cooldowns[entity]
|
|
if data["gcd"] > 0:
|
|
data["gcd"] -= delta
|
|
if data["aa"] > 0:
|
|
data["aa"] -= delta
|
|
var cds: Array = data["cds"]
|
|
for i in range(cds.size()):
|
|
if cds[i] > 0:
|
|
cds[i] -= delta
|
|
EventBus.cooldown_tick.emit(cds, data["max_cds"], data["gcd"])
|
|
|
|
func is_ready(entity: Node, index: int) -> bool:
|
|
if entity not in cooldowns:
|
|
return false
|
|
return cooldowns[entity]["cds"][index] <= 0
|
|
|
|
func is_gcd_ready(entity: Node) -> bool:
|
|
if entity not in cooldowns:
|
|
return false
|
|
return cooldowns[entity]["gcd"] <= 0
|
|
|
|
func is_aa_ready(entity: Node) -> bool:
|
|
if entity not in cooldowns:
|
|
return false
|
|
return cooldowns[entity]["aa"] <= 0
|
|
|
|
func set_cooldown(entity: Node, index: int, cd: float, gcd: float) -> void:
|
|
if entity not in cooldowns:
|
|
return
|
|
var data: Dictionary = cooldowns[entity]
|
|
data["cds"][index] = cd
|
|
data["max_cds"][index] = max(cd, gcd)
|
|
if gcd > 0:
|
|
data["gcd"] = gcd
|
|
|
|
func set_aa_cooldown(entity: Node, cd: float) -> void:
|
|
if entity not in cooldowns:
|
|
return
|
|
cooldowns[entity]["aa"] = cd
|
|
|
|
func _on_role_changed(player: Node, _role_type: int) -> void:
|
|
if player in cooldowns:
|
|
var data: Dictionary = cooldowns[player]
|
|
data["cds"].fill(0.0)
|
|
data["max_cds"].fill(0.0)
|
|
data["gcd"] = 0.0
|