I’m working on a game in Unity where I have a method ‘FindTopmostPeg’ that finds and removes the topmost peg from a list of positions in a specific quadrant of a grid. Here’s the method:
public GameObject FindTopmostPeg(List<Vector3Int> positions, string quadrant)
{
Vector3Int topmostPosition = new Vector3Int(0, int.MinValue, 0);
GameObject topmostPeg = null;
foreach (Vector3Int position in positions)
{
if (pegInventory.TryGetValue(position, out GameObject peg) && peg != null && position.y > topmostPosition.y)
{
topmostPosition = position;
topmostPeg = peg;
}
}
// Remove the peg from the pegInventory
if (topmostPeg != null)
{
pegInventory[topmostPosition] = null;
lastTopmostPositions[quadrant] = topmostPosition;
}
return topmostPeg;
}
I’m trying to develop a method ‘CapturePegs’ that will return pegs to the topmost empty positions. This method should work similarly to ‘FindTopmostPeg’, but instead of removing a peg from the topmost position, it should place a peg at the topmost empty position.
Here is the ‘CapturePegs’ method I currently have:
public void CapturePegs(GameObject loopPeg, List<Vector3Int> positions, string quadrant)
{
Vector3Int topmostEmptyPosition = new Vector3Int(0, int.MinValue, 0);
// Iterate over the positions from first to last to find the topmost empty position
foreach (Vector3Int position in positions)
{
if (!pegInventory.ContainsKey(position) || pegInventory[position] == null)
{
// Update topmostEmptyPosition if the current position is higher
if (position.y > topmostEmptyPosition.y)
{
topmostEmptyPosition = position;
}
}
}
// Check if the pegInventory already contains the topmostEmptyPosition
if (pegInventory.ContainsKey(topmostEmptyPosition))
{
// If it does, update the peg at this position
pegInventory[topmostEmptyPosition] = loopPeg;
}
else
{
// If it doesn't, add a new entry to the pegInventory
pegInventory.Add(topmostEmptyPosition, loopPeg);
// Update the lastTopmostPositions for the quadrant
lastTopmostPositions[quadrant] = topmostEmptyPosition;
Debug.Log($"<color=orange>[PegBehavior] Moved loopPeg: {loopPeg.name} back to position: {topmostEmptyPosition} in quadrant: {quadrant}</color>");
}
// Move the loopPeg to the topmostEmptyPosition
loopPeg.transform.localScale *= 0.6f;
Vector3 newPosition = pegSlotsTilemap.GetCellCenterWorld(topmostEmptyPosition);
Vector3Int slotPosition = pegSlotsTilemap.WorldToCell(newPosition);
slotPosition.z = -1;
loopPeg.transform.position = slotPosition;
// Update the lastTopmostPositions for the quadrant
lastTopmostPositions[quadrant] = topmostEmptyPosition;
// Create a StringBuilder to concatenate the lastTopmostPositions
StringBuilder sb = new StringBuilder();
sb.Append("Last topmost positions: ");
foreach (var kvp in lastTopmostPositions)
{
sb.Append($"Quadrant: {kvp.Key}, Position: {kvp.Value}; ");
}
// Log the concatenated lastTopmostPositions
Debug.LogError(sb.ToString());
Debug.Log($"Moved loopPeg: {loopPeg.name} back to position: {topmostEmptyPosition} in quadrant: {quadrant}");
}
In the ‘CapturePegs’ method, I am trying to find the topmost empty position in a list of positions and place a loopPeg GameObject at this position:
-
Initialize topmostEmptyPosition to a position with a y-coordinate of int.MinValue, which ensures that any position in the positions list will be higher than this initial position.
-
Iterate over the positions list from first to last. For each position, check if it is empty (i.e., if it does not contain a peg in the pegInventory). If it is empty and its y-coordinate is higher than the y-coordinate of topmostEmptyPosition, update topmostEmptyPosition to this position.
-
After finding the topmostEmptyPosition, check if the pegInventory already contains this position. If it does, update the peg at this position to loopPeg. If it doesn’t, add a new entry to the pegInventory with topmostEmptyPosition as the key and loopPeg as the value.
-
Update the lastTopmostPositions for the given quadrant to topmostEmptyPosition.
-
Move the loopPeg to the topmostEmptyPosition by setting loopPeg.transform.position to topmostEmptyPosition.
However, the pegs are not being placed in any coordinate that resembles the last coordinates they were removed from. Any help is appreciated, thanks!