Traveller/scripts/loader/sceneLoader/GameSceneLoader.cs
Cold-Mint 9352f2c32b
No longer throws all items after the player dies, and recreates the map after the player dies.
玩家死亡后不再扔出所有物品,在玩家死亡后重新创建地图。
2024-10-17 16:08:21 +08:00

139 lines
5.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Threading.Tasks;
using ColdMint.scripts.inventory;
using ColdMint.scripts.map;
using ColdMint.scripts.map.events;
using ColdMint.scripts.map.LayoutParsingStrategy;
using ColdMint.scripts.map.layoutStrategy;
using ColdMint.scripts.map.miniMap;
using ColdMint.scripts.map.RoomPlacer;
using ColdMint.scripts.utils;
using Godot;
namespace ColdMint.scripts.loader.sceneLoader;
/// <summary>
/// <para>Game scene loader</para>
/// <para>游戏场景加载器</para>
/// </summary>
public partial class GameSceneLoader : SceneLoaderTemplate
{
private Label? _seedLabel;
public override Task InitializeData()
{
RenderingServer.SetDefaultClearColor(Color.FromHsv(0, 0, 0));
//Loading the blood bar scene
//加载血条场景
var healthBarUi = GetNode<HealthBarUi>("CanvasLayer/Control/VBoxContainer/HealthBarUi");
GameSceneDepend.HealthBarUi = healthBarUi;
//Load HotBar
//加载HotBar
var hotBar = GetNode<HotBar>("CanvasLayer/Control/VBoxContainer/HotBar");
GameSceneDepend.HotBar = hotBar;
//Backpack Ui container
//背包Ui容器
var backpackUiContainer = GetNode<UiGroup>("CanvasLayer/DynamicUiGroup");
GameSceneDepend.DynamicUiGroup = backpackUiContainer;
//Loaded weapon container
//加载武器容器
var weaponContainer = GetNode<Node2D>("WeaponContainer");
GameSceneDepend.WeaponContainer = weaponContainer;
//Load projectile container
//加载抛射体容器
var projectileContainer = GetNode<Node2D>("ProjectileContainer");
GameSceneDepend.ProjectileContainer = projectileContainer;
//Load magic container
//加载魔术容器
var magicContainer = GetNode<Node2D>("SpellContainer");
GameSceneDepend.SpellContainer = magicContainer;
//Load Packsack container
//加载背包容器
var packsackContainer = GetNode<Node2D>("PacksackContainer");
GameSceneDepend.PacksackContainer = packsackContainer;
//Load AICharacter container
//加载AICharacter容器
var aiCharacterContainer = GetNode<Node2D>("AICharacterContainer");
GameSceneDepend.AiCharacterContainer = aiCharacterContainer;
//Load player container
//加载玩家容器
var playerContainer = GetNode<Node2D>("PlayerContainer");
GameSceneDepend.PlayerContainer = playerContainer;
//Load the pickable container
//加载可拾捡物容器
var pickAbleContainer = GetNode<Node2D>("PickAbleContainer");
GameSceneDepend.PickAbleContainer = pickAbleContainer;
//Setting up the mini map
//设置迷你地图
var miniMap = GetNode<MiniMap>("CanvasLayer/Control/MapContainer/Control/MiniMap");
GameSceneDepend.MiniMap = miniMap;
//Set the mini map animation
//设置迷你地图动画
var miniMapAnimationPlayer = GetNode<AnimationPlayer>("CanvasLayer/Control/MapContainer/MiniMapAnimationPlayer");
GameSceneDepend.MiniMapAnimationPlayer = miniMapAnimationPlayer;
return Task.CompletedTask;
}
public override async Task LoadScene()
{
var debugMode = Config.IsDebug();
var recreateMapButton = GetNodeOrNull<Button>("CanvasLayer/Control/RecreateMapButton");
if (recreateMapButton != null)
{
recreateMapButton.Visible = debugMode;
recreateMapButton.Pressed += () => { _ = GenerateMap(); };
}
_seedLabel = GetNodeOrNull<Label>("CanvasLayer/Control/SeedLabel");
if (_seedLabel != null)
{
_seedLabel.Visible = debugMode;
}
MapGenerator.MapRoot = GetNode<Node>("MapRoot");
MapGenerator.LayoutStrategy = new TestLayoutStrategy();
MapGenerator.LayoutParsingStrategy = new SequenceLayoutParsingStrategy();
MapGenerator.RoomPlacementStrategy = new PatchworkRoomPlacementStrategy();
EventBus.GameOverEvent += OnGameOverEvent;
await GenerateMap();
}
private async void OnGameOverEvent(GameOverEvent gameOverEvent)
{
if (GameSceneDepend.WeaponContainer != null)
{
NodeUtils.DeleteAllChild(GameSceneDepend.WeaponContainer);
}
if (GameSceneDepend.PacksackContainer != null)
{
NodeUtils.DeleteAllChild(GameSceneDepend.PacksackContainer);
}
if (GameSceneDepend.SpellContainer != null)
{
NodeUtils.DeleteAllChild(GameSceneDepend.SpellContainer);
}
await GenerateMap();
}
public override void _ExitTree()
{
EventBus.GameOverEvent -= OnGameOverEvent;
}
/// <summary>
/// <para>Generate map</para>
/// <para>生成地图</para>
/// </summary>
private async Task GenerateMap()
{
MapGenerator.Seed = GuidUtils.GetGuid();
if (_seedLabel != null)
{
//If you have a seedLabel, then set the seed to it.
//如果有seedLabel那么将种子设置上去。
var seedInfo = TranslationServerUtils.TranslateWithFormat("ui_seed_info", MapGenerator.Seed);
_seedLabel.Text = seedInfo ?? $"Seed: {MapGenerator.Seed}";
}
await MapGenerator.GenerateMap();
}
}