Armed guys!
This commit is contained in:
@@ -11,7 +11,7 @@ signal hit_connected(target: Node)
|
||||
@export var weapon_data: WeaponData
|
||||
|
||||
# Runtime references
|
||||
var owner_character: Character = null
|
||||
var owner_character: Node = null # Can be Character or ArmedEnemy
|
||||
var _mesh_instance: Node3D = null
|
||||
var _attack_timer: float = 0.0
|
||||
var _hitbox: HitBox = null
|
||||
@@ -57,10 +57,8 @@ func _setup_hitbox():
|
||||
# Create hitbox dynamically based on weapon range
|
||||
_hitbox = HitBox.new()
|
||||
_hitbox.name = "HitBox"
|
||||
add_child(_hitbox)
|
||||
|
||||
# Add collision shape based on attack range - use sphere for consistent detection
|
||||
# regardless of weapon orientation during animations
|
||||
# Add collision shape BEFORE adding hitbox to tree (so _ready can find it)
|
||||
var collision = CollisionShape3D.new()
|
||||
var sphere = SphereShape3D.new()
|
||||
var range_val = weapon_data.attack_range if weapon_data else 1.5
|
||||
@@ -68,6 +66,9 @@ func _setup_hitbox():
|
||||
collision.shape = sphere
|
||||
_hitbox.add_child(collision)
|
||||
|
||||
# Now add the fully configured hitbox to the scene
|
||||
add_child(_hitbox)
|
||||
|
||||
_configure_hitbox()
|
||||
|
||||
## Configure hitbox with weapon stats and owner
|
||||
@@ -102,24 +103,30 @@ func _on_hitbox_hit(target: Node, damage_amount: float, knockback_amount: float,
|
||||
# Route damage through server
|
||||
var attacker_id = multiplayer.get_unique_id()
|
||||
|
||||
if multiplayer.is_server():
|
||||
# We are server, apply directly
|
||||
owner_character._server_apply_damage(
|
||||
target.name,
|
||||
damage_amount,
|
||||
attacker_id,
|
||||
knockback_amount,
|
||||
attacker_pos
|
||||
)
|
||||
# Check if owner has _server_apply_damage (Character has it, ArmedEnemy doesn't)
|
||||
if owner_character.has_method("_server_apply_damage"):
|
||||
if multiplayer.is_server():
|
||||
# We are server, apply directly
|
||||
owner_character._server_apply_damage(
|
||||
target.name,
|
||||
damage_amount,
|
||||
attacker_id,
|
||||
knockback_amount,
|
||||
attacker_pos
|
||||
)
|
||||
else:
|
||||
# Send to server
|
||||
owner_character.rpc_id(1, "_server_apply_damage",
|
||||
target.name,
|
||||
damage_amount,
|
||||
attacker_id,
|
||||
knockback_amount,
|
||||
attacker_pos
|
||||
)
|
||||
else:
|
||||
# Send to server
|
||||
owner_character.rpc_id(1, "_server_apply_damage",
|
||||
target.name,
|
||||
damage_amount,
|
||||
attacker_id,
|
||||
knockback_amount,
|
||||
attacker_pos
|
||||
)
|
||||
# ArmedEnemy or other entity - apply damage directly if we're server
|
||||
if multiplayer.is_server() and target is BaseUnit:
|
||||
target.take_damage(damage_amount, 1, knockback_amount, attacker_pos)
|
||||
|
||||
## Perform an attack with this weapon
|
||||
## Called by the character who owns this weapon
|
||||
@@ -141,20 +148,24 @@ func perform_attack() -> bool:
|
||||
_attack_timer = cooldown
|
||||
_is_attacking = true
|
||||
|
||||
# Notify owner character of attack cooldown (for UI)
|
||||
if owner_character and owner_character.is_multiplayer_authority():
|
||||
# Notify owner character of attack cooldown (for UI) - only for Characters
|
||||
if owner_character.is_multiplayer_authority() and "_attack_timer" in owner_character:
|
||||
owner_character._attack_timer = cooldown
|
||||
|
||||
# Play attack animation on owner (use weapon's animation)
|
||||
if owner_character._body:
|
||||
if "_body" in owner_character and owner_character._body:
|
||||
var anim_name = weapon_data.attack_animation if weapon_data.attack_animation else "Attack_OneHand"
|
||||
owner_character._body.play_attack(anim_name)
|
||||
# Sync animation to other clients
|
||||
owner_character._sync_attack_animation.rpc(anim_name)
|
||||
if owner_character._body.has_method("play_attack"):
|
||||
owner_character._body.play_attack(anim_name)
|
||||
# Sync animation to other clients if method exists
|
||||
if owner_character.has_method("_sync_attack_animation"):
|
||||
owner_character._sync_attack_animation.rpc(anim_name)
|
||||
|
||||
# Activate hitbox for the attack duration
|
||||
# Only activate on authority - they detect hits and send to server
|
||||
if owner_character.is_multiplayer_authority():
|
||||
# For players: only activate on authority
|
||||
# For enemies: they are server-authoritative, so check if we're server
|
||||
var should_activate = owner_character.is_multiplayer_authority() or multiplayer.is_server()
|
||||
if should_activate:
|
||||
_activate_hitbox()
|
||||
|
||||
attack_performed.emit()
|
||||
@@ -197,8 +208,8 @@ func _activate_hitbox():
|
||||
func can_attack() -> bool:
|
||||
return _attack_timer <= 0 and not _is_attacking
|
||||
|
||||
## Set the character who owns this weapon
|
||||
func set_owner_character(character: Character):
|
||||
## Set the character who owns this weapon (can be Character or ArmedEnemy)
|
||||
func set_owner_character(character: Node):
|
||||
owner_character = character
|
||||
# Update hitbox owner
|
||||
if _hitbox:
|
||||
|
||||
Reference in New Issue
Block a user