private Vector2Int?RandomMove(Level.Level level) { var tries = 100; while (true) { var direction = new Vector2Int(random.Next(-1, 2), random.Next(-1, 2)); if (level.MonsterCanMove(new Vector2Int(direction.x + Location.x, direction.y + Location.y))) { return(direction); } if (tries-- == 0) { return(null); } } }
private Vector2Int?MoveTowards(Level.Level level, Vector2Int to) { Vector2Int?direction = null; if (to.x == Location.x) { if (to.y < Location.y) { direction = new Vector2Int(0, -1); } else { direction = new Vector2Int(0, 1); } } else if (to.y == Location.y) { if (to.x < Location.x) { direction = new Vector2Int(-1, 0); } else { direction = new Vector2Int(1, 0); } } else { int dx; if (to.x < Location.x) { dx = -1; } else { dx = 1; } int dy; if (to.y < Location.y) { dy = -1; } else { dy = 1; } direction = new Vector2Int(dx, dy); } if (direction == null) { return(null); } var dir = (Vector2Int)direction; if (!level.MonsterCanMove(new Vector2Int(dir.x + Location.x, dir.y + Location.y))) { return(null); } return(direction); }