public Path(TilePos initialPosition, TilePos initialDirection) { Cost = 0; Steps = new List <Step> { new Step(initialPosition, initialDirection, false) }; }
private void AddNewPathToContinue(TilePos pos, TilePos moveDirection, Path path) { var rotationDifference = path.GetRotationDifference(moveDirection); var stepCost = 1 + rotationDifference; _pathsToContinue.Add(path.Branch(stepCost, pos, moveDirection)); }
public static Entity PushableItemAt(this Pool pool, TilePos position, TilePos moveDirection) { var newPosition = position + moveDirection; var entitiesAtPosition = pool.GetEntitiesAt(position, x => x.isBox).ToList(); var openSpaceAtNextPosition = pool.OpenTileAt(newPosition); var stillInsidePuzzle = pool.IsStillInsideSamePuzzle(position, newPosition); return(openSpaceAtNextPosition && stillInsidePuzzle?entitiesAtPosition.SingleOrDefault() : null); }
public static bool KnockObjectsInFront(this Pool pool, TilePos position, TilePos forwardDirection, bool immediate, float knockWait) { var knockable = pool.GetEntityAt(position + forwardDirection, x => x.isBlockingTile); if (knockable != null) { knockable.ReplaceKnocked(forwardDirection, immediate, knockWait); return(true); } return(false); }
public static bool IsStillInsideSamePuzzle(this Pool pool, TilePos currentPosition, TilePos nextPosition) { var currentPuzzle = pool.GetEntityAt(currentPosition, x => x.isPuzzleArea && x.hasBossConnection); var nextPuzzle = pool.GetEntityAt(nextPosition, x => x.isPuzzleArea && x.hasBossConnection); if (currentPuzzle != null && nextPuzzle != null) { return(nextPuzzle.bossConnection.BossId == currentPuzzle.bossConnection.BossId); } return(currentPuzzle == nextPuzzle); }
public static List <Entity> GetEntitiesAt(this Pool pool, TilePos position, Func <Entity, bool> entityMatcher = null) { if (!pool.objectPositionCache.Cache.ContainsKey(position)) { return(new List <Entity>()); } return(pool.objectPositionCache .Cache[position] .Where(x => x.hasGameObject && !x.isDestroyed && (entityMatcher == null || entityMatcher(x))) .ToList()); }
public static Entity GetEntityAt(this Pool pool, TilePos position, Func <Entity, bool> entityMatcher = null) { var entitiesAtPosition = pool .GetEntitiesAt(position, entityMatcher) .ToList(); if (entitiesAtPosition.Count() > 1) { throw new MoreThanOneMatchException(entitiesAtPosition); } return(entitiesAtPosition.SingleOrDefault()); }
public Path CalculateMoveToTarget( TilePos initialPosition, TilePos initialDirection, TilePos targetPosition) { _targetPosition = targetPosition; var victoryPath = new Path(initialPosition, initialDirection); SetInitialState(initialPosition, initialDirection); while (_pathsToContinue.Count > 0) { var path = PopPathToContinue(); var pos = path.GetLastPosition(); if (pos == _targetPosition) { victoryPath = SelectLateTurnPathWithSameCost(path); break; } foreach (var moveDirection in LocalDirections.GetAll()) { var newPos = pos + moveDirection; if (_walkValidator.CanMoveTo(newPos) && !_visited.Contains(newPos)) { AddNewPathToContinue(newPos, moveDirection, path); } } _visited.Add(pos); OrderByCost(); } victoryPath.Steps.RemoveAt(0); return(victoryPath); }
public bool Equals(TilePos other) { return(X == other.X && Z == other.Z); }
public int RotationDifference(TilePos otherDirection) { return(Mathf.Max(Mathf.Abs(X - otherDirection.X), Mathf.Abs(Z - otherDirection.Z))); }
public bool CanMoveTo(TilePos position) { return(_pool.OpenTileAt(position)); }
private Entity CreateEntity(TilePos position) { return(_pool.CreateEntity().AddPosition(position).AddRotation(0)); }
public Path Branch(int cost, TilePos position, TilePos direction) { return(new Path(this, cost, position, direction)); }
public int GetRotationDifference(TilePos direction) { var lastDirection = Steps[Steps.Count - 1].Direction; return(lastDirection.RotationDifference(direction)); }
public static void DoForAllAtPosition(this Pool pool, TilePos position, Action <Entity> entityAction) { pool.objectPositionCache.Cache[position].ForEach(entityAction); }
protected Path(Path path, int cost, TilePos position, TilePos direction) { Cost = path.Cost + cost; Steps = path.Steps.Concat(new[] { new Step(position, direction, path.GetRotationDifference(direction) > 0) }).ToList(); }
public Step(TilePos position, TilePos direction, bool turned) { Position = position; Direction = direction; Turned = turned; }
public static Entity GetAreaAt(this Pool pool, TilePos position) { return(pool.GetEntityAt(position, x => x.gameObject.Type == ObjectType.Area)); }
private void SetInitialState(TilePos initialPosition, TilePos initialDirection) { _visited = new HashSet <TilePos>(); _pathsToContinue = new List <Path>(); _pathsToContinue.Add(new Path(initialPosition, initialDirection)); }
public static int ToRotation(TilePos direction) { return(DirectionRotation.Single(x => x.Item1 == direction).Item2); }
public static bool OpenTileAt(this Pool pool, TilePos position) { var entitiesAtPosition = pool.GetEntitiesAt(position).ToList(); return(entitiesAtPosition.Any(x => x.gameObject.Type == ObjectType.Tile) && entitiesAtPosition.All(x => !x.isBlockingTile)); }