/// <summary> /// Compute the vertices of the mesh. /// </summary> private Vector3[] ComputeVertices(Mesh mesh) { var rows = heightMap.GetLength(0); var columns = heightMap.GetLength(1); // This mesh uses shared vertices for simplicity var vertices = new Vector3[rows * columns]; for (var r = 0; r < rows; r++) { for (var c = 0; c < columns; c++) { var h = heightMap[r, c]; // Now assign the vertex depending on its row, column and // height (vector in local space; row, column and height in // grid space) var gridVertex = new Vector3(c, h, -r); var worldVertex = _grid.GridToWorld(gridVertex); var localVertex = transform.InverseTransformPoint(worldVertex); vertices[r * rows + c] = localVertex; } } // Assign the vertices to the mesh mesh.vertices = vertices; return(vertices); }
/// <summary> /// Pick the next goal based on user input. The input if filtered /// through a number for criteria. /// </summary> private void PickNext() { Vector3 direction; // Direction to move in (grid-coordinates) var h = Input.GetAxisRaw("Horizontal"); var v = Input.GetAxisRaw("Vertical"); if (h > 0) { direction = Vector3.right; } else if (h < 0) { direction = Vector3.left; } else if (v > 0) { direction = Vector3.up; } else if (v < 0) { direction = Vector3.down; } else { return; } // We will be operating in grid space, so convert the position _goal = _grid.WorldToGrid(transform.position) + direction; // Check that the target is still inside the rendered region of the // grid. for (var i = 0; i < 2; ++i) { var beyondLower = _goal[i] < _renderer.From[i]; var beyondUpper = _goal[i] > _renderer.To[i]; if (beyondLower || beyondUpper) { Debug.Log("I can't swim."); return; } } // Check for walls if (!ForbiddenTiles.CheckTile(_goal)) { Debug.Log("Ouch!"); return; } _goal = _grid.GridToWorld(_goal); _isMoving = true; }
/// <summary> /// Makes the object snap to the bottom of the grid, respecting the /// grid's rotation. /// </summary> private Vector3 CalculateOffsetY() { //first store the objects position in grid coordinates var gridPosition = _grid.WorldToGrid(transform.position); //then change only the Y coordinate gridPosition.y = .5f * transform.lossyScale.y; //convert the result back to world coordinates return(_grid.GridToWorld(gridPosition)); }
public static List <GameObject> GetColumnObjects(Vector3 pos) { List <GameObject> foundGOs = new List <GameObject>(); var sq = GetSquare(pos); //Debug.Log("My Pos: " + sq[0] + " : " + sq[1]); for (var j = levelMatrix.GetLength(1) - 1; j >= 0; --j) { //Debug.Log("EMPTY?: " + levelMatrix[sq[0], j]); if (levelMatrix[sq[0], j] == false) { Vector3 gridPoint = SquareToGrid(new int[] { sq[0], j }); Vector3 worldPoint = _grid.GridToWorld(gridPoint); //Debug.Log("worldPoint: " + worldPoint); Collider2D col = Physics2D.OverlapPoint(new Vector2(worldPoint.x, worldPoint.y), LayerMask.GetMask("Default")); if (col != null) { Debug.Log("col name: " + col.transform.name); Box b = col.GetComponent <Box>(); if (col != null && b != null) { if (b.boxHealthState != BoxHealthState.Broken) { foundGOs.Add(col.gameObject); Debug.Log("col: " + col.transform.name); } } } } } return(foundGOs); }
/// <summary> /// Calculates the movement constrains for a block that's being /// dragged. /// </summary> /// <remarks> /// <para> /// When we want to slide a block we need to know how far we can go /// before we "collide" (note that there is no actual Unity /// collision detection involved anywhere). We can only look up to /// on square ahead in each direction, so the bounds need to be /// recalculated from time to time; this allows us to have /// obstacles in all sorts of directions, like a maze that can /// change all the time. /// </para> /// </remarks> public static Vector3[] CalculateSlidingBounds(Vector3 pos, Vector3 scl) { // Break up the block and find the lower left and upper right // square in the matrix. var squares = BreakUpObstacle(pos, scl); var squaresWidth = squares.GetLength(0); var squaresHeight = squares.GetLength(1); var lowerLeft = GetSquare(squares[0, 0]); var upperRight = GetSquare(squares[squaresWidth - 1, squaresHeight - 1]); var matrixWidth = levelMatrix.GetLength(0); var matrixHeight = levelMatrix.GetLength(1); int left = lowerLeft[0], bottom = lowerLeft[1], right = upperRight[0], top = upperRight[1]; // For each adjacent left square check if all left fields are free // (a bitmask would have been the way to go instead of four bools, // but let's keep it simple). var freeEast = true; //iterate over all the squares one square left of the left edge for (var i = bottom; i <= top; ++i) { // use Min so we don't go off the matrix size. var x = Mathf.Min(matrixWidth - 1, right + 1); var y = i; freeEast = freeEast && levelMatrix[x, y]; } bool freeWest = true; //iterate for (var i = bottom; i <= top; ++i) { // Use the Max so we don't get negative values (the matrix // starts at 0). var x = Mathf.Max(0, left - 1); var y = i; freeWest = freeWest && levelMatrix[x, y]; } var freeSouth = true; for (var i = left; i <= right; ++i) { var x = i; var y = Mathf.Max(0, bottom - 1); freeSouth = freeSouth && levelMatrix[x, y]; } var freeNorth = true; for (var i = left; i <= right; ++i) { var x = i; var y = Mathf.Min(matrixHeight - 1, top + 1); freeNorth = freeNorth && levelMatrix[x, y]; } // Now assume the block canot move anywhere; for each free // direction loosen the constraints by one grid unit (world unit * // spacing). var bounds = new [] { pos, pos }; if (freeEast) { bounds[1] += _grid.Spacing.x * Vector3.right; } if (freeNorth) { bounds[1] += _grid.Spacing.y * Vector3.up; } if (freeWest) { bounds[0] -= _grid.Spacing.x * Vector3.right; } if (freeSouth) { bounds[0] -= _grid.Spacing.y * Vector3.up; } // the bounds can still be outside of the grid, so we need to clamp that as well for (var i = 0; i < 2; i++) { for (var j = 0; j < 2; j++) { var to = _para.To; var bound = bounds[i][j]; bounds[i][j] = Mathf.Clamp(bound, _grid.GridToWorld(Vector3.zero)[j] + 0.5f * scl[j], _grid.GridToWorld(to)[j] - 0.5f * scl[j]); } } return(bounds); }