/// <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); }
/// <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); }
//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); }