示例#1
0
    /// <summary>
    /// Creates a new Building GO and tries to place it into the Grid.
    /// Destroys Building GO if unable to and returns null, returns the newly created GO otherwise
    /// </summary>
    /// <param name="spawnWorldPos">Where to spawn the Building (Transform's Position)</param>
    /// <param name="buildingID">What type of Building</param>
    /// <param name="doCheckingGridTaken">Are any of the spots taken by another building already?</param>
    /// <returns></returns>
    BaseBuildingsClass PlaceBuildingToGrid(Vector2 spawnWorldPos, BuildingDataBase.BUILDINGS buildingID, bool doCheckingGridTaken = false)
    {
        // Check if we need to create a Custom Building GO
        BaseBuildingsClass newBuilding = null;

        if (BuildingDataBase.GetInstance().GetBuildingData(buildingID).GetOwnCustomBuildingObject())
        {
            GameObject customBuilding = BuildingDataBase.GetInstance().GetBuildingData(buildingID).GetOwnCustomBuildingObject();
            newBuilding = Instantiate(customBuilding, spawnWorldPos, Quaternion.identity).GetComponent <BaseBuildingsClass>();
        }
        else
        {
            newBuilding = Instantiate(BuildingDataBase.GetInstance().GetBaseBuildingGO(), spawnWorldPos, Quaternion.identity).GetComponent <BaseBuildingsClass>();
        }
        newBuilding.SetNewBuildingType(BuildingDataBase.GetInstance().GetBuildingData(buildingID));

        Vector3    buildingBottomLeftWorldPos = newBuilding.GetBottomLeftGridPosition();
        Vector2Int buildingSize = newBuilding.GetBuildingSizeOnMap();

        // Can we place it there?
        if (doCheckingGridTaken)
        {
            if (!CanPlaceBuildingOnMap(buildingBottomLeftWorldPos, buildingSize))
            {
                Destroy(newBuilding.gameObject);
                return(null);
            }
        }
        // Set all the grids taken by new building to true
        SetGridTakenArray(buildingBottomLeftWorldPos, buildingSize, true);
        AddBuildingIntoTrackingDictionary(newBuilding); //roads and buildings store accordingly
        newBuilding.BuildingPlaced();

        // Change Sprite Layer back to default
        newBuilding.SetSpriteObjectLayer(0);
        newBuilding.gameObject.name = BuildingDataBase.GetInstance().GetBuildingData(buildingID).GetBuildingName();
        // Set Sprite's Sorting Order
        int newOrder = (int)newBuilding.GetBottomLeftGridPosition().y;

        newOrder = -newOrder;
        newBuilding.SetSpriteSortingOrder(newOrder);

        //set the correct parent
        if (newBuilding.GetBuildingType() == BuildingDataBase.BUILDINGS.B_ROAD)
        {
            newBuilding.gameObject.transform.SetParent(m_RoadParent);
        }
        else
        {
            newBuilding.gameObject.transform.SetParent(m_BuildingParent);
        }

        return(newBuilding);
    }
示例#2
0
    /// <summary>
    /// Tries to place a Building into the Grid from it's world Position.
    /// Returns null if Unable to, returns the newly created GO otherwise
    /// </summary>
    /// <param name="activeBuildingCom">The Building GO to place</param>
    /// <param name="doCheckingGridTaken">Are any of the spots taken by another building already?</param>
    /// <returns></returns>
    BaseBuildingsClass PlaceBuildingToGrid(ref BaseBuildingsClass activeBuildingCom, bool doCheckingGridTaken = false)
    {
        // Check if we need to create a Custom Building GO
        if (BuildingDataBase.GetInstance().GetBuildingData(activeBuildingCom.GetBuildingType()).GetOwnCustomBuildingObject())
        {
            Vector3 buildingPos = activeBuildingCom.transform.position;
            BuildingDataBase.BUILDINGS buildingID = activeBuildingCom.GetBuildingType();
            Destroy(activeBuildingCom.gameObject);
            GameObject customBuilding = BuildingDataBase.GetInstance().GetBuildingData(buildingID).GetOwnCustomBuildingObject();
            activeBuildingCom = Instantiate(customBuilding, buildingPos, Quaternion.identity).GetComponent <BaseBuildingsClass>();
            activeBuildingCom.SetNewBuildingType(BuildingDataBase.GetInstance().GetBuildingData(buildingID));
        }

        Vector3    buildingBottomLeftWorldPos = activeBuildingCom.GetBottomLeftGridPosition();
        Vector2Int buildingSize = activeBuildingCom.GetBuildingSizeOnMap();

        //Debug.Log("GRID POS: " + m_GridGO.WorldToCell(buildingWorldPos));
        // Can we place it there?
        if (doCheckingGridTaken)
        {
            if (!CanPlaceBuildingOnMap(buildingBottomLeftWorldPos, buildingSize))
            {
                return(null);
            }
        }
        // Set all the grids taken by new building to true
        SetGridTakenArray(buildingBottomLeftWorldPos, buildingSize, true);
        AddBuildingIntoTrackingDictionary(activeBuildingCom); //roads and buildings store accordingly
        activeBuildingCom.BuildingPlaced();

        // Change Sprite Layer back to building layer
        activeBuildingCom.SetSpriteObjectLayer(0);
        activeBuildingCom.gameObject.name = BuildingDataBase.GetInstance().GetBuildingData(activeBuildingCom.GetBuildingType()).GetBuildingName();
        // Set Sprite's Sorting Order
        int newOrder = (int)activeBuildingCom.GetBottomLeftGridPosition().y;

        newOrder = -newOrder;
        activeBuildingCom.SetSpriteSortingOrder(newOrder);

        //set the correct parent
        if (activeBuildingCom.GetBuildingType() == BuildingDataBase.BUILDINGS.B_ROAD)
        {
            activeBuildingCom.gameObject.transform.SetParent(m_RoadParent);
        }
        else
        {
            activeBuildingCom.gameObject.transform.SetParent(m_BuildingParent);
        }

        return(activeBuildingCom);
    }
示例#3
0
    public void PlaceMovedBuildingBackToMap()
    {
        if (!m_MovementBrushActive || m_BuildingToMove == null)
        {
            return;
        }
        Vector3    buildingBottomLeftWorldPos = m_BuildingToMove.GetBottomLeftGridPosition();
        Vector2Int buildingSize = m_BuildingToMove.GetBuildingSizeOnMap();

        // Set all the grids taken by new Position to true
        SetGridTakenArray(buildingBottomLeftWorldPos, buildingSize, true);
        // Add back into Dictionary to Track
        AddBuildingIntoTrackingDictionary(m_BuildingToMove);
    }
示例#4
0
    //to store the loaded roads from the save file into the roadManager
    public void StoreLoadedRoads(BaseBuildingsClass roadBuilding, RoadTypeList roadType)
    {
        if (m_RoadManager == null)
        {
            return;
        }

        Vector3    buildingBottomLeftWorldPos = roadBuilding.GetBottomLeftGridPosition();
        Vector2Int buildingSize = roadBuilding.GetBuildingSizeOnMap();

        // Set all the grids taken by new building to true
        SetGridTakenArray(buildingBottomLeftWorldPos, buildingSize, true);

        Vector2Int key = (Vector2Int)m_GridGO.WorldToCell(roadBuilding.GetBottomLeftGridPosition());

        roadBuilding.BuildingPlaced();

        m_RoadManager.StoreLoadedInRoads(key, roadBuilding, roadType);
    }
示例#5
0
    /// <summary>
    /// Removes the Building from Grid by setting it's array spots back to untaken, then returns it
    /// </summary>
    /// <param name="activeBuildingCom"></param>
    /// <returns></returns>
    GameObject RemoveBuildingFromGrid(BaseBuildingsClass activeBuildingCom)
    {
        Vector3    buildingBottomLeftWorldPos = activeBuildingCom.GetBottomLeftGridPosition();
        Vector2Int buildingSize = activeBuildingCom.GetBuildingSizeOnMap();

        // Set all the grids taken by building to false, since no longer there
        SetGridTakenArray(buildingBottomLeftWorldPos, buildingSize, false);
        RemoveBuildingFromTrackingDictionary(activeBuildingCom);

        return(activeBuildingCom.gameObject);
    }
示例#6
0
    /// <summary>
    /// Removes a building from the tracking Dictionary
    /// </summary>
    /// <param name="activeBuildingCom"></param>
    void RemoveBuildingFromTrackingDictionary(BaseBuildingsClass activeBuildingCom)
    {
        Vector2Int key = (Vector2Int)m_GridGO.WorldToCell(activeBuildingCom.GetBottomLeftGridPosition());

        if (m_DictOfBuildingsOnMap.ContainsKey(key)) //if have remove
        {
            m_DictOfBuildingsOnMap.Remove(key);
            return;
        }

        //check roads instead if its not in building map
        m_RoadManager.RemoveRoads(key);
    }
示例#7
0
    /// <summary>
    /// Adds a building into the tracking Dictionary
    /// </summary>
    /// <param name="activeBuildingCom"></param>
    void AddBuildingIntoTrackingDictionary(BaseBuildingsClass activeBuildingCom)
    {
        Vector2Int key = (Vector2Int)m_GridGO.WorldToCell(activeBuildingCom.GetBottomLeftGridPosition());

        if (m_DictOfBuildingsOnMap.ContainsKey(key))
        {
            Debug.LogError("Duplicate Key in MapManager Building Storage!!\n2 Buildings occupying same Position!");
            return;
        }

        //check if its roads or not, store accordingly
        if (activeBuildingCom.GetBuildingType() == BuildingDataBase.BUILDINGS.B_ROAD)
        {
            m_RoadManager.PlaceRoads(key, ref activeBuildingCom);
        }
        else
        {
            m_DictOfBuildingsOnMap[key] = activeBuildingCom;
        }
    }
示例#8
0
 bool CanPlaceBuildingOnMap(BaseBuildingsClass buildingToCheck)
 {
     return(CanPlaceBuildingOnMap(buildingToCheck.GetBottomLeftGridPosition(), buildingToCheck.GetBuildingSizeOnMap()));
 }
示例#9
0
 /// <summary>
 /// Checks if any of the spots the Template building will occupy, is already taken
 /// </summary>
 /// <returns>True if can place Template Building</returns>
 public bool CanPlaceTemplateBuilding()
 {
     return(CanPlaceBuildingOnMap(m_TemplateBuilding.GetBottomLeftGridPosition(), m_TemplateBuilding.GetBuildingSizeOnMap()));
 }