supervisor-simulator/scripts/StudentPortrait16Native.cs

65 lines
2.0 KiB
C#

using System.Collections.Generic;
using Godot;
using Views;
public partial class StudentPortrait16Native : Node2D {
/// <summary>
/// 动画播放器
/// </summary>
private AnimationPlayer _animationPlayer;
private readonly SuitTheme _theme = new();
/// <summary>
/// 主题ID
/// </summary>
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>("AnimationPlayer");
CacheSprites();
if (_animationPlayer != null) _animationPlayer.AnimationFinished += OnAnimationFinished;
PlayAnimation("speak");
}
/// <summary>
/// 缓存精灵节点
/// </summary>
private void CacheSprites() {
// 缓存子节点引用,避免每帧查找
_theme.CacheComponent(Res.Type.Accessory, GetNode<Sprite2D>("parts/accessory"));
_theme.CacheComponent(Res.Type.Body, GetNode<Sprite2D>("parts/body"));
_theme.CacheComponent(Res.Type.Eye, GetNode<Sprite2D>("parts/eye"));
_theme.CacheComponent(Res.Type.Hair, GetNode<Sprite2D>("parts/hairstyle"));
}
/// <summary>
/// 播放指定动画
/// </summary>
/// <param name="animationName">动画名称</param>
private void PlayAnimation(string animationName) {
if (_animationPlayer == null) return;
if (_animationPlayer.CurrentAnimation != animationName) _animationPlayer.Play(animationName);
}
/// <summary>
/// 动画结束回调
/// </summary>
/// <param name="animationName">动画名称</param>
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) { }
}