public override void HandleMovement(Character c) { if (c.Movement == new Vector3Int(0, 0, 0)) { ToState(c, new StateIdle()); } if (!Input.GetKey(KeyCode.LeftShift)) { ToState(c, new StateWalk()); } c.Animator.ChangeFacingDirection(c.CurrentFacing); _whereAmI = WorldGrid.GetGridPositionFromWorld(c.transform.position); DataTile dt = WorldGrid.GetCellAt(_whereAmI + c.Movement); if (!dt.walkable) { Debug.LogError("Sorry buddy, you cant move there!"); return; } _oldPos = c.transform.position; _nextPos = WorldGrid.GetWorldPositionFromGrid(_whereAmI + c.Movement); _difference = _nextPos - _oldPos; _timer = 0; }
void handleMovement(Vector3Int movement) { if (movement == new Vector3Int(0, 0, 0)) { return; } animator.ChangeFacingDirection(currentFacing); _whereAmI = WorldGrid.GetGridPositionFromWorld(transform.position); DataTile dt = WorldGrid.GetCellAt(_whereAmI + movement); if (!dt.walkable) { return; } _oldPos = transform.position; _nextPos = WorldGrid.GetWorldPositionFromGrid(_whereAmI + movement); _difference = _nextPos - _oldPos; _timer = 0; }
private static IEnumerable <(Location, bool)> GetWalkableAdjacentCells(int startX, int startY, IReadOnlyCollection <Location> openList, IReadOnlyCollection <Vector2Int> extraBlockedCells) { WorldGrid grid = null; for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { int xPos = startX + x; int yPos = startY + y; WorldGridCell targetCell = grid.GetCellAt(new Vector2Int(xPos, yPos)); if ((targetCell == null) || !targetCell.Walkable || ((extraBlockedCells.Count > 0) && extraBlockedCells.Contains(new Vector2Int(xPos, yPos)))) { continue; } Location returnLocation = GetLocationFromPos(xPos, yPos) ?? new Location(xPos, yPos); yield return(returnLocation, (x != 0) && (y != 0)); } } Location GetLocationFromPos(int x, int y) { foreach (Location location in openList) { if ((location.X == x) && (location.Y == y)) { return(location); } } return(null); } }