public void AddToPlacementGrid(Mesh mesh, Chunk chunk, Transform chunkTransform) { if (_placementGrid.ContainsKey(chunk)) { return; } List <Vector3> centerPoints = new List <Vector3>(); Vector3 firstPoint = new Vector3(); Vector3 lastPoint = new Vector3(); List <PlaceableCell> placeableCells = new List <PlaceableCell>(); //For every square (4 vertices) store the center point int length = mesh.vertices.Length; for (int i = 0; i < length; i += 4) { firstPoint = mesh.vertices [i]; //1 lastPoint = mesh.vertices [i + 2]; //3 Vector3 centerPoint = ((chunkTransform.transform.TransformPoint(firstPoint) + chunkTransform.transform.TransformPoint(lastPoint)) / 2); centerPoints.Add(centerPoint); PlaceableCell cell = new PlaceableCell(centerPoint, null, chunk); placeableCells.Add(cell); } _placementGrid.Add(chunk, placeableCells); }
private PlaceableCell GetNearestPlaceableCell(List <PlaceableCell> placeableCells, Vector3 position) { float shortestDistance = float.PositiveInfinity; float currentDistance; PlaceableCell nearestPlaceableCell = null; Debug.Log(placeableCells); int length = placeableCells.Count; for (int i = 0; i < length; i++) { currentDistance = Vector3.Magnitude(placeableCells [i].Position - position); if (currentDistance == float.PositiveInfinity && nearestPlaceableCell == null) { nearestPlaceableCell = placeableCells [i]; } if (currentDistance < shortestDistance) { shortestDistance = currentDistance; nearestPlaceableCell = placeableCells [i]; } } return(nearestPlaceableCell); }
private PlaceableCell GetNearestPlaceableCell(Vector3 position, Chunk chunk = null) { PlaceableCell nearestPlaceableCell = null; float shortestDistance = float.PositiveInfinity; float currentDistance; //Search in all chunks of the list if (chunk == null) { PlaceableCell currentNearestPlaceableCell = null; foreach (KeyValuePair <Chunk, List <PlaceableCell> > currentChunk in _placementGrid) { currentNearestPlaceableCell = GetNearestPlaceableCell(currentChunk.Value, position); //Debug.Log( currentNearestPlaceableCell ); currentDistance = Vector3.Magnitude(currentNearestPlaceableCell.Position - position); if (currentDistance < shortestDistance) { shortestDistance = currentDistance; nearestPlaceableCell = currentNearestPlaceableCell; } } } else //Search directly in the chunk { nearestPlaceableCell = GetNearestPlaceableCell(chunk, position); } return(nearestPlaceableCell); }
private void PlaceSelectedPlaceableObject(Vector3 clickPoint) { PlaceableCell cellToPlace = GetNearestPlaceableCell(clickPoint); if (cellToPlace.IsFree()) { cellToPlace.AddPlaceable(_selectedPlaceableObject); _selectedPlaceableObject = null; _selectedPleacableGameObject.transform.position = cellToPlace.Position; //_selectedPleacableGameObject.transform.SetParent( cellToPlace to place chunk transform ); } }
private void MoveSelectedPlaceableObject(Vector3 mousePoint) { PlaceableCell cellToPlace = GetNearestPlaceableCell(mousePoint); if (cellToPlace == null) { return; } if (cellToPlace.IsFree()) { _selectedPleacableGameObject.transform.position = cellToPlace.Position; } }
private PlaceableCell GetNearestPlaceableCell(Chunk chunk, Vector3 position) { PlaceableCell nearestCell = null; List <PlaceableCell> placeableCells; if (_placementGrid.TryGetValue(chunk, out placeableCells)) { nearestCell = GetNearestPlaceableCell(placeableCells, position); } return(nearestCell); }