70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
using Godot;
|
|
|
|
public partial class StudentPortrait16Native : Node2D {
|
|
/// <summary>
|
|
/// 饰品精灵
|
|
/// </summary>
|
|
private Sprite2D _accessory;
|
|
|
|
/// <summary>
|
|
/// 动画播放器
|
|
/// </summary>
|
|
private AnimationPlayer _animationPlayer;
|
|
|
|
/// <summary>
|
|
/// 身体精灵
|
|
/// </summary>
|
|
private Sprite2D _body;
|
|
|
|
/// <summary>
|
|
/// 眼睛精灵
|
|
/// </summary>
|
|
private Sprite2D _eye;
|
|
|
|
/// <summary>
|
|
/// 发型精灵
|
|
/// </summary>
|
|
private Sprite2D _hairstyle;
|
|
|
|
// Called when the node enters the scene tree for the first time.
|
|
public override void _Ready() {
|
|
_animationPlayer = GetNodeOrNull<AnimationPlayer>("AnimationPlayer");
|
|
CacheSprites();
|
|
|
|
if (_animationPlayer != null) _animationPlayer.AnimationFinished += OnAnimationFinished;
|
|
|
|
PlayAnimation("speak");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 缓存精灵节点
|
|
/// </summary>
|
|
private void CacheSprites() {
|
|
// 缓存子节点引用,避免每帧查找
|
|
_body = GetNode<Sprite2D>("parts/body");
|
|
_hairstyle = GetNode<Sprite2D>("parts/hairstyle");
|
|
_eye = GetNode<Sprite2D>("parts/eye");
|
|
_accessory = GetNode<Sprite2D>("parts/accessory");
|
|
}
|
|
|
|
/// <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) { }
|
|
} |