Exemplo n.º 1
0
    private IEnumerator MoveEnemyPawnsPairRoutine(EnemyPawnsPair pair, List <Vector2Int> path)
    {
        if (path.Count < 1)
        {
            yield break;
        }
        Vector2Int     pawn1EndCell;
        PawnTransition pawn1Transition = new PawnTransition(pair.Pawn1, _tilemap, pawn1EndCell = path.PickLastElement());
        PawnTransition pawn2Transition = new PawnTransition(pair.Pawn2, _tilemap, path.Count > 0 ? path.PickFirstElement() : pawn1EndCell);
        float          duration        = 0.3f;

        yield return(CoroutineUtils.UpdateRoutine(duration, (elapsedTime, transition) => {
            float shift = Maths.GetTransition(TransitionType.COS_IN_PI_RANGE, transition);
            pawn1Transition.Update(shift);
            pawn2Transition.Update(shift);
        }));

        pawn1Transition.Finish(_tilemap);
        pawn2Transition.Finish(_tilemap);
    }
Exemplo n.º 2
0
    public IEnumerator MoveEnemyPawnsRoutine(Action <bool> onEnd, Action onSuccess, Action onFail)
    {
        int  surroundedEnemiesCount = 0;
        int  movedEnemiesCount      = 0;
        bool enemyReachedTarget     = false;

        _pathfinder.ClearSprites();

        List <PathResult>     pathResults     = new List <PathResult>();
        List <PawnTransition> pawnTransitions = new List <PawnTransition>();

        foreach (var enemyPawn in _enemySingles)
        {
            PathResult pathResult = null;
#if DEBUG_PATHFINDING
            yield return(_pathfinder.FindPathRoutine(enemyPawn, enemyPawn.Target, aPathResult => {
                pathResult = aPathResult;
            }, pawnTransitions, _playerPawnsContainer, _enemyPawnsContainer, _enemyTargetsContainer));
#else
            pathResult = _pathfinder.FindPath(enemyPawn, enemyPawn.Target, pawnTransitions,
                                              _playerPawnsContainer, _enemyPawnsContainer, _enemyTargetsContainer, _enemyPawnsPathSpritesContainer);
#endif
            pathResults.Add(pathResult);
            var path = pathResult.Path;
            if (pathResult.PathFound && path.Count > 0)
            {
                _pathfinder.CreatePathSprites(path, 1, 1, Color.white);
                PawnTransition pawnTransition = new PawnTransition(enemyPawn, _tilemap, path.PickLastElement());
                pawnTransitions.Add(pawnTransition);
            }
        }
        if (pawnTransitions.Count > 0)
        {
            yield return(MoveEnemyPawnsRoutine(pawnTransitions));

            movedEnemiesCount += pawnTransitions.Count;
        }
        for (int i = 0; i < pathResults.Count; i++)
        {
            EnemyPawn  enemyPawn  = _enemyPawns[i];
            PathResult pathResult = pathResults[i];
            var        path       = pathResult.Path;
            if (pathResult.PathFound)
            {
                if (path.Count <= 1)
                {
                    enemyReachedTarget = true;
                    break;
                }
            }
            else
            {
                bool targetSurrounded = !_pathfinder.FindPathToBoundsMin(enemyPawn.Target, _playerPawnsContainer, _enemyTargetsContainer).PathFound;
                bool enemySurrounded  = targetSurrounded ? !_pathfinder.FindPathToBoundsMin(enemyPawn, _playerPawnsContainer).PathFound : true;
                surroundedEnemiesCount += enemySurrounded ? 1 : 0;
            }
        }
        foreach (var pair in _enemyPairs)
        {
            EnemyPawn  enemyPawnA = pair.Pawn1;
            EnemyPawn  enemyPawnB = pair.Pawn2;
            PathResult pathResult = null;
#if DEBUG_PATHFINDING
            yield return(_pathfinder.FindPathRoutine(enemyPawnA, enemyPawnB, aPathResult => {
                pathResult = aPathResult;
            }, null, _playerPawnsContainer, _enemyPawnsContainer, _enemyTargetsContainer));
#else
            pathResult = _pathfinder.FindPath(enemyPawnA, enemyPawnB, null, _playerPawnsContainer, _enemyPawnsContainer, _enemyTargetsContainer);
#endif
            if (pathResult.PathFound)
            {
                var path = pathResult.Path;
                if (path.Count > 0)
                {
                    path.RemoveAt(0);
                    if (path.Count > 0)
                    {
                        List <SpriteRenderer> pathSprites = _pathfinder.CreatePathSprites(path, 1, 1, Color.white);
                        yield return(MoveEnemyPawnsPairRoutine(pair, path));

                        movedEnemiesCount += 2;
                    }
                    if (path.Count < 1)
                    {
                        enemyReachedTarget = true;
                        break;
                    }
                }
            }
            else
            {
                bool pawnASurrounded = !_pathfinder.FindPathToBoundsMin(enemyPawnA, _playerPawnsContainer, _enemyTargetsContainer).PathFound;
                bool pawnBSurrounded = !_pathfinder.FindPathToBoundsMin(enemyPawnB, _playerPawnsContainer, _enemyTargetsContainer).PathFound;
                surroundedEnemiesCount += pawnASurrounded ? 1 : 0;
                surroundedEnemiesCount += pawnBSurrounded ? 1 : 0;
            }
        }
        if (enemyReachedTarget)
        {
            yield return(new WaitForSeconds(0.5f));

            onFail?.Invoke();
        }
        else if (surroundedEnemiesCount == _enemyPawns.Count)
        {
            yield return(new WaitForSeconds(0.5f));

            onSuccess?.Invoke();
        }
        onEnd?.Invoke(movedEnemiesCount > 0);
    }