private HashSet <PartPlacement> GetPartNeighbors(PartPlacement part) { var neighbors = new HashSet <PartPlacement>(); // Throw this trash away in the morning. Instead do this: // 1. Collect all neighbors (tiles bordering part, respecting width and height; 8, 10, or 12) // 2. Get the part in that tile, if there is one // 3. If there's a part there, add it to the hashmap for (int y = (int)part.y - 1; y <= part.y + part.part.height; y++) { for (int x = (int)part.x - 1; x <= part.x + part.part.width; x++) { if (x >= 0 && x < grid.GetLength(0) && y >= 0 && y < grid.GetLength(1)) { var neighbor = GetPartAt(x, y); if (neighbor != part && neighbor != null) { neighbors.Add(neighbor); } } } } return(neighbors); }
public bool PlacePart(CarPart part, uint x, uint y) { if (PlacementIsValid(part, x, y)) { if (part.partName != "Wrench") { // Add a part with coordinates to the list of parts var placement = new PartPlacement(part, x, y); parts.Add(placement); var sprite = Instantiate(part.sprite); sprite.transform.SetParent(this.partPlacementContainer.transform, false); placement.sprite = sprite; var rect = tileGrid[0, 0].GetComponent <RectTransform>().rect; var size = rect.height; // Correct for position sprite.transform.position += new Vector3( size * x, size * (gridSize - 1 - y), 0 ); // Correct for tile size sprite.transform.position -= new Vector3( (part.width - 1) * (size / 2) * -1, (part.height - 1) * (size / 2), 0 ); // Update the grid so parts can't be placed on top of each other for (var yCell = 0; yCell < part.height; yCell++) { for (var xCell = 0; xCell < part.width; xCell++) { grid[xCell + x, yCell + y] = true; } } } else { // Remove the part at the coordinates var partToRemove = GetPartAt((int)x, (int)y); if (partToRemove != null) { parts.Remove(partToRemove); Destroy(partToRemove.sprite); // Update the grid so parts can be placed here again for (var yCell = 0; yCell < partToRemove.part.height; yCell++) { for (var xCell = 0; xCell < partToRemove.part.width; xCell++) { grid[xCell + partToRemove.x, yCell + partToRemove.y] = false; } } } } return(true); } return(false); }