Rework loading maps to be asynchronous, change debug menu so that its not completely pausing the game
This commit is contained in:
@@ -3,24 +3,20 @@ using Chickensoft.Collections;
|
||||
using Chickensoft.GodotNodeInterfaces;
|
||||
using Chickensoft.SaveFileBuilder;
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Threading.Tasks;
|
||||
using Zennysoft.Ma.Adapter;
|
||||
|
||||
namespace Zennysoft.Game.Ma;
|
||||
|
||||
public interface IMap : INode3D, IProvide<ISaveChunk<MapData>>
|
||||
{
|
||||
void LoadMap();
|
||||
|
||||
void LoadFloor();
|
||||
Task LoadFloor();
|
||||
|
||||
ImmutableDictionary<Floor, string> FloorScenes { get; }
|
||||
|
||||
IDungeonFloor CurrentFloor { get; }
|
||||
|
||||
void SpawnNextFloor();
|
||||
|
||||
Transform3D GetPlayerSpawnPosition();
|
||||
|
||||
IDungeonRoom GetPlayersCurrentRoom();
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
using Chickensoft.AutoInject;
|
||||
using Chickensoft.Collections;
|
||||
using Chickensoft.Introspection;
|
||||
using Chickensoft.LogicBlocks;
|
||||
using Chickensoft.SaveFileBuilder;
|
||||
using Godot;
|
||||
using System.Collections.Generic;
|
||||
using Godot.Collections;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Zennysoft.Game.Implementation;
|
||||
using Zennysoft.Ma.Adapter;
|
||||
|
||||
namespace Zennysoft.Game.Ma;
|
||||
@@ -27,12 +28,15 @@ public partial class Map : Node3D, IMap
|
||||
|
||||
ISaveChunk<MapData> IProvide<ISaveChunk<MapData>>.Value() => MapChunk;
|
||||
|
||||
[Node]
|
||||
public AnimationPlayer AnimationPlayer { get; set; } = default!;
|
||||
|
||||
[Dependency]
|
||||
public ISaveChunk<GameData> GameChunk => this.DependOn<ISaveChunk<GameData>>();
|
||||
#endregion
|
||||
|
||||
[Export]
|
||||
private Godot.Collections.Array<Floor> Floors { get; set; } = default!;
|
||||
private Array<Floor> Floors { get; set; } = default!;
|
||||
|
||||
public ImmutableDictionary<Floor, string> FloorScenes { get; private set; }
|
||||
|
||||
@@ -51,7 +55,6 @@ public partial class Map : Node3D, IMap
|
||||
|
||||
public void InitializeMapData()
|
||||
{
|
||||
ClearMap();
|
||||
FloorScenes = ImmutableDictionary<Floor, string>.Empty
|
||||
.Add(Floor.Overworld, $"res://src/map/overworld/Overworld.tscn")
|
||||
.Add(Floor.Altar, $"res://src/map/dungeon/floors/Floor00.tscn")
|
||||
@@ -68,31 +71,6 @@ public partial class Map : Node3D, IMap
|
||||
CurrentFloorNumber.OnNext(0);
|
||||
}
|
||||
|
||||
public void LoadMap()
|
||||
{
|
||||
LoadFloor();
|
||||
CurrentFloor.InitializeDungeon();
|
||||
var transform = GetPlayerSpawnPosition();
|
||||
Player.TeleportPlayer(transform);
|
||||
CurrentFloor.FloorIsLoaded = true;
|
||||
}
|
||||
|
||||
private void ClearMap()
|
||||
{
|
||||
CurrentFloor?.CallDeferred(MethodName.QueueFree, []);
|
||||
}
|
||||
|
||||
public void SpawnNextFloor()
|
||||
{
|
||||
ClearMap();
|
||||
LoadFloor();
|
||||
CurrentFloor.InitializeDungeon();
|
||||
var transform = GetPlayerSpawnPosition();
|
||||
Player.TeleportPlayer(transform);
|
||||
CurrentFloor.FloorIsLoaded = true;
|
||||
CurrentFloorNumber.OnNext(CurrentFloorNumber.Value + 1);
|
||||
}
|
||||
|
||||
public IDungeonRoom GetPlayersCurrentRoom()
|
||||
{
|
||||
var rooms = CurrentFloor.Rooms;
|
||||
@@ -102,15 +80,33 @@ public partial class Map : Node3D, IMap
|
||||
|
||||
public Transform3D GetPlayerSpawnPosition() => CurrentFloor.GetPlayerSpawnPoint();
|
||||
|
||||
public void LoadFloor()
|
||||
public async Task LoadFloor()
|
||||
{
|
||||
ClearMap();
|
||||
FloorScenes.TryGetValue(Floors.First(), out var currentFloorScene);
|
||||
var instantiator = new Instantiator(GetTree());
|
||||
var loadedScene = instantiator.LoadAndInstantiate<Node3D>(currentFloorScene);
|
||||
AddChild(loadedScene);
|
||||
CurrentFloor = (IDungeonFloor)loadedScene;
|
||||
AnimationPlayer.CallDeferred(AnimationPlayer.MethodName.Play, "fade_out");
|
||||
FloorScenes.TryGetValue(Floors.First(), out var sceneToLoad);
|
||||
ClearCurrentMap();
|
||||
var sceneLoader = new SceneLoader();
|
||||
AddChild(sceneLoader);
|
||||
sceneLoader.LoadSceneRequest(sceneToLoad);
|
||||
await ToSignal(sceneLoader, SceneLoader.SignalName.SceneLoaded);
|
||||
AddChild(sceneLoader.LoadedScene);
|
||||
CurrentFloor = (IDungeonFloor)sceneLoader.LoadedScene;
|
||||
SetupDungeonFloor();
|
||||
CurrentFloor.FloorIsLoaded = true;
|
||||
CurrentFloorNumber.OnNext(CurrentFloorNumber.Value + 1);
|
||||
sceneLoader.QueueFree();
|
||||
AnimationPlayer.CallDeferred(AnimationPlayer.MethodName.Play, ("fade_in"));
|
||||
}
|
||||
|
||||
private void ClearCurrentMap()
|
||||
{
|
||||
CurrentFloor?.CallDeferred(MethodName.QueueFree, []);
|
||||
Floors.Remove(Floors.First());
|
||||
}
|
||||
|
||||
private void SetupDungeonFloor()
|
||||
{
|
||||
CurrentFloor.InitializeDungeon();
|
||||
var transform = GetPlayerSpawnPosition();
|
||||
Player.TeleportPlayer(transform);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,73 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://by67pn7fdsg1m"]
|
||||
[gd_scene load_steps=6 format=3 uid="uid://by67pn7fdsg1m"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://14e8mu48ed4" path="res://src/map/Map.cs" id="1_bw70o"]
|
||||
|
||||
[sub_resource type="Animation" id="Animation_g6eui"]
|
||||
resource_name = "fade_in"
|
||||
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, 1),
|
||||
"transitions": PackedFloat32Array(1, 1),
|
||||
"update": 0,
|
||||
"values": [Color(0, 0, 0, 1), Color(0, 0, 0, 0)]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id="Animation_00xd7"]
|
||||
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, 1)]
|
||||
}
|
||||
|
||||
[sub_resource type="Animation" id="Animation_v14r0"]
|
||||
resource_name = "fade_out"
|
||||
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.0666667, 0),
|
||||
"transitions": PackedFloat32Array(1, 1),
|
||||
"update": 0,
|
||||
"values": [Color(0, 0, 0, 1), Color(0, 0, 0, 1)]
|
||||
}
|
||||
|
||||
[sub_resource type="AnimationLibrary" id="AnimationLibrary_00xd7"]
|
||||
_data = {
|
||||
&"RESET": SubResource("Animation_00xd7"),
|
||||
&"fade_in": SubResource("Animation_g6eui"),
|
||||
&"fade_out": SubResource("Animation_v14r0")
|
||||
}
|
||||
|
||||
[node name="Map" type="Node3D"]
|
||||
script = ExtResource("1_bw70o")
|
||||
Floors = Array[int]([3, 2, 0, 4, 1, 5, 6])
|
||||
|
||||
[node name="ColorRect" type="ColorRect" parent="."]
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
color = Color(0, 0, 0, 1)
|
||||
|
||||
[node name="AnimationPlayer" type="AnimationPlayer" parent="."]
|
||||
unique_name_in_owner = true
|
||||
libraries = {
|
||||
&"": SubResource("AnimationLibrary_00xd7")
|
||||
}
|
||||
|
||||
1
Zennysoft.Game.Ma/src/map/SceneLoader.cs.uid
Normal file
1
Zennysoft.Game.Ma/src/map/SceneLoader.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bftr18a1c2gll
|
||||
Reference in New Issue
Block a user