public void SaveFileWasLoaded(BuildingsOnMap[] saveFileBuildingsData, RoadsOnMap[] saveFileRoadsData) { // Removes existing buildings first RemoveAllBuildingsFromMap_Quietly(); BaseBuildingsClass savedBuilding = null; Vector3 savedPosition = Vector3.zero; for (int i = 0; i < saveFileBuildingsData.Length; ++i) { savedPosition.x = saveFileBuildingsData[i].worldPosX; savedPosition.y = saveFileBuildingsData[i].worldPosY; savedBuilding = Instantiate(BuildingDataBase.GetInstance().GetBaseBuildingGO(), savedPosition, Quaternion.identity).GetComponent <BaseBuildingsClass>(); savedBuilding.SetNewBuildingType(BuildingDataBase.GetInstance().GetBuildingData((BuildingDataBase.BUILDINGS)saveFileBuildingsData[i].buildingType)); PlaceBuildingToGrid(ref savedBuilding); } //do the same for the roads for (int i = 0; i < saveFileRoadsData.Length; ++i) { savedPosition.x = saveFileRoadsData[i].worldPosX; savedPosition.y = saveFileRoadsData[i].worldPosY; savedBuilding = Instantiate(BuildingDataBase.GetInstance().GetBaseBuildingGO(), savedPosition, Quaternion.identity).GetComponent <BaseBuildingsClass>(); savedBuilding.SetNewBuildingType(BuildingDataBase.GetInstance().GetBuildingData(BuildingDataBase.BUILDINGS.B_ROAD)); //set to the correct road direction sprite RoadTypeList roadType = (RoadTypeList)saveFileRoadsData[i].roadType; savedBuilding.SetSprite(BuildingDataBase.GetInstance().GetRoadSprite(roadType)); StoreLoadedRoads(savedBuilding, roadType); } }
/// <summary> /// Enables/Disables Placement Brush to be used for placing down New Buildings in Map. /// </summary> /// <param name="newValue">True=Enable, False=Disable</param> /// <param name="selectedBuildingID">What Building Type to Start with</param> public void SetPlacementBrush(bool newValue, BuildingDataBase.BUILDINGS selectedBuildingID = BuildingDataBase.BUILDINGS.B_POND) { m_PlacmentBrushActive = newValue; m_TemplateBuildingID = selectedBuildingID; if (m_PlacmentBrushActive) { m_TemplateBuilding.gameObject.SetActive(true); m_TemplateBuilding.SetNewBuildingType(BuildingDataBase.GetInstance().GetBuildingData(m_TemplateBuildingID)); PlayerOpenAddEditorMode(m_TemplateBuilding); } else { m_TemplateBuilding.gameObject.SetActive(false); PlayerCloseAddEditorMode(); } }
/// <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); }
/// <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); }