void SetMovingBuildingToGridPosition() { // Calculate actual Position on Grid BaseMapClass gridLayout = MapManager.GetInstance().GetCurrentMap(); // Follow Camera Position Vector3Int gridPos = gridLayout.GetTileMapCom().WorldToCell(Camera.main.transform.position + (Vector3)m_MovingPlacedBuildingOffset); Vector3 newPos = gridLayout.GetTileMapCom().CellToWorld(gridPos); newPos += gridLayout.GetTileMapCom().cellSize * 0.5f; MapManager.GetInstance().GetBuildingToMove().transform.position = newPos; }
/// <summary> /// Attempts to place a new Building of type buildingID into the map /// WITHOUT checking and deducting if have resources needed /// </summary> /// <param name="spawnWorldPosition">World position of the Building</param> /// <param name="buildingID">What type of building to place</param> /// <returns></returns> public BaseBuildingsClass PlaceNewBuildingIntoMap_WithoutResources(Vector2 spawnWorldPosition, BuildingDataBase.BUILDINGS buildingID) { // Convert World to Grid Coordinates BaseMapClass gridLayout = GetCurrentMap(); Vector3Int gridPos = gridLayout.GetTileMapCom().WorldToCell(spawnWorldPosition); spawnWorldPosition = gridLayout.GetTileMapCom().CellToWorld(gridPos); spawnWorldPosition += (Vector2)gridLayout.GetTileMapCom().cellSize * 0.5f; // Check if we can place building Down Vector3 buildingBottomLeft = spawnWorldPosition + BuildingDataBase.GetInstance().GetBuildingData(buildingID).GetBottomLeftCorner_PositionOffset(); Vector2Int buildingSize = BuildingDataBase.GetInstance().GetBuildingData(buildingID).GetBuildingSizeOnMap(); if (!CanPlaceBuildingOnMap(buildingBottomLeft, buildingSize)) { Debug.LogError("Unable to Place Building, Canceling..."); return(null); } // Create the Building return(PlaceBuildingToGrid(spawnWorldPosition, buildingID)); }
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); }