/// <summary> /// Whether two points are adjacent in the grid. Assumes whole /// numbers for simplicity. /// </summary> /// <param name="grid"> /// Grid used for comparison. /// </param> /// <param name="a"> /// First coordinate in world coordinates. /// </param> /// <param name="b"> /// Second coordinate in world coordinates. /// </param> public static bool AreAdjacent(this RectGrid grid, Vector3 a, Vector3 b) { // Convert to grid-coordinates var u = grid.WorldToGrid(a); var v = grid.WorldToGrid(b); // Manhattan distance is the sum of the horizontal and vertical distances var manhattanDistance = AbsDelta(u.x, v.x) + AbsDelta(u.y, v.y); // Diagonal positions would have a Manhattan distance of 2. return(manhattanDistance <= 1.25); }
/// <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)); }
/// <summary> /// Whether the head of the snake is outside the grid. /// </summary> /// <param name="position"> /// Position in grid-coordinates. /// </param> /// <remarks> /// <para> /// </para> /// </remarks> private bool OutsideRange(Vector3 position) { var local = _grid.WorldToGrid(position); var from = _renderer.From; var to = _renderer.To; var x = local.x; var y = local.y; if (x > to.x || y > to.y || x < from.x || y < from.y) { return(true); } return(false); }
void OnMouseOver() { int raise; if (Input.GetMouseButtonDown(0)) // when the player left-clicks { raise = 1; } else if (Input.GetMouseButtonDown(1)) // when the player right-clicks { raise = -1; } else { return; } RaycastHit hit; _mc.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity); var gridPoint = _grid.WorldToGrid(hit.point); var row = Mathf.RoundToInt(-gridPoint.z); var column = Mathf.RoundToInt(gridPoint.x); var index = RowColumn2Index(row, column); var vertices = _mesh.vertices; vertices[index] += raise * _grid.Up; _mesh.vertices = vertices; CleanupMesh(_mesh); // To update the mesh collider remove its mesh and then re-assign // it again. _mc.sharedMesh = null; _mc.sharedMesh = _mesh; heightMap[row, column] += raise; UpdateHeightString(); }
/// <summary> /// Takes world coordinates, finds the corresponding tile and sets /// that entry to either true or false. Use it to disable or enable /// tile. /// </summary> /// <param name="tile"> /// Position of the tile in world-coordinates. /// </param> /// <param name="status"> /// Initial value of the tile. /// </param> public static void SetTile(Vector3 tile, bool status) { // We need the tile in grid-coordinates tile = _grid.WorldToGrid(tile); _tiles[Mathf.FloorToInt(tile.x), Mathf.FloorToInt(tile.y)] = status; }
public Vector3 WorldToGrid(Vector3 world) { return(_mainGrid.WorldToGrid(world)); }