Add ResourceLoader

This commit is contained in:
wjsjwr 2024-11-29 22:56:50 +08:00
parent 5b26158acf
commit bd0f367687
8 changed files with 618 additions and 19 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"cSpell.words": [
"tscn"
]
}

26
loader.tscn Normal file
View File

@ -0,0 +1,26 @@
[gd_scene load_steps=2 format=3 uid="uid://c7twiu4wplofj"]
[ext_resource type="Script" path="res://scripts/Loader.cs" id="1_leg4d"]
[node name="Loader" type="Control"]
layout_mode = 3
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("1_leg4d")
[node name="ProgressBar" type="ProgressBar" parent="."]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -863.0
offset_top = -37.5
offset_right = 863.0
offset_bottom = 37.5
grow_horizontal = 2
grow_vertical = 2

View File

@ -11,7 +11,7 @@ config_version=5
[application] [application]
config/name="导师模拟器" config/name="导师模拟器"
run/main_scene="res://lab.tscn" run/main_scene="res://loader.tscn"
config/features=PackedStringArray("4.3", "C#", "GL Compatibility") config/features=PackedStringArray("4.3", "C#", "GL Compatibility")
config/icon="res://icon.svg" config/icon="res://icon.svg"
@ -19,8 +19,8 @@ config/icon="res://icon.svg"
GameManager="*res://game_manager.tscn" GameManager="*res://game_manager.tscn"
OneSecondTicker="*res://game_manager.tscn" OneSecondTicker="*res://game_manager.tscn"
Player="*res://scripts/Player.cs"
Dialogic="*res://addons/dialogic/Core/DialogicGameHandler.gd" Dialogic="*res://addons/dialogic/Core/DialogicGameHandler.gd"
SceneTransit="*res://scene_transit.tscn"
[dialogic] [dialogic]

54
scene_transit.tscn Normal file
View File

@ -0,0 +1,54 @@
[gd_scene load_steps=5 format=3 uid="uid://xnssep8q3gm5"]
[ext_resource type="Script" path="res://scripts/SceneTransit.cs" id="1_138ac"]
[sub_resource type="Animation" id="Animation_n35b4"]
resource_name = "transit"
length = 0.3
step = 0.3
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("ColorRect:color")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.3),
"transitions": PackedFloat32Array(1, 1),
"update": 0,
"values": [Color(0, 0, 0, 0), Color(0, 0, 0, 1)]
}
[sub_resource type="Animation" id="Animation_6aqtw"]
length = 0.001
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("ColorRect:color")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0),
"transitions": PackedFloat32Array(1),
"update": 0,
"values": [Color(0, 0, 0, 0)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_g0ecv"]
_data = {
"RESET": SubResource("Animation_6aqtw"),
"transit": SubResource("Animation_n35b4")
}
[node name="CanvasLayer" type="CanvasLayer"]
script = ExtResource("1_138ac")
[node name="ColorRect" type="ColorRect" parent="."]
offset_right = 1920.0
offset_bottom = 1080.0
color = Color(0, 0, 0, 0)
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
libraries = {
"": SubResource("AnimationLibrary_g0ecv")
}

View File

@ -7,6 +7,7 @@ public partial class GameManager : Node
/// Indicates if the game is currently in tutorial mode. /// Indicates if the game is currently in tutorial mode.
/// </summary> /// </summary>
public static bool IsTutorial { get; private set; } public static bool IsTutorial { get; private set; }
public static string NextScene { get; set; } = null;
public static readonly Resource Arrow2x = ResourceLoader.Load("res://temp_res/kenney_ui-pack-space-expansion/PNG/Extra/Double/cursor_f.png"); public static readonly Resource Arrow2x = ResourceLoader.Load("res://temp_res/kenney_ui-pack-space-expansion/PNG/Extra/Double/cursor_f.png");
// Called when the node enters the scene tree for the first time. // Called when the node enters the scene tree for the first time.
public override void _Ready() public override void _Ready()

48
scripts/Loader.cs Normal file
View File

@ -0,0 +1,48 @@
using Godot;
using System;
public partial class Loader : Control
{
private ProgressBar progressBar;
private int sceneLoaded = 0;
private string[] scenesToLoad = {
"res://lab.tscn",
"res://player.tscn"
};
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
progressBar = GetNode<ProgressBar>("ProgressBar");
string nextScene = GameManager.NextScene;
if (nextScene != null) {
GameManager.NextScene = "res://lab.tscn";
}
ResourceLoader.LoadThreadedRequest(scenesToLoad[sceneLoaded]);
progressBar.MaxValue = 100 * scenesToLoad.Length;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
Godot.Collections.Array progress = new();
switch(ResourceLoader.LoadThreadedGetStatus(scenesToLoad[sceneLoaded], progress))
{
case ResourceLoader.ThreadLoadStatus.InProgress:
progressBar.Value = 100 * sceneLoaded + (int)((float)progress[0] * 100);
break;
case ResourceLoader.ThreadLoadStatus.Loaded:
sceneLoaded++;
if(sceneLoaded < scenesToLoad.Length) {
ResourceLoader.LoadThreadedRequest(scenesToLoad[sceneLoaded]);
} else {
var transit = GetNode<SceneTransit>("/root/SceneTransit");
transit.Transit("res://lab.tscn", false);
}
break;
default:
GD.Print("Error loading scene: " + scenesToLoad[sceneLoaded]);
break;
}
}
}

37
scripts/SceneTransit.cs Normal file
View File

@ -0,0 +1,37 @@
using Godot;
using System;
public partial class SceneTransit : CanvasLayer
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
Layer = -1;
Hide();
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
public async void Transit(string nextScene, bool needLoadResources = true)
{
GameManager.NextScene = nextScene;
var ap = GetNode<AnimationPlayer>("AnimationPlayer");
Layer = 9999;
Show();
ap.Play("transit");
await ToSignal(ap, AnimationMixer.SignalName.AnimationFinished);
if (needLoadResources) {
GetTree().ChangeSceneToFile("res://loader.tscn");
} else {
GetTree().ChangeSceneToFile(nextScene);
}
ap.PlayBackwards("transit");
await ToSignal(ap, AnimationMixer.SignalName.AnimationFinished);
Layer = -1;
Hide();
}
}

File diff suppressed because one or more lines are too long