291 lines
8.6 KiB
GDScript
291 lines
8.6 KiB
GDScript
extends Node2D
|
|
|
|
## Mob Visual
|
|
## Renders mobs using sprite sheets from CDN with JSON animation config
|
|
## Falls back to simple shapes if assets aren't available
|
|
|
|
var fallback_sprite: Sprite2D = null
|
|
var name_label: Label = null
|
|
var health_bar: ProgressBar = null
|
|
|
|
var mob_data: Dictionary = {}
|
|
var _sprite_config: Dictionary = {} # Animation config from server
|
|
var _animated_sprite: AnimatedSprite2D = null
|
|
var _using_fallback: bool = true
|
|
var _initialized: bool = false # Track if we've already set up this mob
|
|
var _last_animation: String = "" # Track animation to avoid spam
|
|
|
|
# Animation state
|
|
var _current_state: String = "idle"
|
|
var _facing_direction: String = "down" # down, up, left, right, front, back
|
|
|
|
func _ready():
|
|
# Try to find child nodes (they may not exist in all mob scenes)
|
|
fallback_sprite = get_node_or_null("Sprite2D")
|
|
name_label = get_node_or_null("NameLabel")
|
|
health_bar = get_node_or_null("HealthBar")
|
|
|
|
# Create fallback sprite if it doesn't exist
|
|
if not fallback_sprite:
|
|
fallback_sprite = Sprite2D.new()
|
|
fallback_sprite.name = "FallbackSprite"
|
|
add_child(fallback_sprite)
|
|
|
|
# Create name label if it doesn't exist
|
|
if not name_label:
|
|
name_label = Label.new()
|
|
name_label.name = "NameLabel"
|
|
name_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
|
name_label.position = Vector2(-50, -35)
|
|
name_label.size = Vector2(100, 15)
|
|
add_child(name_label)
|
|
|
|
# Create health bar if it doesn't exist
|
|
if not health_bar:
|
|
health_bar = ProgressBar.new()
|
|
health_bar.name = "HealthBar"
|
|
health_bar.position = Vector2(-20, -28)
|
|
health_bar.size = Vector2(40, 5)
|
|
health_bar.show_percentage = false
|
|
add_child(health_bar)
|
|
|
|
# Connect to UpdateManager for async texture loading
|
|
if UpdateManager:
|
|
UpdateManager.file_downloaded.connect(_on_file_downloaded)
|
|
|
|
func update_data(data: Dictionary):
|
|
mob_data = data
|
|
|
|
# Update name
|
|
var mname = data.get("name", data.get("Name", "Mob"))
|
|
if name_label:
|
|
name_label.text = mname
|
|
|
|
# Update health bar
|
|
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
|
|
|
|
# Only process sprite config on first update or if it changed
|
|
if not _initialized:
|
|
var sprite_config = data.get("spriteConfig", data.get("SpriteConfig", null))
|
|
|
|
if sprite_config != null and sprite_config is Dictionary and not sprite_config.is_empty():
|
|
if sprite_config != _sprite_config:
|
|
_sprite_config = sprite_config
|
|
_build_animated_sprite()
|
|
else:
|
|
_setup_fallback(data)
|
|
|
|
_initialized = true
|
|
|
|
# Update visual state
|
|
var state = data.get("state", data.get("State", "idle")).to_lower()
|
|
var rotation = data.get("rotation", data.get("Rotation", 0.0))
|
|
_update_animation_state(state, rotation)
|
|
|
|
# Apply tint (only on first setup)
|
|
if not _initialized:
|
|
var tint = data.get("tintColor", data.get("TintColor", "#ffffff"))
|
|
_apply_tint(tint)
|
|
|
|
# Apply scale
|
|
var mob_scale = data.get("scale", data.get("Scale", 1.0))
|
|
if mob_scale == null or mob_scale <= 0:
|
|
mob_scale = 1.0
|
|
|
|
if _animated_sprite:
|
|
_animated_sprite.scale = Vector2.ONE * mob_scale
|
|
elif fallback_sprite:
|
|
fallback_sprite.scale = Vector2.ONE * mob_scale
|
|
|
|
func _build_animated_sprite():
|
|
## Build AnimatedSprite2D from sprite config
|
|
var mname = mob_data.get("name", "unknown")
|
|
var sprites = _sprite_config.get("sprites", {})
|
|
|
|
if sprites == null or not sprites is Dictionary or sprites.is_empty():
|
|
_setup_fallback(mob_data)
|
|
return
|
|
|
|
# Check if all required textures are cached
|
|
var all_cached = true
|
|
var missing_files = []
|
|
for anim_name in sprites:
|
|
var sprite_path = sprites[anim_name]
|
|
if not UpdateManager or not UpdateManager.has_file(sprite_path):
|
|
all_cached = false
|
|
missing_files.append(sprite_path)
|
|
|
|
if not all_cached:
|
|
_setup_fallback(mob_data)
|
|
return
|
|
|
|
# Create or reuse AnimatedSprite2D
|
|
if not _animated_sprite:
|
|
_animated_sprite = AnimatedSprite2D.new()
|
|
_animated_sprite.name = "MobAnimatedSprite"
|
|
add_child(_animated_sprite)
|
|
|
|
# Build SpriteFrames
|
|
var sprite_frames = SpriteFrames.new()
|
|
var frame_width = _sprite_config.get("frameWidth", 64)
|
|
var frame_height = _sprite_config.get("frameHeight", 64)
|
|
var fps = _sprite_config.get("fps", 8)
|
|
var animations = _sprite_config.get("animations", {})
|
|
|
|
for anim_name in sprites:
|
|
var sprite_path = sprites[anim_name]
|
|
var texture = UpdateManager.load_texture(sprite_path)
|
|
|
|
if not texture:
|
|
continue
|
|
|
|
# Get animation config
|
|
var anim_config = animations.get(anim_name, {"frames": 1, "loop": true})
|
|
var frame_count = anim_config.get("frames", 1)
|
|
var loop = anim_config.get("loop", true)
|
|
frame_count = int(frame_count)
|
|
|
|
# Add animation
|
|
sprite_frames.add_animation(anim_name)
|
|
sprite_frames.set_animation_speed(anim_name, fps)
|
|
sprite_frames.set_animation_loop(anim_name, loop)
|
|
|
|
# Add frames from sprite sheet (horizontal strip)
|
|
var tex_width = texture.get_width()
|
|
var calculated_frames = int(tex_width / frame_width) if frame_width > 0 else 1
|
|
frame_count = min(frame_count, calculated_frames)
|
|
|
|
for i in range(frame_count):
|
|
var atlas = AtlasTexture.new()
|
|
atlas.atlas = texture
|
|
atlas.region = Rect2(i * frame_width, 0, frame_width, frame_height)
|
|
sprite_frames.add_frame(anim_name, atlas)
|
|
|
|
_animated_sprite.sprite_frames = sprite_frames
|
|
_animated_sprite.visible = true
|
|
_animated_sprite.z_index = 10
|
|
_animated_sprite.modulate = Color.WHITE
|
|
_animated_sprite.position = Vector2.ZERO
|
|
_using_fallback = false
|
|
|
|
# Hide fallback
|
|
if fallback_sprite:
|
|
fallback_sprite.visible = false
|
|
|
|
# Play default animation
|
|
_update_animation_state(_current_state, 0.0)
|
|
|
|
func _on_file_downloaded(file_path: String):
|
|
# Check if this is a texture we need
|
|
if not _sprite_config.is_empty():
|
|
var sprites = _sprite_config.get("sprites", {})
|
|
for anim_name in sprites:
|
|
if sprites[anim_name] == file_path:
|
|
# A texture we need was downloaded, try building again
|
|
_build_animated_sprite()
|
|
return
|
|
|
|
func _setup_fallback(data: Dictionary):
|
|
_using_fallback = true
|
|
|
|
# Hide animated sprite if exists
|
|
if _animated_sprite:
|
|
_animated_sprite.visible = false
|
|
|
|
if fallback_sprite:
|
|
fallback_sprite.visible = true
|
|
|
|
# Use SpriteGenerator for a simple shape
|
|
if SpriteGenerator:
|
|
fallback_sprite.texture = SpriteGenerator.get_circle()
|
|
|
|
# Color based on mob type or default red
|
|
var tint = data.get("tintColor", data.get("TintColor", "#cc4444"))
|
|
fallback_sprite.modulate = Color.from_string(tint, Color(0.8, 0.3, 0.3))
|
|
|
|
func _update_animation_state(state: String, rotation: float):
|
|
_current_state = state
|
|
|
|
# Determine facing direction from rotation
|
|
_facing_direction = _rotation_to_direction(rotation)
|
|
|
|
if _animated_sprite and _animated_sprite.sprite_frames:
|
|
_play_animation(state, _facing_direction)
|
|
else:
|
|
_update_fallback_state(state)
|
|
|
|
func _rotation_to_direction(rotation: float) -> String:
|
|
# Convert rotation (radians) to cardinal direction
|
|
var angle = fmod(rotation + TAU, TAU)
|
|
|
|
if angle < PI * 0.25 or angle >= PI * 1.75:
|
|
return "right"
|
|
elif angle < PI * 0.75:
|
|
return "front"
|
|
elif angle < PI * 1.25:
|
|
return "left"
|
|
else:
|
|
return "back"
|
|
|
|
func _play_animation(state: String, direction: String):
|
|
if not _animated_sprite or not _animated_sprite.sprite_frames:
|
|
return
|
|
|
|
var frames = _animated_sprite.sprite_frames
|
|
|
|
# Try different animation name formats
|
|
var names_to_try = [
|
|
"%s_%s" % [state, direction],
|
|
"%s_%s" % [state, _alt_direction(direction)],
|
|
state,
|
|
"idle_%s" % direction,
|
|
"idle_%s" % _alt_direction(direction),
|
|
"idle"
|
|
]
|
|
|
|
for anim_name in names_to_try:
|
|
if frames.has_animation(anim_name):
|
|
# Only log and play if animation changed
|
|
if anim_name != _last_animation:
|
|
_last_animation = anim_name
|
|
_animated_sprite.play(anim_name)
|
|
|
|
# Handle left direction with flip
|
|
_animated_sprite.flip_h = (direction == "left" and not frames.has_animation("%s_left" % state))
|
|
return
|
|
|
|
func _alt_direction(dir: String) -> String:
|
|
match dir:
|
|
"front": return "down"
|
|
"back": return "up"
|
|
"down": return "front"
|
|
"up": return "back"
|
|
_: return dir
|
|
|
|
func _update_fallback_state(state: String):
|
|
if not fallback_sprite:
|
|
return
|
|
|
|
match state:
|
|
"attacking":
|
|
fallback_sprite.modulate = fallback_sprite.modulate.lightened(0.3)
|
|
"chasing":
|
|
fallback_sprite.modulate = fallback_sprite.modulate.darkened(0.1)
|
|
"dead":
|
|
fallback_sprite.modulate = Color(0.3, 0.3, 0.3, 0.5)
|
|
_:
|
|
var tint = mob_data.get("tintColor", mob_data.get("TintColor", "#cc4444"))
|
|
fallback_sprite.modulate = Color.from_string(tint, Color(0.8, 0.3, 0.3))
|
|
|
|
func _apply_tint(tint_hex: String):
|
|
var tint_color = Color.from_string(tint_hex, Color.WHITE)
|
|
|
|
if _animated_sprite:
|
|
_animated_sprite.modulate = tint_color
|
|
elif fallback_sprite:
|
|
fallback_sprite.modulate = tint_color
|