♻️ Better miner tile

This commit is contained in:
2025-08-29 18:05:30 +08:00
parent 60b6d6f989
commit ac1d8cfab9
3 changed files with 27 additions and 8 deletions

View File

@@ -51,7 +51,7 @@ public partial class GridManager : Node
}
}
public Node2D? GetBuildingAtCell(Vector2I cell, GridLayer layer = GridLayer.Building)
public Node2D? GetTileAtCell(Vector2I cell, GridLayer layer = GridLayer.Building)
{
return _layers[layer].TryGetValue(cell, out var data) ? data.Building : null;
}

View File

@@ -229,7 +229,7 @@ public partial class PlacementManager : Node2D
// Check if the area is occupied by under-construction tiles
var occupiedCells = GridUtils.GetOccupiedCells(_hoveredCell, building.Size, _currentRotation);
var isUnderConstruction = occupiedCells.Any(cell =>
Grid.GetBuildingAtCell(cell, building.Layer) is BaseTile { IsConstructing: true });
Grid.GetTileAtCell(cell, building.Layer) is BaseTile { IsConstructing: true });
if (!isUnderConstruction)
_cannotDeploySound.Play();
@@ -289,7 +289,7 @@ public partial class PlacementManager : Node2D
!Grid.IsAreaFree(_hoveredCell, Vector2I.One, 0f))
{
// Right click to destroy from current layer
var building = Grid.GetBuildingAtCell(_hoveredCell);
var building = Grid.GetTileAtCell(_hoveredCell);
if (building == null) return;
// Find all cells occupied by this building
@@ -426,7 +426,7 @@ public static class GridManagerExtensions
public static (Vector2I Position, Vector2I Size, float Rotation)? GetBuildingInfoAtCell(this GridManager grid,
Vector2I cell, GridLayer layer)
{
if (grid.GetBuildingAtCell(cell, layer) is { } building)
if (grid.GetTileAtCell(cell, layer) is { } building)
{
// Find the top-left position of the building
for (int x = 0; x < 100; x++) // Arbitrary max size
@@ -434,16 +434,16 @@ public static class GridManagerExtensions
for (int y = 0; y < 100; y++)
{
var checkCell = new Vector2I(cell.X - x, cell.Y - y);
if (grid.GetBuildingAtCell(checkCell, layer) == building)
if (grid.GetTileAtCell(checkCell, layer) == building)
{
// Found the top-left corner, now find the size
var size = Vector2I.One;
// Search right
while (grid.GetBuildingAtCell(new Vector2I(checkCell.X + size.X, checkCell.Y), layer) ==
while (grid.GetTileAtCell(new Vector2I(checkCell.X + size.X, checkCell.Y), layer) ==
building)
size.X++;
// Search down
while (grid.GetBuildingAtCell(new Vector2I(checkCell.X, checkCell.Y + size.Y), layer) ==
while (grid.GetTileAtCell(new Vector2I(checkCell.X, checkCell.Y + size.Y), layer) ==
building)
size.Y++;

View File

@@ -6,7 +6,7 @@ namespace AceFieldNewHorizon.Scripts.Tiles;
public partial class MinerTile : BaseTile
{
[Export] public PackedScene ItemPickup { get; set; }
[Export] public string ItemToMine { get; set; } = "OreIron";
[Export] public string ItemToMine { get; set; }
[Export] public int MiningRate = 1; // Items per second
private Vector2I _gridPosition;
@@ -16,10 +16,29 @@ public partial class MinerTile : BaseTile
{
base._Ready();
_gridPosition = GridUtils.WorldToGrid(Position);
var ground = Grid.GetTileAtCell(_gridPosition, GridLayer.Ground) as BaseTile;
if (ground == null)
{
GD.Print($"[Miner] Miner {GetInstanceId()} not found available resource...");
return;
}
ItemToMine = (ground.TileId) switch
{
"stone" => "stone",
"ore_iron" => "ore_iron",
_ => null
};
if (ItemToMine == null)
GD.Print($"[Miner] Miner {GetInstanceId()} not found available resource...");
}
public override void _Process(double delta)
{
if (ItemToMine == null)
return;
// Don't mine if building is not completed
if (!IsConstructed || ItemPickup == null)
return;