Exemplo n.º 1
0
    private void AddCurrentBuildingToMap()
    {
        if (currentBuilding)
        {
            Vector3    position = currentBuilding.transform.position - Vector3.up * preViewOffset;
            GameObject building = Instantiate(currentBuildingPrefab, position, Quaternion.identity, buildingsTransform);

            if (isCurrentBuildResourceExtractor)
            {
                ResourceDeposit resourceDeposit = worldMap.GetResourceDeposit(position);
                Debug.Assert(resourceDeposit, "Resource Deposit is null");

                ResourceExtractor resourceExtractor = building.GetComponent <ResourceExtractor>();
                resourceExtractor.SetDeposit(resourceDeposit);
            }

            bool isRoad = building.TryGetComponent(out RoadUpdater roadUpdater);

            if (isRoad)
            {
                worldMap.AddRoadToHexCell(position, building);
            }
            else
            {
                worldMap.AddBuildingToHexCell(position, building);
            }

            WorldMap.DoubleCircleOfHexCellsAround doubleCircleOfHexCellsAround =
                worldMap.GetDoubleCircleOfHexCellsAround(position);

            if (isRoad)
            {
                roadUpdater.UpdateRoad(doubleCircleOfHexCellsAround.firstCircle);
            }
            for (int i = 0; i < WorldMap.cellNeighbourAmount; ++i)
            {
                WorldMap.HexCellsAround hexCellsAround = doubleCircleOfHexCellsAround.secondCircles[i];
                if ((hexCellsAround.centerCell.hexType & WorldMap.HexType.Road) == WorldMap.HexType.Road)
                {
                    if (hexCellsAround.centerCell.road)
                    {
                        if (hexCellsAround.centerCell.road.TryGetComponent(out RoadUpdater currentRoadUpdater))
                        {
                            currentRoadUpdater.UpdateRoad(hexCellsAround);
                        }
                        else
                        {
                            Debug.LogError("Game object doesn't contain road updater");
                        }
                    }
                    else
                    {
                        Debug.LogError("Road game object is empty");
                    }
                }
            }
        }
    }