mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-06 07:14:55 +08:00
[godot] Fetch animation player on the fly, improve animation player example.
This commit is contained in:
parent
15e6ccf05e
commit
dfd2fc057a
@ -16,4 +16,5 @@ mix = 0.2
|
||||
[resource]
|
||||
atlas_res = ExtResource( 1 )
|
||||
skeleton_file_res = ExtResource( 2 )
|
||||
default_mix = 0.2
|
||||
animation_mixes = [ SubResource( 1 ), SubResource( 2 ) ]
|
||||
|
||||
@ -1,10 +1,33 @@
|
||||
extends Node2D
|
||||
|
||||
onready var player = $AnimationPlayer
|
||||
onready var spineboy = $Spineboy
|
||||
|
||||
var speed = 400;
|
||||
var velocity_x = 0;
|
||||
|
||||
func _ready():
|
||||
player.play("cutscene")
|
||||
pass
|
||||
|
||||
func _process(_delta):
|
||||
if Input.is_action_just_pressed("ui_accept"):
|
||||
player.play("walk-run-die")
|
||||
|
||||
func _process(delta):
|
||||
if (!player.is_playing()):
|
||||
if Input.is_action_just_released("ui_left"):
|
||||
spineboy.get_animation_state().set_animation("idle", true, 0)
|
||||
velocity_x = 0
|
||||
|
||||
if Input.is_action_just_released("ui_right"):
|
||||
spineboy.get_animation_state().set_animation("idle", true, 0)
|
||||
velocity_x = 0
|
||||
|
||||
if (Input.is_action_just_pressed("ui_right")):
|
||||
spineboy.get_animation_state().set_animation("run", true, 0)
|
||||
spineboy.get_skeleton().set_scale_x(1)
|
||||
velocity_x = 1
|
||||
|
||||
if Input.is_action_just_pressed("ui_left"):
|
||||
spineboy.get_animation_state().set_animation("run", true, 0)
|
||||
spineboy.get_skeleton().set_scale_x(-1)
|
||||
velocity_x = -1
|
||||
|
||||
spineboy.position.x += velocity_x * speed * delta
|
||||
|
||||
@ -1646,7 +1646,7 @@ tracks/1/keys = {
|
||||
"times": PoolRealArray( 0 ),
|
||||
"transitions": PoolRealArray( 1 ),
|
||||
"update": 0,
|
||||
"values": [ Vector2( 1284, 540 ) ]
|
||||
"values": [ Vector2( 1284, 520 ) ]
|
||||
}
|
||||
tracks/2/type = "value"
|
||||
tracks/2/path = NodePath("Spineboy/SpineAnimationTrack:mix_duration")
|
||||
@ -1697,49 +1697,9 @@ tracks/5/keys = {
|
||||
"values": [ 1.0 ]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id=83]
|
||||
resource_name = "reverse-timescale"
|
||||
length = 2.0
|
||||
loop = true
|
||||
tracks/0/type = "value"
|
||||
tracks/0/path = NodePath("Spineboy/SpineAnimationTrack:reverse")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/keys = {
|
||||
"times": PoolRealArray( 0 ),
|
||||
"transitions": PoolRealArray( 1 ),
|
||||
"update": 1,
|
||||
"values": [ true ]
|
||||
}
|
||||
tracks/1/type = "animation"
|
||||
tracks/1/path = NodePath("Spineboy/SpineAnimationTrack/Spineboy Track 0")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/keys = {
|
||||
"clips": PoolStringArray( "walk_looped" ),
|
||||
"times": PoolRealArray( 0 )
|
||||
}
|
||||
tracks/2/type = "value"
|
||||
tracks/2/path = NodePath("Spineboy/SpineAnimationTrack:time_scale")
|
||||
tracks/2/interp = 1
|
||||
tracks/2/loop_wrap = true
|
||||
tracks/2/imported = false
|
||||
tracks/2/enabled = true
|
||||
tracks/2/keys = {
|
||||
"times": PoolRealArray( 0 ),
|
||||
"transitions": PoolRealArray( 1 ),
|
||||
"update": 0,
|
||||
"values": [ 0.5 ]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id=1]
|
||||
resource_name = "walk-run-die"
|
||||
resource_name = "cutscene"
|
||||
length = 10.0
|
||||
loop = true
|
||||
step = 0.02
|
||||
tracks/0/type = "value"
|
||||
tracks/0/path = NodePath("Spineboy:position")
|
||||
@ -1760,8 +1720,8 @@ tracks/1/loop_wrap = true
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/keys = {
|
||||
"clips": PoolStringArray( "walk_looped", "idle", "death" ),
|
||||
"times": PoolRealArray( 0, 4, 4.34 )
|
||||
"clips": PoolStringArray( "walk_looped", "idle", "death", "idle" ),
|
||||
"times": PoolRealArray( 0, 4, 4.34, 8.86 )
|
||||
}
|
||||
tracks/2/type = "value"
|
||||
tracks/2/path = NodePath("Spineboy/SpineAnimationTrack2:mix_duration")
|
||||
@ -1795,7 +1755,7 @@ tracks/4/keys = {
|
||||
"times": PoolRealArray( 0, 2.02, 3.7, 10 ),
|
||||
"transitions": PoolRealArray( 1, 1, 1, 1 ),
|
||||
"update": 0,
|
||||
"values": [ Vector2( 1284, 540 ), Vector2( 1284, 540 ), Vector2( 800, 540 ), Vector2( 800, 540 ) ]
|
||||
"values": [ Vector2( 1284, 520 ), Vector2( 1284, 520 ), Vector2( 750, 520 ), Vector2( 750, 520 ) ]
|
||||
}
|
||||
tracks/5/type = "animation"
|
||||
tracks/5/path = NodePath("Raptor/SpineAnimationTrack/Raptor Track 0")
|
||||
@ -1819,6 +1779,69 @@ tracks/6/keys = {
|
||||
"update": 0,
|
||||
"values": [ 0.2 ]
|
||||
}
|
||||
tracks/7/type = "value"
|
||||
tracks/7/path = NodePath("Raptor/SpineAnimationTrack:mix_duration")
|
||||
tracks/7/interp = 1
|
||||
tracks/7/loop_wrap = true
|
||||
tracks/7/imported = false
|
||||
tracks/7/enabled = true
|
||||
tracks/7/keys = {
|
||||
"times": PoolRealArray( 0 ),
|
||||
"transitions": PoolRealArray( 1 ),
|
||||
"update": 0,
|
||||
"values": [ 0.2 ]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id=83]
|
||||
resource_name = "slow-moonwalk"
|
||||
length = 5.0
|
||||
loop = true
|
||||
tracks/0/type = "value"
|
||||
tracks/0/path = NodePath("Spineboy:position")
|
||||
tracks/0/interp = 1
|
||||
tracks/0/loop_wrap = true
|
||||
tracks/0/imported = false
|
||||
tracks/0/enabled = true
|
||||
tracks/0/keys = {
|
||||
"times": PoolRealArray( 0, 5 ),
|
||||
"transitions": PoolRealArray( 1, 1 ),
|
||||
"update": 0,
|
||||
"values": [ Vector2( 905, 565 ), Vector2( 70, 565 ) ]
|
||||
}
|
||||
tracks/1/type = "animation"
|
||||
tracks/1/path = NodePath("Spineboy/SpineAnimationTrack/Spineboy Track 0")
|
||||
tracks/1/interp = 1
|
||||
tracks/1/loop_wrap = true
|
||||
tracks/1/imported = false
|
||||
tracks/1/enabled = true
|
||||
tracks/1/keys = {
|
||||
"clips": PoolStringArray( "walk_looped" ),
|
||||
"times": PoolRealArray( 0 )
|
||||
}
|
||||
tracks/2/type = "value"
|
||||
tracks/2/path = NodePath("Spineboy/SpineAnimationTrack:reverse")
|
||||
tracks/2/interp = 1
|
||||
tracks/2/loop_wrap = true
|
||||
tracks/2/imported = false
|
||||
tracks/2/enabled = true
|
||||
tracks/2/keys = {
|
||||
"times": PoolRealArray( 0 ),
|
||||
"transitions": PoolRealArray( 1 ),
|
||||
"update": 1,
|
||||
"values": [ true ]
|
||||
}
|
||||
tracks/3/type = "value"
|
||||
tracks/3/path = NodePath("Spineboy/SpineAnimationTrack:time_scale")
|
||||
tracks/3/interp = 1
|
||||
tracks/3/loop_wrap = true
|
||||
tracks/3/imported = false
|
||||
tracks/3/enabled = true
|
||||
tracks/3/keys = {
|
||||
"times": PoolRealArray( 0 ),
|
||||
"transitions": PoolRealArray( 1 ),
|
||||
"update": 0,
|
||||
"values": [ 0.5 ]
|
||||
}
|
||||
|
||||
[node name="Node2D" type="Node2D"]
|
||||
script = ExtResource( 2 )
|
||||
@ -1890,7 +1913,7 @@ anims/walk = SubResource( 128 )
|
||||
anims/walk_looped = SubResource( 129 )
|
||||
|
||||
[node name="Raptor" type="SpineSprite" parent="."]
|
||||
position = Vector2( 1284, 540 )
|
||||
position = Vector2( 1284, 520 )
|
||||
scale = Vector2( -0.328761, 0.328761 )
|
||||
skeleton_data_res = ExtResource( 3 )
|
||||
|
||||
@ -1913,5 +1936,5 @@ anims/walk_looped = SubResource( 140 )
|
||||
|
||||
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
|
||||
anims/RESET = SubResource( 25 )
|
||||
anims/reverse-timescale = SubResource( 83 )
|
||||
anims/walk-run-die = SubResource( 1 )
|
||||
anims/cutscene = SubResource( 1 )
|
||||
anims/slow-moonwalk = SubResource( 83 )
|
||||
|
||||
@ -67,8 +67,7 @@ SpineAnimationTrack::SpineAnimationTrack():
|
||||
draw_order_threshold(0),
|
||||
mix_blend(SpineConstant::MixBlend_Replace),
|
||||
debug(false),
|
||||
sprite(nullptr),
|
||||
animation_player(nullptr) {
|
||||
sprite(nullptr) {
|
||||
}
|
||||
|
||||
void SpineAnimationTrack::_notification(int what) {
|
||||
@ -85,8 +84,10 @@ void SpineAnimationTrack::_notification(int what) {
|
||||
break;
|
||||
}
|
||||
case NOTIFICATION_UNPARENTED: {
|
||||
if (sprite)
|
||||
if (sprite) {
|
||||
sprite->disconnect("before_animation_state_update", this, "update_animation_state");
|
||||
sprite = nullptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
@ -94,9 +95,22 @@ void SpineAnimationTrack::_notification(int what) {
|
||||
}
|
||||
}
|
||||
|
||||
AnimationPlayer* SpineAnimationTrack::find_animation_player() {
|
||||
AnimationPlayer *animation_player = nullptr;
|
||||
for (int i = 0; i < get_child_count(); i++) {
|
||||
animation_player = cast_to<AnimationPlayer>(get_child(i));
|
||||
if (animation_player) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return animation_player;
|
||||
}
|
||||
|
||||
void SpineAnimationTrack::setup_animation_player() {
|
||||
if (!sprite) return;
|
||||
if (!sprite->get_skeleton_data_res().is_valid() || !sprite->get_skeleton_data_res()->is_skeleton_data_loaded()) return;
|
||||
AnimationPlayer *animation_player = find_animation_player();
|
||||
if (!animation_player) return;
|
||||
|
||||
// If we don't have a track index yet, find the highest track number used
|
||||
// by existing tracks.
|
||||
@ -114,14 +128,6 @@ void SpineAnimationTrack::setup_animation_player() {
|
||||
|
||||
// Find the animation player under the track and reset its animation. Create a new one
|
||||
// if there isn't one already.
|
||||
animation_player = nullptr;
|
||||
for (int i = 0; i < get_child_count(); i++) {
|
||||
animation_player = cast_to<AnimationPlayer>(get_child(i));
|
||||
if (animation_player) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!animation_player) {
|
||||
animation_player = memnew(AnimationPlayer);
|
||||
animation_player->set_name(String("{0} Track {1}").format(varray(sprite->get_name(), String::num_int64(track_index))));
|
||||
@ -187,6 +193,9 @@ void SpineAnimationTrack::update_animation_state(const Variant &variant_sprite)
|
||||
if (!sprite->get_skeleton_data_res().is_valid() || !sprite->get_skeleton_data_res()->is_skeleton_data_loaded()) return;
|
||||
if (track_index < 0) return;
|
||||
|
||||
AnimationPlayer *animation_player = find_animation_player();
|
||||
if (!animation_player) return;
|
||||
|
||||
spine::AnimationState *animation_state = sprite->get_animation_state()->get_spine_object();
|
||||
if (!animation_state) return;
|
||||
spine::Skeleton *skeleton = sprite->get_skeleton()->get_spine_object();
|
||||
|
||||
@ -57,11 +57,12 @@ protected:
|
||||
bool debug;
|
||||
|
||||
SpineSprite *sprite;
|
||||
AnimationPlayer *animation_player;
|
||||
|
||||
static void _bind_methods();
|
||||
|
||||
void _notification(int what);
|
||||
|
||||
AnimationPlayer *find_animation_player();
|
||||
|
||||
void setup_animation_player();
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user