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;