rokojori_action_library/Runtime/Actions/LoadScene.cs

114 lines
2.2 KiB
C#
Raw Normal View History

2024-05-12 17:03:20 +00:00
using Godot;
using System.Collections.Generic;
namespace Rokojori
{
[Tool][GlobalClass]
2025-01-08 18:46:17 +00:00
public partial class LoadScene : SequenceAction
2024-05-12 17:03:20 +00:00
{
2026-02-26 14:06:27 +00:00
[Export( PropertyHint.File )]
2024-05-12 17:03:20 +00:00
public string scenePath;
[Export]
public Node target;
2026-02-26 14:06:27 +00:00
[ExportGroup("Transforming")]
[Export]
public Node3D poseParent;
[Export]
public Vector3 offsetTranslation = Vector3.Zero;
[Export]
public Quaternion offsetRotation = Quaternion.Identity;
2024-05-12 17:03:20 +00:00
[Export]
2025-01-08 18:46:17 +00:00
public Action onLoaded;
2024-05-12 17:03:20 +00:00
2026-02-26 14:06:27 +00:00
Node _loadedNode;
public Node GetLoadedNode()
{
return _loadedNode;
}
2024-05-12 17:03:20 +00:00
bool _loading = false;
int _id = -1;
List<int> _cached = new List<int>();
2025-01-08 18:46:17 +00:00
protected override void _OnTrigger()
2024-05-12 17:03:20 +00:00
{
if ( _loading )
{
_cached.Add( DispatchStart() );
return;
}
_loading = true;
_id = DispatchStart();
var errorCode = ResourceLoader.LoadThreadedRequest( scenePath );
if ( Error.Ok != errorCode )
{
_cached.Add( _id );
_id = -1;
_loading = false;
}
}
2026-02-26 14:06:27 +00:00
2024-05-12 17:03:20 +00:00
public override void _Process ( double delta )
{
if ( ! _loading )
{
return;
}
var status = ResourceLoader.LoadThreadedGetStatus( scenePath );
if ( ResourceLoader.ThreadLoadStatus.Loaded != status )
{
return;
}
_loading = false;
var packedScene = (PackedScene) ResourceLoader.LoadThreadedGet( scenePath );
var node = packedScene.Instantiate();
2026-02-26 14:06:27 +00:00
_loadedNode = node;
target.AddChild( node );
node.Owner = target.Owner;
if ( node is Node3D n3D )
{
if ( poseParent != null )
{
Pose.From( poseParent ).Set( n3D );
}
n3D.GlobalPosition = n3D.GlobalPosition + offsetTranslation;
n3D.SetGlobalQuaternion( n3D.GlobalQuaternion() * offsetRotation );
}
2025-01-08 18:46:17 +00:00
Action.Trigger( onLoaded );
2026-02-26 14:06:27 +00:00
_loadedNode = null;
2024-05-12 17:03:20 +00:00
DispatchEnd( _id );
_id = -1;
_cached.ForEach(
( c )=>
{
target.AddChild( packedScene.Instantiate() );
2025-01-08 18:46:17 +00:00
Action.Trigger( onLoaded );
2024-05-12 17:03:20 +00:00
DispatchEnd( c );
}
);
_cached.Clear();
}
}
}