110 lines
2.6 KiB
C#
110 lines
2.6 KiB
C#
using Chickensoft.AutoInject;
|
|
using Chickensoft.GodotNodeInterfaces;
|
|
using Chickensoft.Introspection;
|
|
using Chickensoft.SaveFileBuilder;
|
|
using Godot;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Zennysoft.Ma.Adapter;
|
|
|
|
namespace Zennysoft.Game.Ma;
|
|
|
|
public interface IMap : INode3D, IProvide<ISaveChunk<MapData>>
|
|
{
|
|
public void LoadMap();
|
|
|
|
public List<string> FloorScenes { get; }
|
|
|
|
public IDungeonFloor CurrentFloor { get; }
|
|
|
|
public void SpawnNextFloor();
|
|
|
|
public Transform3D GetPlayerSpawnPosition();
|
|
}
|
|
|
|
|
|
[Meta(typeof(IAutoNode))]
|
|
public partial class Map : Node3D, IMap
|
|
{
|
|
public override void _Notification(int what) => this.Notify(what);
|
|
|
|
[Dependency]
|
|
public IGame Game => this.DependOn<IGame>();
|
|
|
|
[Dependency]
|
|
public IPlayer Player => this.DependOn<IPlayer>();
|
|
|
|
#region Save
|
|
public ISaveChunk<MapData> MapChunk { get; set; } = default!;
|
|
|
|
ISaveChunk<MapData> IProvide<ISaveChunk<MapData>>.Value() => MapChunk;
|
|
|
|
[Dependency]
|
|
public ISaveChunk<GameData> GameChunk => this.DependOn<ISaveChunk<GameData>>();
|
|
#endregion
|
|
|
|
[Export]
|
|
private Godot.Collections.Array<PackedScene> _floors { get; set; } = default!;
|
|
|
|
public List<string> FloorScenes { get; private set; }
|
|
|
|
public IDungeonFloor CurrentFloor { get; private set; }
|
|
|
|
public void OnResolved()
|
|
{
|
|
FloorScenes = [];
|
|
|
|
MapChunk = new SaveChunk<MapData>(
|
|
onSave: (chunk) => new MapData()
|
|
{
|
|
FloorScenes = FloorScenes,
|
|
},
|
|
onLoad: (chunk, data) =>
|
|
{
|
|
FloorScenes = data.FloorScenes;
|
|
}
|
|
);
|
|
|
|
GameChunk.AddChunk(MapChunk);
|
|
|
|
this.Provide();
|
|
}
|
|
|
|
public void LoadMap()
|
|
{
|
|
foreach (var floor in _floors)
|
|
FloorScenes.Add(floor.ResourcePath);
|
|
|
|
LoadFloor();
|
|
CurrentFloor.InitializeDungeon();
|
|
var transform = GetPlayerSpawnPosition();
|
|
Player.TeleportPlayer(transform);
|
|
CurrentFloor.FloorIsLoaded = true;
|
|
Game.NextFloorLoaded();
|
|
}
|
|
|
|
public void SpawnNextFloor()
|
|
{
|
|
var oldFloor = CurrentFloor;
|
|
oldFloor.CallDeferred(MethodName.QueueFree, []);
|
|
LoadFloor();
|
|
CurrentFloor.InitializeDungeon();
|
|
var transform = GetPlayerSpawnPosition();
|
|
Player.TeleportPlayer(transform);
|
|
CurrentFloor.FloorIsLoaded = true;
|
|
Game.NextFloorLoaded();
|
|
}
|
|
|
|
public Transform3D GetPlayerSpawnPosition() => CurrentFloor.GetPlayerSpawnPoint();
|
|
|
|
private void LoadFloor()
|
|
{
|
|
var currentFloorScene = FloorScenes.First();
|
|
var instantiator = new Instantiator(GetTree());
|
|
var loadedScene = instantiator.LoadAndInstantiate<Node3D>(currentFloorScene);
|
|
AddChild(loadedScene);
|
|
CurrentFloor = (IDungeonFloor)loadedScene;
|
|
FloorScenes.Remove(currentFloorScene);
|
|
}
|
|
}
|