Files
mmo/scripts/player/combat.gd
Marek Le 4fddc74df1 update
2026-03-30 09:03:29 +02:00

85 lines
2.6 KiB
GDScript

extends Node
const GCD_TIME := 0.5
const AA_DAMAGE := 10.0
const AA_COOLDOWN := 1.0
const AA_RANGE := 20.0
@onready var player: CharacterBody3D = get_parent()
@onready var targeting: Node = get_parent().get_node("Targeting")
@onready var role: Node = get_parent().get_node("Role")
var abilities: Array = []
var cooldowns: Array[float] = [0.0, 0.0, 0.0, 0.0, 0.0]
var max_cooldowns: Array[float] = [0.0, 0.0, 0.0, 0.0, 0.0]
var gcd_timer := 0.0
var aa_timer := 0.0
func _ready() -> void:
_load_abilities()
EventBus.role_changed.connect(_on_role_changed)
func _process(delta: float) -> void:
if gcd_timer > 0:
gcd_timer -= delta
for i in range(cooldowns.size()):
if cooldowns[i] > 0:
cooldowns[i] -= delta
EventBus.cooldown_tick.emit(cooldowns, max_cooldowns, gcd_timer)
_auto_attack(delta)
func _auto_attack(delta: float) -> void:
aa_timer -= delta
if aa_timer > 0:
return
if not targeting.in_combat or not targeting.current_target:
return
if not is_instance_valid(targeting.current_target):
return
var dist := player.global_position.distance_to(targeting.current_target.global_position)
if dist > AA_RANGE:
return
var dmg := apply_passive(AA_DAMAGE)
EventBus.damage_requested.emit(player, targeting.current_target, dmg)
print("AA: %s Schaden an %s" % [dmg, targeting.current_target.name])
aa_timer = AA_COOLDOWN
func _load_abilities() -> void:
var ability_set: AbilitySet = role.get_ability_set()
if ability_set:
abilities = ability_set.abilities
else:
abilities = []
cooldowns = [0.0, 0.0, 0.0, 0.0, 0.0]
max_cooldowns = [0.0, 0.0, 0.0, 0.0, 0.0]
gcd_timer = 0.0
func _unhandled_input(event: InputEvent) -> void:
for i in range(min(abilities.size(), 5)):
if event.is_action_pressed("ability_%s" % (i + 1)) and abilities[i]:
if abilities[i].type == Ability.Type.PASSIVE:
return
if cooldowns[i] > 0:
return
if abilities[i].uses_gcd and gcd_timer > 0:
return
var success: bool = abilities[i].execute(player, targeting)
if not success:
return
var ability_cd: float = abilities[i].cooldown
var gcd_cd: float = GCD_TIME if abilities[i].uses_gcd else 0.0
cooldowns[i] = ability_cd
max_cooldowns[i] = max(ability_cd, gcd_cd)
if abilities[i].uses_gcd:
gcd_timer = GCD_TIME
return
func apply_passive(base_damage: float) -> float:
for ability in abilities:
if ability and ability.type == Ability.Type.PASSIVE:
return base_damage * (1.0 + ability.damage / 100.0)
return base_damage
func _on_role_changed(_player: Node, _role_type: int) -> void:
_load_abilities()