示例#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
    /// <summary>
    /// Places the Template Building into the Map
    /// </summary>
    /// <param name="doChecking">Are any of the spots taken by another building already?</param>
    /// <returns></returns>
    public BaseBuildingsClass PlaceTemplateBuilding(bool doChecking = false)
    {
        BaseBuildingsClass PlacedBuilding = PlaceBuildingToGrid(ref m_TemplateBuilding, doChecking);

        // Success in placing building, Deduct resources
        ResourceManager.GetInstance().DeductResourcesFromBuildingData(m_TemplateBuildingID);
        // Success in placing building, Create new building for next placment
        BuildingDataBase.BUILDINGS oldID = m_TemplateBuilding.GetBuildingType();
        m_TemplateBuilding = Instantiate(BuildingDataBase.GetInstance().GetBaseBuildingGO(), Camera.main.transform.position, Quaternion.identity).GetComponent <BaseBuildingsClass>();
        m_TemplateBuilding.SetNewBuildingType(BuildingDataBase.GetInstance().GetBuildingData(oldID));
        m_TemplateBuilding.SetSpriteObjectLayer(LayerMask.NameToLayer("BuildingPlaceRef"));

        return(PlacedBuilding);
    }
示例#4
0
    private void Awake()
    {
        if (m_Instance != null)
        {
            Destroy(gameObject);
            return;
        }
        m_Instance = this;
        DontDestroyOnLoad(gameObject);
        m_GridTakenArray = new List <bool>();

        // Placement and Removal
        m_TemplateBuilding = Instantiate(BuildingDataBase.GetInstance().GetBaseBuildingGO(), Camera.main.transform.position, Quaternion.identity).GetComponent <BaseBuildingsClass>();
        m_TemplateBuilding.SetSpriteObjectLayer(LayerMask.NameToLayer("BuildingPlaceRef"));
        m_ListOfBuildingsToRemove = new List <BaseBuildingsClass>();

        // Parent the m_GridGO
        m_GridGO.transform.parent = transform;
    }