Files
2026-02-09 08:58:44 -05:00

143 lines
4.5 KiB
GDScript

extends Node2D
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite
@onready var fallback_sprite: Sprite2D = $FallbackSprite
@onready var name_label: Label = $NameLabel
@onready var health_bar: ProgressBar = $HealthBar
var player_data: Dictionary = {}
var player_id: int = 0
var _tint_color: Color = Color(0.27, 0.67, 1.0)
func _ready():
# Start with fallback circle until the sprite sheet is ready
if fallback_sprite and SpriteGenerator:
fallback_sprite.texture = SpriteGenerator.get_circle()
fallback_sprite.visible = true
if animated_sprite:
animated_sprite.visible = false
# Try to load sprite sheet immediately
if UpdateManager:
var texture = UpdateManager.load_texture("textures/player/Warrior-Blue.png")
if texture:
_setup_sprite_frames(texture)
else:
if not UpdateManager.file_downloaded.is_connected(_on_file_downloaded):
UpdateManager.file_downloaded.connect(_on_file_downloaded)
func update_data(data: Dictionary):
player_data = data
player_id = int(data.get("id", data.get("Id", 0)))
var pname = data.get("name", data.get("Name", "Player"))
if name_label:
name_label.text = pname
var hp = data.get("health", data.get("Health", 100))
var max_hp = data.get("maxHealth", data.get("MaxHealth", 100))
if health_bar:
health_bar.max_value = max_hp
health_bar.value = hp
var is_alive = data.get("isAlive", data.get("IsAlive", true))
if is_alive:
_tint_color = _color_from_id(player_id)
else:
_tint_color = Color(0.3, 0.3, 0.3)
_apply_tint(_tint_color)
# Ensure idle animation is playing when available
if animated_sprite and animated_sprite.sprite_frames:
if animated_sprite.animation != "walk_down":
animated_sprite.play("walk_down")
func _on_file_downloaded(file_path: String):
if file_path != "textures/player/Warrior-Blue.png":
return
if UpdateManager:
var texture = UpdateManager.load_texture("textures/player/Warrior-Blue.png")
if texture:
_setup_sprite_frames(texture)
func _apply_tint(tint: Color):
if animated_sprite:
animated_sprite.modulate = tint
if fallback_sprite:
fallback_sprite.modulate = tint
func _setup_sprite_frames(texture: Texture2D):
if not animated_sprite:
return
var frames = SpriteFrames.new()
var frame_width = 32
var frame_height = 32
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)
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)
create_anim.call("walk_down", 0, 0, 4)
create_anim.call("walk_right", 64, 0, 4)
create_anim.call("walk_up", 128, 0, 4)
create_anim.call("walk_left", 192, 0, 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)
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)
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)
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)
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)
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
animated_sprite.visible = true
animated_sprite.play("walk_down")
if fallback_sprite:
fallback_sprite.visible = false
_apply_tint(_tint_color)
func _color_from_id(id_value: int) -> Color:
# Stable, distinct tint per player ID
var hue = float(abs(id_value) % 360) / 360.0
return Color.from_hsv(hue, 0.6, 1.0)