/// <summary> /// Creates a new TileMap /// </summary> /// <param name="mapType"></param> /// <returns></returns> public bool CreateNewMap(MapDataBase.MAPS mapType) { // Create new Tilemap, parented under and positioned at m_GridGO GameObject mapObject = MapDataBase.GetInstance().GetMapGO(mapType); mapObject = Instantiate(mapObject, m_GridGO.transform.position, Quaternion.identity, m_GridGO.transform); m_currentMap = mapObject.GetComponent <BaseMapClass>(); // Resize the layout Array int totalSize = m_currentMap.GetTileMapSize().x *m_currentMap.GetTileMapSize().y; m_GridTakenArray.Clear(); m_GridTakenArray.Capacity = totalSize; // Set the capactiy to prevent calling Array.Resize() multiple times for (int i = 0; i < totalSize; ++i) { m_GridTakenArray.Add(false); } // Center the Camera to the map Vector3 centerMapWorldPos = m_GridGO.CellToWorld((Vector3Int)m_currentMap.GetTileMapSize() / 2); centerMapWorldPos += m_GridGO.cellSize * 0.5f; centerMapWorldPos.z = -10.0f; Camera.main.transform.position = centerMapWorldPos; // Fire the Map Generated Event OnMapGenerated(); //Get starting route from one of the routes build at spawn List <MapStartingBuildings> startingBuildingsList = m_currentMap.GetStartingBuildings(); Vector2Int mainRoadPos = Vector2Int.zero; foreach (MapStartingBuildings startingbuild in startingBuildingsList) { if (startingbuild.buildingID == BuildingDataBase.BUILDINGS.B_ROAD) { mainRoadPos = startingbuild.spawnGridPositions[0]; break; } } m_RoadManager.Init(m_currentMap.GetTileMapSize(), mainRoadPos); return(true); }
bool CanPlaceBuildingOnMap(Vector3 buildingBottomLeftWorldPos, Vector2Int buildingSize) { Vector2Int buildingGridPos = (Vector2Int)m_GridGO.WorldToCell(buildingBottomLeftWorldPos); Vector2Int testGridPos = buildingGridPos; int arrayIndex; for (int yAxis = 0; yAxis < buildingSize.y; ++yAxis) { testGridPos.y += yAxis; for (int xAxis = 0; xAxis < buildingSize.x; ++xAxis) { testGridPos.x = buildingGridPos.x + xAxis; // Out of Scope? if (testGridPos.x < 0 || testGridPos.x >= m_currentMap.GetTileMapSize().x || testGridPos.y < 0 || testGridPos.y >= m_currentMap.GetTileMapSize().y) { return(false); } // Out of Map? if (!m_currentMap.GetTileMapCom().HasTile((Vector3Int)testGridPos)) { return(false); } // return false; // Check if spot is already taken arrayIndex = Convert2DToIntIndex(testGridPos); if (m_GridTakenArray[arrayIndex]) { //Debug.Log("SPOT TAKEN"); return(false); } } // Reset checking Position testGridPos = buildingGridPos; } return(true); }
public void Init() { //set the building grid background accordingly BaseMapClass map = MapManager.GetInstance().GetCurrentMap(); if (map != null) { Vector2Int size = map.GetTileMapSize(); if (m_Grid != null) { m_Grid.transform.localScale = new Vector3(size.x, size.y, 1.0f); } if (m_GridMaterial != null) { m_GridMaterial.SetVector("_Tiling", new Vector4(size.x, size.y, 1.0f, 1.0f)); } } m_Grid.SetActive(false); }