/// <param name="changeDirectionModifier">Value from 0.0 to 1.0. Percentage chance of changing direction.</param> public DirectionPicker(Random random, Direction previousDirection, double changeDirectionModifier) { _random = random; _directionsPicked = new List<Direction>(); _previousDirection = previousDirection; _changeDirectionModifer = changeDirectionModifier; }
public bool HasAdjacentCellInDirection(Vector2I location, Direction direction) { // Check that the location falls within the bounds of the map: if (!Bounds.Contains(location)) { return false; } // Check if there is an adjacent cell in the direction: if (direction.Equals(Direction.North)) { return location.Y > 0; } else if (direction.Equals(Direction.South)) { return location.Y < (Rows - 1); } else if (direction.Equals(Direction.West)) { return location.X > 0; } else if (direction.Equals(Direction.East)) { return location.X < (Columns - 1); } else { return false; } }
static Direction() { _random = new Random(); North = new Direction(new Vector2I(0, -1)); South = new Direction(new Vector2I(0, 1)); East = new Direction(new Vector2I(1, 0)); West = new Direction(new Vector2I(-1, 0)); }
public Entity() { _isAlive = true; _speed = 1.0f; _orientation = Direction.North; _range = 4.0f; Size = 1.0f; }
private Vector2I CreateSide(Vector2I location, Direction direction, SideType sideType) { var target = GetTargetLocation(location, direction); if (direction.Equals(Direction.North)) { this[location].NorthSide = sideType; this[target].SouthSide = sideType; } else if (direction.Equals(Direction.South)) { this[location].SouthSide = sideType; this[target].NorthSide = sideType; } else if (direction.Equals(Direction.West)) { this[location].WestSide = sideType; this[target].EastSide = sideType; } else if (direction.Equals(Direction.East)) { this[location].EastSide = sideType; this[target].WestSide = sideType; } return target; }
public Vector2I CreateDoor(Vector2I location, Direction direction) { return CreateSide(location, direction, SideType.Door); }
public Vector2I CreateWall(Vector2I location, Direction direction) { return CreateSide(location, direction, SideType.Wall); }
public Vector2I CreateCorridor(Vector2I location, Direction direction) { return CreateSide(location, direction, SideType.Empty); }
public bool AdjacentCellInDirectionIsCorridor(Vector2I location, Direction direction) { if (!HasAdjacentCellInDirection(location, direction)) { return false; } var target = GetTargetLocation(location, direction); return this[target].IsCorridor; }
public bool AdjacentCellInDirectionIsVisited(Vector2I location, Direction direction) { if (!HasAdjacentCellInDirection(location, direction)) { throw new InvalidOperationException("No adjacent cell exists for the location and direction provided."); } return this[location + direction.ToVector2I()].Visited; }
private static IEnumerable<Vector2I> GetRoomPoints(int x, int y, int xlen, int ylen, Direction d) { // north and south share the same x strategy // east and west share the same y strategy Func<int, int, int> a = GetFeatureLowerBound; Func<int, int, int> b = GetFeatureUpperBound; if (d == Direction.North) { for (var xt = a(x, xlen); xt < b(x, xlen); xt++) for (var yt = y; yt > y - ylen; yt--) yield return new Vector2I(xt, yt); } else if (d == Direction.East) { for (var xt = x; xt < x + xlen; xt++) for (var yt = a(y, ylen); yt < b(y, ylen); yt++) yield return new Vector2I(xt, yt); } else if (d == Direction.South) { for (var xt = a(x, xlen); xt < b(x, xlen); xt++) for (var yt = y; yt < y + ylen; yt++) yield return new Vector2I(xt, yt); } else if (d == Direction.West) { for (var xt = x; xt > x - xlen; xt--) for (var yt = a(y, ylen); yt < b(y, ylen); yt++) yield return new Vector2I(xt, yt); } }
private void Move(Level level, Direction direction) { // TODO: Modify speed according to the friction value of the current block. // TODO: Move slower if traveling on the background layer. // TODO: Don't allow walking through a position where all layers, including the background, are empty. MoveBy(level, direction, _speed); }
private void MoveBy(Level level, Direction direction, float speed) { var vi = direction.ToVector2I(); MoveBy(level, new Vector2(vi.X * speed, vi.Y * speed)); }
private bool MakeRoom(IProgress<string> progress, int x, int y, int xlength, int ylength, Direction direction) { // define the dimensions of the room, it should be at least 4x4 tiles (2x2 for walking on, the rest is walls) int xlen = GetRand(MIN_ROOM_COLUMNS, xlength); int ylen = GetRand(MIN_ROOM_ROWS, ylength); // the tile type it's going to be filled with const Tile Floor = Tile.DirtFloor; const Tile Wall = Tile.DirtWall; // choose the way it's pointing at var points = GetRoomPoints(x, y, xlen, ylen, direction).ToArray(); // Check if there's enough space left for it if (points.Any(s => !MathHelper.IsInRange(s.Y, 0, _rows + 1) || !MathHelper.IsInRange(s.X, 0, _columns + 1) || GetCellType(s.X, s.Y) != Tile.Unused)) { return false; } progress.Report($"Making room:int x={x}, int y={y}, int xlength={xlength}, int ylength={ylen}, int direction={direction}"); foreach (var p in points) { SetCell(p.X, p.Y, IsWall(x, y, xlen, ylen, p.X, p.Y, direction) ? Wall : Floor); } // yay, all done return true; }
private bool MakeCorridor(int x, int y, int length, Direction direction) { // define the dimensions of the corridor (er.. only the width and height..) int len = this.GetRand(2, length); const Tile Floor = Tile.Corridor; int xtemp; int ytemp = 0; if (direction == Direction.North) { // north // check if there's enough space for the corridor // start with checking it's not out of the boundaries if (x < 0 || x > this._columns) return false; xtemp = x; // same thing here, to make sure it's not out of the boundaries for (ytemp = y; ytemp > (y - len); ytemp--) { if (ytemp < 0 || ytemp > this._rows) return false; // oh boho, it was! if (GetCellType(xtemp, ytemp) != Tile.Unused) return false; } // if we're still here, let's start building Corridors++; for (ytemp = y; ytemp > (y - len); ytemp--) { this.SetCell(xtemp, ytemp, Floor); } } else if (direction == Direction.East) { // east if (y < 0 || y > this._rows) return false; ytemp = y; for (xtemp = x; xtemp < (x + len); xtemp++) { if (xtemp < 0 || xtemp > this._columns) return false; if (GetCellType(xtemp, ytemp) != Tile.Unused) return false; } Corridors++; for (xtemp = x; xtemp < (x + len); xtemp++) { this.SetCell(xtemp, ytemp, Floor); } } else if (direction == Direction.South) { // south if (x < 0 || x > this._columns) return false; xtemp = x; for (ytemp = y; ytemp < (y + len); ytemp++) { if (ytemp < 0 || ytemp > this._rows) return false; if (GetCellType(xtemp, ytemp) != Tile.Unused) return false; } Corridors++; for (ytemp = y; ytemp < (y + len); ytemp++) { this.SetCell(xtemp, ytemp, Floor); } } else if (direction == Direction.West) { // west if (ytemp < 0 || ytemp > this._rows) return false; ytemp = y; for (xtemp = x; xtemp > (x - len); xtemp--) { if (xtemp < 0 || xtemp > this._columns) return false; if (GetCellType(xtemp, ytemp) != Tile.Unused) return false; } Corridors++; for (xtemp = x; xtemp > (x - len); xtemp--) { this.SetCell(xtemp, ytemp, Floor); } } // woot, we're still here! let's tell the other guys we're done!! return true; }
protected Vector2I GetTargetLocation(Vector2I location, Direction direction) { if (!HasAdjacentCellInDirection(location, direction)) { throw new InvalidOperationException("No adjacent cell exists for the location and direction provided."); } if (direction.Equals(Direction.North)) { return location - Vector2I.UnitY; } else if (direction.Equals(Direction.South)) { return location + Vector2I.UnitY; } else if (direction.Equals(Direction.West)) { return location - Vector2I.UnitX; } else if (direction.Equals(Direction.East)) { return location + Vector2I.UnitX; } else { throw new InvalidOperationException("No adjacent cell exists for the location and direction provided."); } }
private static bool IsWall(int x, int y, int xlen, int ylen, int xt, int yt, Direction d) { Func<int, int, int> a = GetFeatureLowerBound; Func<int, int, int> b = IsFeatureWallBound; if (d == Direction.North) { return xt == a(x, xlen) || xt == b(x, xlen) || yt == y || yt == y - ylen + 1; } else if (d == Direction.East) { return xt == x || xt == x + xlen - 1 || yt == a(y, ylen) || yt == b(y, ylen); } else if (d == Direction.South) { return xt == a(x, xlen) || xt == b(x, xlen) || yt == y || yt == y + ylen - 1; } else if (d == Direction.West) { return xt == x || xt == x - xlen + 1 || yt == a(y, ylen) || yt == b(y, ylen); } throw new InvalidOperationException(); }