Map start system

This commit is contained in:
Marco 2025-02-21 11:39:22 +01:00
commit 7acc344986
11 changed files with 323 additions and 41 deletions

View file

@ -23,15 +23,18 @@ public partial class GameManager : Node2D
[Export] public PackedScene PlayerTemplate { get; set; }
[Export] public Marker2D PlayerSpawnMarker { get; set; }
[Export] public Dictionary<int, NodePath> SpawnMarkers { get; private set; } = new();
//[Export] public Marker2D PlayerSpawnMarker { get; set; }
[Export] public PackedScene WeaponTemplate { get; private set; }
[Export] public Array<LootItem> StartingEquipment { get; private set; }
[Export] public Array<LootItem> StartingEquipment { get; private set; } = new();
private InventoryManager _inventoryManager { get; set; }
public MapStartData MapStartData { get; set; }
[Export]
public MapStartDataResource MapStartData { get; private set; }
//private AlarmManager _alarmManager { get; set; }
@ -48,7 +51,7 @@ public partial class GameManager : Node2D
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
Instance = this;
Instance = this;
_hud = GetNodeOrNull<Hud>("HUD");
if (_hud == null) GD.Print("No HUD in scene.");
@ -81,6 +84,15 @@ public partial class GameManager : Node2D
CallDeferred(MethodName.DelayPlayerSpawn);
}
public void ApplyMapStartData(MapStartDataResource mapStartData)
{
MapStartData = mapStartData;
StartingEquipment.AddRange(mapStartData.StartingEquipment);
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
@ -94,7 +106,7 @@ public partial class GameManager : Node2D
{
//await Task.Delay(500);
if (PlayerSpawnMarker != null)
if (SpawnMarkers.Any())
{
SpawnPlayer();
}
@ -141,13 +153,16 @@ public partial class GameManager : Node2D
public void SpawnPlayer()
{
if (_player != null) return;
//_player = this.CreateChild<PlayerMovement>(PlayerTemplate, PlayerSpawnMarker.Position );
_player = PlayerTemplate.Instantiate<PlayerMovement>();
this.CallDeferred("add_child", _player);
_player.Transform = this.GlobalTransform;
_player.GlobalPosition = PlayerSpawnMarker.Position;
_player.GlobalPosition = GetStartPosition();
//_player.GlobalPosition = PlayerSpawnMarker.Position;
CameraTargetPlayer();
//
@ -158,6 +173,22 @@ public partial class GameManager : Node2D
// }
}
private Vector2 GetStartPosition()
{
if (MapStartData != null)
{
if (SpawnMarkers.TryGetValue(MapStartData.EggIndex, out var spawnMarker))
{
var marker = GetNode<Node2D>(spawnMarker);
return marker.Position; // Why position and not globalposition? I have no idea
}
}
var m = GetNode<Node2D>(SpawnMarkers.First().Value);
return m.GlobalPosition;
}
public void CameraTargetPlayer()
{
if (_player is null) return;

View file

@ -1,3 +1,4 @@
using Cirno.Scripts.Resources;
using Godot;
public partial class GlobalState : Node
@ -13,8 +14,6 @@ public partial class GlobalState : Node
Viewport root = GetTree().Root;
// Using a negative index counts from the end, so this gets the last child node of `root`.
CurrentScene = root.GetChild(-1);
}
public void GotoScene(string path)
@ -31,7 +30,13 @@ public partial class GlobalState : Node
CallDeferred(MethodName.DeferredGotoScene, path);
}
public void DeferredGotoScene(string path)
public void GoToScene(string path, MapStartDataResource startData)
{
// TODO: Implement startdata usage
CallDeferred(MethodName.DeferredGotoScene, path, startData);
}
private void DeferredGotoScene(string path, MapStartDataResource startData = null)
{
// It is now safe to remove the current scene.
CurrentScene.Free();
@ -47,11 +52,16 @@ public partial class GlobalState : Node
// Optionally, to make it compatible with the SceneTree.change_scene_to_file() API.
GetTree().CurrentScene = CurrentScene;
if (startData is not null)
{
// Call deferred if it gives issues
DeferredAddStartDataToGameManager(startData);
}
}
}
public class MapStartData
{
public int EggIndex = 0;
private void DeferredAddStartDataToGameManager(MapStartDataResource resource)
{
GameManager.Instance.ApplyMapStartData(resource);
}
}

View file

@ -59,7 +59,8 @@ public partial class MainMenu : Control
private void _on_mainmenu_button_pressed()
{
if (MainMenuScene != null) {
GetTree().ChangeSceneToFile(MainMenuScene);
GlobalState.Instance.GotoScene(MainMenuScene);
//GetTree().ChangeSceneToFile(MainMenuScene);
}
}

View file

@ -0,0 +1,11 @@
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Resources.DebugMenu;
[GlobalClass]
public partial class DebugMapSelectData : Resource
{
[Export]
public Array<DebugMenu.DebugMapSelectResource> Maps { get; private set; }
}

View file

@ -0,0 +1,35 @@
using Godot;
namespace Cirno.Scripts.Resources.DebugMenu;
[GlobalClass]
public partial class DebugMapSelectResource : Resource
{
[Export] public bool Enabled { get; private set; } = true;
[Export] public string Path { get; private set; }
[Export] private string _name { get; set; }
[Export] public MapStartDataResource StartData { get; private set; }
[Export] public Texture2D Icon { get; private set; }
public string DisplayName
{
get
{
if (!string.IsNullOrWhiteSpace(_name))
{
return _name;
}
if (!string.IsNullOrWhiteSpace(Path))
{
return Path.Split("/")[^1].Split(".")[0];
}
return string.Empty;
}
set => _name = value;
}
}

View file

@ -0,0 +1,20 @@
using System.Linq;
using Godot;
using Godot.Collections;
namespace Cirno.Scripts.Resources;
[GlobalClass]
public partial class MapStartDataResource : Resource
{
[Export]
public int EggIndex { get; set; }
[Export]
public Array<LootItem> StartingEquipment { get; set; } = new Array<LootItem>();
public override string ToString()
{
return $"EggIndex: {EggIndex}, Equipment: {string.Join("," ,StartingEquipment.Select(x => x.ItemKey))}";
}
}

View file

@ -1,6 +1,9 @@
using Godot;
using System;
using Cirno.Scripts.Resources;
using Cirno.Scripts.Resources.DebugMenu;
using Godot.Collections;
using DebugMapSelectData = Cirno.Scripts.Resources.DebugMenu.DebugMapSelectData;
public partial class DebugMenu : Control
{
@ -9,7 +12,7 @@ public partial class DebugMenu : Control
public Theme ButtonTheme { get; private set; }
[Export]
public Array<string> Levels { get; set; }
public DebugMapSelectData Levels { get; set; }
[Export]
public Container ButtonsContainer { get; private set; }
@ -20,26 +23,25 @@ public partial class DebugMenu : Control
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
foreach (var level in Levels)
foreach (var level in Levels.Maps)
{
if (!level.Enabled) continue;
var button = new Button();
button.Text = level.Split("/")[^1].Split(".")[0];
//button.Text = level.Split("/")[^1].Split(".")[0];
button.Text = level.DisplayName;
button.Icon = level.Icon;
//button.Text = level;
button.Theme = ButtonTheme;
ButtonsContainer.CallDeferred("add_child", button);
button.Pressed += () => ButtonOnPressed(level);
}
}
private void ButtonOnPressed(string scene)
private void ButtonOnPressed(DebugMapSelectResource scene)
{
GD.Print("Button was pressed, now what");
GlobalState.Instance.GotoScene(scene);
//GetTree().ChangeSceneToFile(scene);
GlobalState.Instance.GoToScene(scene.Path, scene.StartData);
}
private void _on_back_button_pressed()