Reactor, and enemies attacking the tiles

This commit is contained in:
2025-08-31 14:30:18 +08:00
parent c72353716f
commit 09511b37c9
17 changed files with 517 additions and 222 deletions

View File

@@ -8,11 +8,20 @@ public partial class Hud : CanvasLayer
private ResourceManager _resourceManager;
private VBoxContainer _resourceDisplay;
private readonly Dictionary<string, Label> _resourceLabels = new();
private VBoxContainer _pickupLogContainer;
public override void _Ready()
{
_resourceDisplay = GetNode<VBoxContainer>("ResourceDisplay");
_pickupLogContainer = new VBoxContainer();
_pickupLogContainer.Name = "PickupLogContainer";
_pickupLogContainer.SetAnchorsPreset(Control.LayoutPreset.BottomLeft);
_pickupLogContainer.OffsetLeft = 10;
_pickupLogContainer.OffsetBottom = -10; // Negative offset from bottom anchor
_pickupLogContainer.GrowVertical = Control.GrowDirection.Begin; // Make it grow upwards
AddChild(_pickupLogContainer);
_resourceManager = DependencyInjection.Container.GetInstance<ResourceManager>();
if (_resourceManager == null)
{
@@ -21,6 +30,7 @@ public partial class Hud : CanvasLayer
}
_resourceManager.OnResourceChanged += UpdateResourceDisplay;
_resourceManager.OnResourcePickedUp += OnResourcePickedUp;
// Initialize display with current resources
foreach (var entry in _resourceManager.GetAllResources())
@@ -54,11 +64,39 @@ public partial class Hud : CanvasLayer
}
}
private void OnResourcePickedUp(string resourceId, int amount)
{
AddPickupLogEntry($"Picked up {amount} {resourceId}!");
}
private void AddPickupLogEntry(string message)
{
var logLabel = new Label();
logLabel.Text = message;
_pickupLogContainer.AddChild(logLabel);
var timer = new Timer();
timer.WaitTime = 3.0f;
timer.OneShot = true;
timer.Autostart = true;
logLabel.AddChild(timer); // Add timer as child of the label
timer.Timeout += () => OnLogEntryTimeout(logLabel);
}
private void OnLogEntryTimeout(Label logLabel)
{
// Start fading out the label
var tween = logLabel.CreateTween();
tween.TweenProperty(logLabel, "modulate", new Color(1, 1, 1, 0), 0.5f); // Fade out over 0.5 seconds
tween.TweenCallback(Callable.From(() => logLabel.QueueFree())); // Free after fading
}
public override void _ExitTree()
{
if (_resourceManager != null)
{
_resourceManager.OnResourceChanged -= UpdateResourceDisplay;
_resourceManager.OnResourcePickedUp -= OnResourcePickedUp;
}
}
}

View File

@@ -67,6 +67,9 @@ public partial class NaturalResourceGenerator : Node2D
}
GD.Print($"{LogPrefix} NaturalResourceGenerator ready, SpawnEnemyNest = {SpawnEnemyNest}");
GD.Print($"{LogPrefix} Spawning the core reactor...");
SpawnCoreReactor();
if (SpawnEnemyNest)
{
@@ -393,6 +396,12 @@ public partial class NaturalResourceGenerator : Node2D
GD.Print($"{LogPrefix} Finished placing vein - placed {placedCount}/{maxSize} {tileType} tiles");
}
private void SpawnCoreReactor()
{
// Place the reactor tile at the center of the map
PlaceTile("reactor", new Vector2I(0, 0));
}
private void SpawnRandomEnemyNest()
{
// Generate a random position within the specified distance from origin
@@ -402,7 +411,7 @@ public partial class NaturalResourceGenerator : Node2D
var nestPosition = new Vector2I((int)offset.X, (int)offset.Y);
// Try to find a valid position for the nest
int attempts = 0;
var attempts = 0;
const int maxAttempts = 10;
while (attempts < maxAttempts)
@@ -428,25 +437,18 @@ public partial class NaturalResourceGenerator : Node2D
{
try
{
// For enemy nest, we want to place it on top of existing ground
if (tileType != "enemy_nest")
{
// Original behavior for other tile types
Grid.FreeArea(cell, Vector2I.One, 0f, GridLayer.Ground);
}
else
{
// For enemy nest, check if there's ground below (skip for now)
GD.Print($"{LogPrefix} Attempting placing nest at {cell}.");
}
var building = Registry.GetBuilding(tileType);
if (building == null)
{
GD.PrintErr($"{LogPrefix} Building type not found in registry: {tileType}");
return false;
}
// GD.Print($"{LogPrefix} Found building in registry: {tileType}");
// Free area for ground layer if needed
if (building.Layer == GridLayer.Ground)
Grid.FreeArea(cell, building.Size, 0f, GridLayer.Ground);
else
GD.Print($"{LogPrefix} Attempting placing building tile {tileType} at {cell}.");
var scene = building.Scene;
if (scene == null)
@@ -454,29 +456,24 @@ public partial class NaturalResourceGenerator : Node2D
GD.PrintErr($"{LogPrefix} Scene is null for building type: {tileType}");
return false;
}
// GD.Print($"{LogPrefix} Scene loaded for {tileType}");
if (scene.Instantiate() is not BaseTile instance)
{
GD.PrintErr($"{LogPrefix} Failed to instantiate scene for: {tileType}");
return false;
}
// GD.Print($"{LogPrefix} Successfully instantiated {tileType}");
// Use the same positioning logic as PlacementManager
// Calculate position with proper offset based on building size
var rotatedSize = building.GetRotatedSize(0f);
var offset = GridUtils.GetCenterOffset(rotatedSize, 0f);
instance.ZIndex = (int)building.Layer;
instance.GlobalPosition = GridUtils.GridToWorld(cell) + offset;
instance.Grid = Grid;
AddChild(instance);
// For enemy nest, use Building layer
var layer = tileType == "enemy_nest" ? GridLayer.Building : building.Layer;
Grid.OccupyArea(cell, instance, building.Size, 0f, layer);
// Occupy the appropriate area based on building size
Grid.OccupyArea(cell, instance, building.Size, 0f, building.Layer);
// GD.Print($"{LogPrefix} Successfully placed {tileType} at {cell} on layer {layer}");
return true;
}
catch (Exception e)

View File

@@ -203,7 +203,6 @@ public partial class PlacementManager : Node2D
var scene = building.Scene;
_ghostBuilding = (BaseTile)scene.Instantiate();
_ghostBuilding.Grid = Grid;
_ghostBuilding.SetGhostMode(true);
_ghostBuilding.RotationDegrees = _currentRotation;
_ghostBuilding.ZAsRelative = false;
@@ -251,7 +250,6 @@ public partial class PlacementManager : Node2D
// Create the building instance
var scene = building.Scene;
var buildingInstance = (BaseTile)scene.Instantiate();
buildingInstance.Grid = Grid;
buildingInstance.RotationDegrees = _currentRotation;
buildingInstance.ZIndex = (int)building.Layer;
buildingInstance.Position = _ghostBuilding.Position;

View File

@@ -13,6 +13,10 @@ public partial class ResourceManager : Node
// Event for when resource amounts change
[Signal]
public delegate void OnResourceChangedEventHandler(string resourceId, int newAmount);
// Event for when resources are picked up (added)
[Signal]
public delegate void OnResourcePickedUpEventHandler(string resourceId, int amount);
public override void _Ready()
{
@@ -43,6 +47,7 @@ public partial class ResourceManager : Node
}
EmitSignal(nameof(OnResourceChanged), resourceId, _resources[resourceId]);
EmitSignal(nameof(OnResourcePickedUp), resourceId, amount);
}
// Remove resources of a specific type