using System.Collections.Generic;
using Godot;
using Views;
public partial class StudentPortrait16Native : Node2D {
///
/// 动画播放器
///
private AnimationPlayer _animationPlayer;
private readonly SuitTheme _theme = new();
///
/// 主题ID
///
public ulong ThemeId {
get => _theme.GetThemeId();
set => _theme.ApplyTheme(value);
}
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
_theme.IsPortrait = true;
_theme.Use16 = true;
_animationPlayer = GetNodeOrNull("AnimationPlayer");
CacheSprites();
if (_animationPlayer != null) _animationPlayer.AnimationFinished += OnAnimationFinished;
PlayAnimation("speak");
}
///
/// 缓存精灵节点
///
private void CacheSprites() {
// 缓存子节点引用,避免每帧查找
_theme.CacheComponent(Res.Type.Accessory, GetNode("parts/accessory"));
_theme.CacheComponent(Res.Type.Body, GetNode("parts/body"));
_theme.CacheComponent(Res.Type.Eye, GetNode("parts/eye"));
_theme.CacheComponent(Res.Type.Hair, GetNode("parts/hairstyle"));
}
///
/// 播放指定动画
///
/// 动画名称
private void PlayAnimation(string animationName) {
if (_animationPlayer == null) return;
if (_animationPlayer.CurrentAnimation != animationName) _animationPlayer.Play(animationName);
}
///
/// 动画结束回调
///
/// 动画名称
private void OnAnimationFinished(StringName animationName) {
_animationPlayer?.Play(animationName);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) { }
}