258 lines
9.0 KiB
GDScript
258 lines
9.0 KiB
GDScript
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:
|
||
print("[Player] Checking if player sprite is available...")
|
||
print("[Player] UpdateManager._is_web: %s" % UpdateManager._is_web)
|
||
print("[Player] Has file in cache: %s" % UpdateManager.has_file("textures/player/Warrior-Blue.png"))
|
||
|
||
# Try to load immediately (may already be cached)
|
||
var texture = UpdateManager.load_texture("textures/player/Warrior-Blue.png")
|
||
if texture:
|
||
print("[Player] ✓ Loaded sprite sheet texture successfully")
|
||
_setup_sprite_frames(texture)
|
||
else:
|
||
print("[Player] ✗ Sprite sheet not available yet, connecting to file_downloaded signal")
|
||
# Connect to file_downloaded signal to retry when file downloads
|
||
if not UpdateManager.file_downloaded.is_connected(_on_file_downloaded):
|
||
UpdateManager.file_downloaded.connect(_on_file_downloaded)
|
||
print("[Player] Connected to file_downloaded signal")
|
||
|
||
# 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 _on_file_downloaded(file_path: String):
|
||
print("[Player] file_downloaded signal received: %s" % file_path)
|
||
|
||
# Check if the player sprite was downloaded
|
||
if file_path == "textures/player/Warrior-Blue.png" and UpdateManager:
|
||
print("[Player] Player sprite downloaded! Attempting to load...")
|
||
var texture = UpdateManager.load_texture("textures/player/Warrior-Blue.png")
|
||
if texture:
|
||
print("[Player] ✓ Loaded sprite sheet texture after download")
|
||
_setup_sprite_frames(texture)
|
||
# Start playing walk animation
|
||
if animated_sprite and animated_sprite.sprite_frames:
|
||
animated_sprite.play("walk_down")
|
||
print("[Player] Started walk_down animation")
|
||
# Hide aim line since we have sprites
|
||
if aim_line:
|
||
aim_line.visible = false
|
||
else:
|
||
print("[Player] ✗ Failed to load texture even after download!")
|
||
|
||
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
|
||
# Don't update direction from aim - only update from movement
|
||
|
||
func _update_walk_animation():
|
||
if not animated_sprite or not animated_sprite.sprite_frames:
|
||
return
|
||
|
||
# Update direction based on movement vector
|
||
if move_direction.length() > 0.1:
|
||
# Player is moving, update direction based on movement
|
||
var angle = move_direction.angle()
|
||
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"
|
||
|
||
# Play walk animation for current direction (will loop)
|
||
var anim_name = "walk_" + current_direction
|
||
if animated_sprite.animation != anim_name and not is_attacking:
|
||
animated_sprite.play(anim_name)
|
||
else:
|
||
# Player stopped moving - show idle frame (frame 0 of walk animation)
|
||
if not is_attacking:
|
||
var anim_name = "walk_" + current_direction
|
||
if animated_sprite.animation != anim_name or animated_sprite.is_playing():
|
||
animated_sprite.play(anim_name)
|
||
animated_sprite.pause()
|
||
animated_sprite.frame = 0 # Show first frame as idle
|
||
|
||
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
|