// Checks if the given character's moving direction is wall or not public bool IsDirectionWall(Character character, Point direction) { Point targetPoint = new Point(character.Position.X + direction.X, character.Position.Y + direction.Y); Point currentPoint = new Point(character.Position.X, character.Position.Y); // Validate if the target Tile actually exists. (Not outside the map) if (targetPoint.X >= Map.GetLength(0) || targetPoint.Y >= Map.GetLength(1)) { return true; } Tile targetTile = Map[targetPoint.X, targetPoint.Y]; Tile currentTile = Map[currentPoint.X, currentPoint.Y]; if (direction.Y < 0) // Up { if (targetTile.FourWalls.HasFlag(Walls.Down) || currentTile.FourWalls.HasFlag(Walls.Up)) { return true; } } else if (direction.Y > 0) // Down { if (targetTile.FourWalls.HasFlag(Walls.Up) || currentTile.FourWalls.HasFlag(Walls.Down)) { return true; } } if (direction.X < 0) // Left { if (targetTile.FourWalls.HasFlag(Walls.Right) || currentTile.FourWalls.HasFlag(Walls.Left)) { return true; } } else if (direction.X > 0) // Right { if (targetTile.FourWalls.HasFlag(Walls.Left) || currentTile.FourWalls.HasFlag(Walls.Right)) { return true; } } return false; }
// Checks if the character can move. If it can, set the new position to the character. public bool TryToMoveTheCharacter(Character character, Point direction) { if (IsDirectionWall(character, direction)) { return false; } // New new position(moves) character.Position.X += direction.X; character.Position.Y += direction.Y; return true; }