146 lines
5.2 KiB
GDScript
146 lines
5.2 KiB
GDScript
extends CanvasLayer
|
|
|
|
const GCD_TIME := 0.5
|
|
|
|
@onready var health_bar: ProgressBar = $HealthBar
|
|
@onready var health_label: Label = $HealthBar/HealthLabel
|
|
@onready var shield_bar: ProgressBar = $ShieldBar
|
|
@onready var shield_label: Label = $ShieldBar/ShieldLabel
|
|
@onready var respawn_label: Label = $RespawnTimer
|
|
@onready var class_icon: Label = $AbilityBar/ClassIcon/Label
|
|
@onready var ability_panels: Array = [
|
|
$AbilityBar/Ability1,
|
|
$AbilityBar/Ability2,
|
|
$AbilityBar/Ability3,
|
|
$AbilityBar/Ability4,
|
|
$AbilityBar/Ability5,
|
|
]
|
|
|
|
var ability_labels: Array[String] = ["1", "2", "3", "4", "P"]
|
|
var effect_container: HBoxContainer = null
|
|
|
|
func _ready() -> void:
|
|
respawn_label.visible = false
|
|
_create_effect_container()
|
|
EventBus.health_changed.connect(_on_health_changed)
|
|
EventBus.shield_changed.connect(_on_shield_changed)
|
|
EventBus.entity_died.connect(_on_entity_died)
|
|
EventBus.player_respawned.connect(_on_player_respawned)
|
|
EventBus.role_changed.connect(_on_role_changed)
|
|
EventBus.respawn_tick.connect(_on_respawn_tick)
|
|
EventBus.cooldown_tick.connect(_on_cooldown_tick)
|
|
EventBus.effect_applied.connect(_on_effect_applied)
|
|
EventBus.effect_expired.connect(_on_effect_expired)
|
|
|
|
func _on_health_changed(entity: Node, current: float, max_val: float) -> void:
|
|
if entity.name == "Player":
|
|
health_bar.max_value = max_val
|
|
health_bar.value = current
|
|
health_label.text = "%d/%d" % [current, max_val]
|
|
|
|
func _on_shield_changed(entity: Node, current: float, max_val: float) -> void:
|
|
if entity.name == "Player":
|
|
shield_bar.max_value = max_val
|
|
shield_bar.value = current
|
|
shield_label.text = "%d/%d" % [current, max_val]
|
|
|
|
func _on_entity_died(entity: Node) -> void:
|
|
if entity.name == "Player":
|
|
respawn_label.visible = true
|
|
|
|
func _on_player_respawned(_player: Node) -> void:
|
|
respawn_label.visible = false
|
|
|
|
func _on_respawn_tick(timer: float) -> void:
|
|
respawn_label.text = str(ceil(timer))
|
|
|
|
func _on_role_changed(_player: Node, role_type: int) -> void:
|
|
match role_type:
|
|
0: class_icon.text = "T"
|
|
1: class_icon.text = "D"
|
|
2: class_icon.text = "H"
|
|
|
|
func _on_cooldown_tick(cooldowns: Array, max_cooldowns: Array, gcd_timer: float) -> void:
|
|
for i in range(min(ability_panels.size(), cooldowns.size())):
|
|
var panel: Panel = ability_panels[i]
|
|
var label: Label = panel.get_node("Label")
|
|
var overlay: ColorRect = panel.get_node("CooldownOverlay")
|
|
var cd: float = cooldowns[i]
|
|
var gcd: float = gcd_timer if i != 2 and i != 4 else 0.0
|
|
var active_cd: float = max(cd, gcd)
|
|
var max_cd: float = max_cooldowns[i] if max_cooldowns[i] > 0 else GCD_TIME
|
|
|
|
if active_cd > 0:
|
|
var ratio: float = clamp(active_cd / max_cd, 0.0, 1.0)
|
|
overlay.visible = true
|
|
overlay.anchor_bottom = ratio
|
|
label.text = str(ceil(active_cd))
|
|
else:
|
|
overlay.visible = false
|
|
label.text = ability_labels[i]
|
|
|
|
func _create_effect_container() -> void:
|
|
effect_container = HBoxContainer.new()
|
|
effect_container.name = "EffectContainer"
|
|
effect_container.position = Vector2(10, 60)
|
|
effect_container.add_theme_constant_override("separation", 3)
|
|
add_child(effect_container)
|
|
|
|
func _on_effect_applied(target: Node, effect: Effect) -> void:
|
|
if target.name != "Player":
|
|
return
|
|
var panel := _create_icon_panel(effect)
|
|
var insert_idx: int = _get_sorted_index(effect.type)
|
|
effect_container.add_child(panel)
|
|
effect_container.move_child(panel, insert_idx)
|
|
|
|
func _on_effect_expired(target: Node, effect: Effect) -> void:
|
|
if target.name != "Player":
|
|
return
|
|
for child in effect_container.get_children():
|
|
if child.has_meta("effect_type") and child.has_meta("effect_name"):
|
|
if child.get_meta("effect_type") == effect.type and child.get_meta("effect_name") == effect.effect_name:
|
|
child.queue_free()
|
|
return
|
|
|
|
func _get_sorted_index(type: int) -> int:
|
|
var idx := 0
|
|
for child in effect_container.get_children():
|
|
if not child.has_meta("effect_type"):
|
|
continue
|
|
var child_type: int = child.get_meta("effect_type")
|
|
if child_type <= type:
|
|
idx += 1
|
|
else:
|
|
break
|
|
return idx
|
|
|
|
func _create_icon_panel(effect: Effect) -> PanelContainer:
|
|
var panel := PanelContainer.new()
|
|
var style := StyleBoxFlat.new()
|
|
match effect.type:
|
|
Effect.Type.AURA:
|
|
style.bg_color = Color(0.15, 0.15, 0.3, 1)
|
|
style.border_color = Color(0.3, 0.5, 1.0, 1)
|
|
Effect.Type.BUFF:
|
|
style.bg_color = Color(0.15, 0.3, 0.15, 1)
|
|
style.border_color = Color(0.3, 1.0, 0.3, 1)
|
|
Effect.Type.DEBUFF:
|
|
style.bg_color = Color(0.3, 0.15, 0.15, 1)
|
|
style.border_color = Color(1.0, 0.3, 0.3, 1)
|
|
style.set_border_width_all(2)
|
|
style.set_content_margin_all(2)
|
|
panel.add_theme_stylebox_override("panel", style)
|
|
var label := Label.new()
|
|
label.text = effect.effect_name.left(1)
|
|
label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
label.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
|
|
label.add_theme_font_size_override("font_size", 14)
|
|
label.add_theme_color_override("font_color", Color.WHITE)
|
|
label.custom_minimum_size = Vector2(20, 20)
|
|
panel.add_child(label)
|
|
panel.custom_minimum_size = Vector2(24, 24)
|
|
panel.set_meta("effect_type", effect.type)
|
|
panel.set_meta("effect_name", effect.effect_name)
|
|
return panel
|