Player changes
This commit is contained in:
@@ -42,6 +42,8 @@ var damage_number_scene: PackedScene = null
|
||||
var projectiles: Dictionary = {} # id -> projectile node
|
||||
|
||||
var aim_angle: float = 0.0
|
||||
var last_attack_time: float = 0.0
|
||||
var attack_cooldown: float = 0.5 # Default attack cooldown in seconds
|
||||
|
||||
func _ready():
|
||||
# Add to group for HUD to access
|
||||
@@ -148,9 +150,7 @@ func _on_dungeon_entered():
|
||||
if camera:
|
||||
camera.position = world_pos
|
||||
|
||||
# Setup player texture
|
||||
if player_node and player_node.has_node("Sprite") and SpriteGenerator:
|
||||
player_node.get_node("Sprite").texture = SpriteGenerator.get_circle()
|
||||
# Player texture is now setup in player.gd using AnimatedSprite2D
|
||||
|
||||
# Request initial inventory
|
||||
if Network:
|
||||
@@ -286,24 +286,79 @@ func _handle_input():
|
||||
_use_hotbar_slot(i - 1)
|
||||
|
||||
func _perform_attack(attack_angle: float):
|
||||
# Check if player has a ranged weapon equipped
|
||||
# Check attack cooldown
|
||||
var current_time = Time.get_ticks_msec() / 1000.0
|
||||
var time_since_last_attack = current_time - last_attack_time
|
||||
|
||||
# Get attack speed from equipped weapon
|
||||
var equipped_weapon = GameState.equipment.get("weapon", null)
|
||||
var weapon_cooldown = attack_cooldown # Default cooldown
|
||||
|
||||
if equipped_weapon != null and not equipped_weapon.is_empty():
|
||||
# Get attack speed stat (attacks per second) if available
|
||||
var attack_speed = equipped_weapon.get("attackSpeed", 0.0)
|
||||
if attack_speed > 0:
|
||||
weapon_cooldown = 1.0 / attack_speed # Convert attacks/sec to seconds/attack
|
||||
else:
|
||||
# Default cooldowns based on weapon type
|
||||
var weapon_type = equipped_weapon.get("type", "")
|
||||
match weapon_type:
|
||||
"Weapon":
|
||||
var weapon_name = equipped_weapon.get("name", "").to_lower()
|
||||
if "dagger" in weapon_name or "fist" in weapon_name:
|
||||
weapon_cooldown = 0.3 # Fast weapons
|
||||
elif "sword" in weapon_name:
|
||||
weapon_cooldown = 0.5 # Medium weapons
|
||||
elif "axe" in weapon_name or "mace" in weapon_name:
|
||||
weapon_cooldown = 0.7 # Slow weapons
|
||||
elif "bow" in weapon_name or "crossbow" in weapon_name:
|
||||
weapon_cooldown = 0.8 # Ranged weapons
|
||||
else:
|
||||
weapon_cooldown = 0.5 # Default
|
||||
|
||||
# Check if enough time has passed since last attack
|
||||
if time_since_last_attack < weapon_cooldown:
|
||||
# Only print every 0.5 seconds to avoid spam
|
||||
if int(time_since_last_attack * 2) != int((time_since_last_attack - get_process_delta_time()) * 2):
|
||||
print("[DungeonManager] Attack on cooldown: %.2f / %.2f seconds" % [time_since_last_attack, weapon_cooldown])
|
||||
return
|
||||
|
||||
# Update last attack time
|
||||
last_attack_time = current_time
|
||||
|
||||
# Determine weapon type for animation
|
||||
var animation_type = "fist"
|
||||
|
||||
# Check if player has a ranged weapon equipped
|
||||
if equipped_weapon != null and not equipped_weapon.is_empty():
|
||||
# Server sends camelCase properties
|
||||
var requires_ammo = equipped_weapon.get("requiresAmmunition", false)
|
||||
var is_throwable = equipped_weapon.get("isThrowable", false)
|
||||
var projectile_speed = equipped_weapon.get("projectileSpeed", 0)
|
||||
var weapon_name = equipped_weapon.get("name", "").to_lower()
|
||||
|
||||
# Determine animation type from weapon
|
||||
if requires_ammo or "bow" in weapon_name or "crossbow" in weapon_name:
|
||||
animation_type = "bow"
|
||||
elif "staff" in weapon_name or "wand" in weapon_name:
|
||||
animation_type = "spell"
|
||||
elif weapon_name != "":
|
||||
animation_type = "sword"
|
||||
|
||||
print("[DungeonManager] Attack - Weapon: ", equipped_weapon.get("name", "Unknown"))
|
||||
print("[DungeonManager] RequiresAmmo: ", requires_ammo, ", IsThrowable: ", is_throwable, ", Speed: ", projectile_speed)
|
||||
|
||||
# If weapon fires projectiles (bow, crossbow, throwable), send projectile fire
|
||||
if (requires_ammo or is_throwable) and projectile_speed > 0:
|
||||
print("[DungeonManager] Firing projectile!")
|
||||
print("[DungeonManager] Firing projectile! Direction: (%.2f, %.2f)" % [cos(attack_angle), sin(attack_angle)])
|
||||
# Calculate direction vector from aim angle
|
||||
var direction_x = cos(attack_angle)
|
||||
var direction_y = sin(attack_angle)
|
||||
GameState.send_fire_projectile(direction_x, direction_y)
|
||||
if GameState and GameState.has_method("send_fire_projectile"):
|
||||
GameState.send_fire_projectile(direction_x, direction_y)
|
||||
print("[DungeonManager] Projectile fire command sent to server")
|
||||
else:
|
||||
print("[DungeonManager] ERROR: GameState.send_fire_projectile not available!")
|
||||
else:
|
||||
print("[DungeonManager] Using melee attack")
|
||||
# Melee weapon - use normal attack
|
||||
@@ -313,19 +368,28 @@ func _perform_attack(attack_angle: float):
|
||||
# No weapon or empty slot - use normal attack (fists)
|
||||
GameState.send_attack(attack_angle)
|
||||
|
||||
# Play attack animation on player (don't await to avoid blocking)
|
||||
if player_node and player_node.has_method("play_attack_animation"):
|
||||
player_node.play_attack_animation(animation_type)
|
||||
print("[DungeonManager] Triggered %s attack animation" % animation_type)
|
||||
|
||||
func _send_movement():
|
||||
# Don't move if UI is open
|
||||
var ui_open = (inventory_panel and inventory_panel.visible) or (chat_panel and chat_panel.visible)
|
||||
if ui_open:
|
||||
return
|
||||
|
||||
|
||||
var dx = Input.get_axis("move_left", "move_right")
|
||||
var dy = Input.get_axis("move_up", "move_down")
|
||||
|
||||
|
||||
var move_vec = Vector2(dx, dy)
|
||||
if move_vec.length() > 1:
|
||||
move_vec = move_vec.normalized()
|
||||
|
||||
|
||||
# Update player animation based on movement
|
||||
if player_node and player_node.has_method("set_movement"):
|
||||
player_node.set_movement(move_vec)
|
||||
|
||||
GameState.send_move(move_vec.x, move_vec.y, aim_angle)
|
||||
|
||||
func _update_camera(delta):
|
||||
|
||||
@@ -0,0 +1,216 @@
|
||||
extends Node2D
|
||||
|
||||
# Player visual representation with animated sprite
|
||||
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite
|
||||
@onready var aim_line: Line2D = $AimLine
|
||||
@onready var name_label: Label = $NameLabel
|
||||
@onready var health_bar: ProgressBar = $HealthBar
|
||||
|
||||
var aim_angle: float = 0.0
|
||||
var move_direction: Vector2 = Vector2.ZERO
|
||||
var is_attacking: bool = false
|
||||
var current_direction: String = "down" # down, up, left, right
|
||||
|
||||
func _ready():
|
||||
# Sprite sheet will be loaded from CDN path: textures/player/Warrior-Blue.png
|
||||
if UpdateManager:
|
||||
var texture = UpdateManager.load_texture("textures/player/Warrior-Blue.png")
|
||||
if texture:
|
||||
print("[Player] Loaded sprite sheet texture")
|
||||
_setup_sprite_frames(texture)
|
||||
else:
|
||||
print("[Player] Failed to load sprite sheet texture")
|
||||
|
||||
# Setup aim line (will be hidden once sprites load)
|
||||
if aim_line:
|
||||
aim_line.default_color = Color(0.53, 1.0, 0.67) # #88ffaa
|
||||
aim_line.width = 3.0
|
||||
aim_line.visible = false # Hide by default, show only if no sprite
|
||||
|
||||
# Start with idle animation
|
||||
if animated_sprite and animated_sprite.sprite_frames:
|
||||
animated_sprite.play("walk_down")
|
||||
# Hide aim line since we have sprites
|
||||
if aim_line:
|
||||
aim_line.visible = false
|
||||
else:
|
||||
# No sprites loaded, show aim line as fallback
|
||||
if aim_line:
|
||||
aim_line.visible = true
|
||||
|
||||
func _setup_sprite_frames(texture: Texture2D):
|
||||
"""
|
||||
Setup sprite frames from the Warrior-Blue sprite sheet
|
||||
768x256 pixels = 24 frames wide × 8 rows tall (each frame is 32x32)
|
||||
|
||||
Sprite sheet layout (user specified):
|
||||
- Row 1 (Y=0): down direction
|
||||
- Row 3 (Y=64): right direction
|
||||
- Row 5 (Y=128): up direction
|
||||
- Row 7 (Y=192): left direction
|
||||
|
||||
Each row contains (24 frames total):
|
||||
- Frames 0-3: walk
|
||||
- Frames 4-7: attack with sword
|
||||
- Frames 8-11: attack with bow
|
||||
- Frames 12-15: attack with spell
|
||||
- Frames 16-19: attack with fist
|
||||
- Frame 20: hurt (single frame)
|
||||
- Frames 21-24: dead
|
||||
"""
|
||||
if not animated_sprite:
|
||||
print("[Player] No animated_sprite node found!")
|
||||
return
|
||||
|
||||
var frames = SpriteFrames.new()
|
||||
var frame_width = 32
|
||||
var frame_height = 32
|
||||
|
||||
# Helper to create animation from sprite sheet row
|
||||
var create_anim = func(anim_name: String, row_y: int, start_col: int, frame_count: int, loop: bool = true):
|
||||
frames.add_animation(anim_name)
|
||||
frames.set_animation_loop(anim_name, loop)
|
||||
frames.set_animation_speed(anim_name, 8.0) # Slower animation speed for better visibility
|
||||
|
||||
for i in range(frame_count):
|
||||
var atlas = AtlasTexture.new()
|
||||
atlas.atlas = texture
|
||||
atlas.region = Rect2(
|
||||
(start_col + i) * frame_width,
|
||||
row_y,
|
||||
frame_width,
|
||||
frame_height
|
||||
)
|
||||
frames.add_frame(anim_name, atlas)
|
||||
|
||||
print("[Player] Created animation '%s' with %d frames at row Y=%d, starting col %d" % [anim_name, frame_count, row_y, start_col])
|
||||
|
||||
# Walk animations (4 frames each, starting at column 0)
|
||||
create_anim.call("walk_down", 0, 0, 4) # Row 1 (Y=0)
|
||||
create_anim.call("walk_right", 64, 0, 4) # Row 3 (Y=64)
|
||||
create_anim.call("walk_up", 128, 0, 4) # Row 5 (Y=128)
|
||||
create_anim.call("walk_left", 192, 0, 4) # Row 7 (Y=192)
|
||||
|
||||
# Attack with sword (4 frames each, starting at column 4)
|
||||
create_anim.call("attack_sword_down", 0, 4, 4, false)
|
||||
create_anim.call("attack_sword_right", 64, 4, 4, false)
|
||||
create_anim.call("attack_sword_up", 128, 4, 4, false)
|
||||
create_anim.call("attack_sword_left", 192, 4, 4, false)
|
||||
|
||||
# Attack with bow (4 frames each, starting at column 8)
|
||||
create_anim.call("attack_bow_down", 0, 8, 4, false)
|
||||
create_anim.call("attack_bow_right", 64, 8, 4, false)
|
||||
create_anim.call("attack_bow_up", 128, 8, 4, false)
|
||||
create_anim.call("attack_bow_left", 192, 8, 4, false)
|
||||
|
||||
# Attack with spell (4 frames each, starting at column 12)
|
||||
create_anim.call("attack_spell_down", 0, 12, 4, false)
|
||||
create_anim.call("attack_spell_right", 64, 12, 4, false)
|
||||
create_anim.call("attack_spell_up", 128, 12, 4, false)
|
||||
create_anim.call("attack_spell_left", 192, 12, 4, false)
|
||||
|
||||
# Attack with fist (4 frames each, starting at column 16)
|
||||
create_anim.call("attack_fist_down", 0, 16, 4, false)
|
||||
create_anim.call("attack_fist_right", 64, 16, 4, false)
|
||||
create_anim.call("attack_fist_up", 128, 16, 4, false)
|
||||
create_anim.call("attack_fist_left", 192, 16, 4, false)
|
||||
|
||||
# Hurt (1 frame, at column 20)
|
||||
create_anim.call("hurt_down", 0, 20, 1, false)
|
||||
create_anim.call("hurt_right", 64, 20, 1, false)
|
||||
create_anim.call("hurt_up", 128, 20, 1, false)
|
||||
create_anim.call("hurt_left", 192, 20, 1, false)
|
||||
|
||||
# Dead (4 frames, starting at column 21)
|
||||
create_anim.call("dead_down", 0, 21, 4, false)
|
||||
create_anim.call("dead_right", 64, 21, 4, false)
|
||||
create_anim.call("dead_up", 128, 21, 4, false)
|
||||
create_anim.call("dead_left", 192, 21, 4, false)
|
||||
|
||||
animated_sprite.sprite_frames = frames
|
||||
print("[Player] Sprite frames setup complete with %d animations" % frames.get_animation_names().size())
|
||||
|
||||
func _process(_delta):
|
||||
# Update aim line direction (only if visible as fallback)
|
||||
if aim_line and aim_line.visible:
|
||||
aim_line.clear_points()
|
||||
aim_line.add_point(Vector2.ZERO)
|
||||
aim_line.add_point(Vector2(cos(aim_angle) * 30, sin(aim_angle) * 30))
|
||||
|
||||
# Update animation based on movement and attack state
|
||||
if not is_attacking:
|
||||
_update_walk_animation()
|
||||
|
||||
func set_aim_angle(angle: float):
|
||||
aim_angle = angle
|
||||
|
||||
# Update facing direction based on aim angle
|
||||
# Convert angle to direction (down, up, left, right)
|
||||
var deg = rad_to_deg(angle)
|
||||
if deg < 0:
|
||||
deg += 360
|
||||
|
||||
if deg >= 315 or deg < 45:
|
||||
current_direction = "right"
|
||||
elif deg >= 45 and deg < 135:
|
||||
current_direction = "down"
|
||||
elif deg >= 135 and deg < 225:
|
||||
current_direction = "left"
|
||||
else:
|
||||
current_direction = "up"
|
||||
|
||||
func _update_walk_animation():
|
||||
if not animated_sprite or not animated_sprite.sprite_frames:
|
||||
return
|
||||
|
||||
var anim_name = "walk_" + current_direction
|
||||
if animated_sprite.animation != anim_name:
|
||||
animated_sprite.play(anim_name)
|
||||
|
||||
func play_attack_animation(weapon_type: String = "fist"):
|
||||
"""
|
||||
Play attack animation based on weapon type
|
||||
weapon_type: "sword", "bow", "spell", "fist"
|
||||
"""
|
||||
if not animated_sprite or not animated_sprite.sprite_frames:
|
||||
return
|
||||
|
||||
is_attacking = true
|
||||
var anim_name = "attack_" + weapon_type + "_" + current_direction
|
||||
|
||||
if animated_sprite.sprite_frames.has_animation(anim_name):
|
||||
animated_sprite.play(anim_name)
|
||||
# Wait for animation to finish
|
||||
await animated_sprite.animation_finished
|
||||
|
||||
is_attacking = false
|
||||
_update_walk_animation()
|
||||
|
||||
func play_hurt_animation():
|
||||
if not animated_sprite or not animated_sprite.sprite_frames:
|
||||
return
|
||||
|
||||
var anim_name = "hurt_" + current_direction
|
||||
if animated_sprite.sprite_frames.has_animation(anim_name):
|
||||
animated_sprite.play(anim_name)
|
||||
await get_tree().create_timer(0.2).timeout
|
||||
_update_walk_animation()
|
||||
|
||||
func play_dead_animation():
|
||||
if not animated_sprite or not animated_sprite.sprite_frames:
|
||||
return
|
||||
|
||||
var anim_name = "dead_" + current_direction
|
||||
if animated_sprite.sprite_frames.has_animation(anim_name):
|
||||
animated_sprite.play(anim_name)
|
||||
|
||||
func update_health(current: int, maximum: int):
|
||||
health_bar.max_value = maximum
|
||||
health_bar.value = current
|
||||
|
||||
func set_player_name(pname: String):
|
||||
name_label.text = pname
|
||||
|
||||
func set_movement(direction: Vector2):
|
||||
"""Called by dungeon manager to update movement for animation"""
|
||||
move_direction = direction
|
||||
@@ -0,0 +1 @@
|
||||
uid://nfk0y6eg2n6d
|
||||
@@ -10,6 +10,12 @@ var source_type: String = "" # "player" or "mob"
|
||||
var source_id: int = 0
|
||||
var velocity: Vector2 = Vector2.ZERO
|
||||
var sprite_key: String = ""
|
||||
var is_linear: bool = false
|
||||
var front_x: int = 0 # Arrow tip X coordinate in sprite
|
||||
var front_y: int = 0 # Arrow tip Y coordinate in sprite
|
||||
var back_x: int = 0 # Arrow tail X coordinate in sprite
|
||||
var back_y: int = 0 # Arrow tail Y coordinate in sprite
|
||||
var spin_speed: float = 10.0 # Radians per second for spinning projectiles
|
||||
|
||||
func _ready():
|
||||
# Create sprite node
|
||||
@@ -46,6 +52,23 @@ func _load_sprite():
|
||||
sprite.texture = texture
|
||||
sprite.modulate = Color.WHITE # Reset modulate
|
||||
sprite.scale = Vector2(0.5, 0.5) # Scale down projectiles
|
||||
|
||||
# For linear projectiles, offset sprite so it rotates around the midpoint
|
||||
# between front (tip) and back (tail) coordinates
|
||||
if is_linear and front_x > 0 and back_x > 0:
|
||||
# Calculate center point between arrow tip and tail
|
||||
var center_x = (front_x + back_x) / 2.0
|
||||
var center_y = (front_y + back_y) / 2.0
|
||||
|
||||
# Offset the sprite so the center point is at (0,0) in local space
|
||||
sprite.offset = Vector2(-center_x, -center_y)
|
||||
|
||||
# Calculate the arrow's natural angle in the sprite (from back to front)
|
||||
var arrow_angle = Vector2(front_x - back_x, front_y - back_y).angle()
|
||||
|
||||
# Store the arrow's natural angle so we can rotate it correctly
|
||||
# The sprite is oriented at arrow_angle, but we want it to face the velocity direction
|
||||
sprite.rotation = -arrow_angle
|
||||
else:
|
||||
print("[Projectile] Failed to load sprite: ", sprite_key)
|
||||
_use_fallback_sprite()
|
||||
@@ -56,7 +79,14 @@ func _process(delta):
|
||||
# Move projectile based on velocity (client-side prediction)
|
||||
if velocity.length() > 0:
|
||||
position += velocity * delta
|
||||
rotation = velocity.angle()
|
||||
|
||||
# Rotation behavior depends on projectile type
|
||||
if is_linear:
|
||||
# Linear projectiles (arrows) point in direction of travel
|
||||
rotation = velocity.angle()
|
||||
else:
|
||||
# Spinning projectiles rotate continuously
|
||||
rotation += spin_speed * delta
|
||||
|
||||
func initialize(data: Dictionary):
|
||||
projectile_id = data.get("id", -1)
|
||||
@@ -70,12 +100,17 @@ func initialize(data: Dictionary):
|
||||
velocity.y = data.get("vy", 0)
|
||||
|
||||
sprite_key = data.get("sprite", "")
|
||||
is_linear = data.get("isLinear", false)
|
||||
front_x = data.get("frontX", 0)
|
||||
front_y = data.get("frontY", 0)
|
||||
back_x = data.get("backX", 0)
|
||||
back_y = data.get("backY", 0)
|
||||
|
||||
# Set rotation based on initial velocity
|
||||
if velocity.length() > 0:
|
||||
# Set rotation based on initial velocity (for linear projectiles)
|
||||
if velocity.length() > 0 and is_linear:
|
||||
rotation = velocity.angle()
|
||||
|
||||
# Load sprite after initialization
|
||||
_load_sprite()
|
||||
|
||||
print("[Projectile] Initialized projectile ", projectile_id, " at (", position.x, ", ", position.y, ") with velocity (", velocity.x, ", ", velocity.y, ")")
|
||||
print("[Projectile] Initialized projectile ", projectile_id, " at (", position.x, ", ", position.y, ") with velocity (", velocity.x, ", ", velocity.y, "), linear=", is_linear)
|
||||
|
||||
Reference in New Issue
Block a user