Exemplo n.º 1
0
    private IEnumerator StaggerdPathThroughMaze(Node lastNode)
    {
        foreach (Node waypoint in Spawner.waypoints)
        {
            List <Node> pathingPoints = PathFinding.AStar(lastNode, waypoint);

            yield return(new WaitForEndOfFrame());

            foreach (Node pathingPoint in pathingPoints)
            {
                moveQue.Add(pathingPoint);
            }
            lastNode = moveQue[moveQue.Count - 1];

            yield return(new WaitForSeconds(.05F));
        }

        yield return(new WaitForSeconds(.1F));

        List <Node> toEndPathPoints = PathFinding.AStar(lastNode, endNode);

        yield return(new WaitForEndOfFrame());

        foreach (Node pathingPoint in toEndPathPoints)
        {
            moveQue.Add(pathingPoint);
        }
    }
Exemplo n.º 2
0
        public void UpdatePath()
        {
            currentField = currentLevel.Fields[ApproximatePosition()];
            List <Field> path = PathFinding.AStar(currentField, currentLevel.Fields[goal], PathFinding.EmptyHeuristic /*ManhattanHeuristic*/);

            if (path.Count > 1)
            {
                velocity = (path[1].transform.position - transform.position).normalized;
                if (velocity.x > 0)
                {
                    GetComponent <SpriteRenderer>().flipX = false;
                }
                else if (velocity.x < 0)
                {
                    GetComponent <SpriteRenderer>().flipX = true;
                }
            }
            else if (path[0].transform.position == new Vector3(goal.x, goal.y, path[0].transform.position.z))
            {
                currentLevel.LemmingsEscape();
                GetComponent <AudioSource>().PlayOneShot(clips[1]);
                StartCoroutine(CommitSuicide());
            }
            else
            {
                velocity = new Vector3(0, 0, 0);
            }
        }
Exemplo n.º 3
0
        protected void Go(double updateTime, bool onlyOne = false)
        {
            // ruszanie sie zwierzecia
            if ((GoToField != null))
            {
                // Stworzenie sciezki i wybranie pola na ktore ma sie zwierze poruszyc
                if ((isOnfield_ && (path_ == null)) || counter_ == 0)
                {
                    try {
                        path_    = PathFinding.AStar(Location, GoToField, heuristic_);
                        counter_ = 5;
                    } catch (Exception) {
                        return;
                    }
                }

                if (!IsMoveing && path_ != null)
                {
                    path_.RemoveAt(0);
                    GoToField = path_[0];
                    --counter_;
                }

                if (onlyOne)
                {
                    path_ = null;
                }

                IsMoveing = true;

                // obliczenie ile dotychczasowej drogi miedzy dwoma polami przebyla jednostka
                moved_ += (float)(updateTime * MoveSpeed * Location.MoveSpeed);

                // obliczenie na tej podstawei pozycji na mapie
                ScreenPosition = new Vector2f((float)Lerp(Location.Center.X, GoToField.Center.X, moved_),
                                              (float)Lerp(Location.Center.Y, GoToField.Center.Y, moved_));

                // jesli przebyla cala droge zmiana jej lokacji
                if (moved_ >= 1.0f)
                {
                    Location = GoToField;
                }
            }

            if (GoToField == Location)
            {
                if (path_?.Count == 1)
                {
                    path_ = null;
                }
                GoToField  = null;
                IsMoveing  = false;
                isOnfield_ = true;
                moved_     = 0.0f;
            }
        }
Exemplo n.º 4
0
    // Use this for initialization
    void Start()
    {
        currentNode = 0;
        StartCoroutine(HacerCambio());

        List <Node> breadthwise = PathFinding.Breadthwise(start, end);
        List <Node> depthwise   = PathFinding.Depthwise(start, end);
        List <Node> aStar       = PathFinding.AStar(start, end);

        for (int i = 0; i < breadthwise.Count; i++)
        {
            print(aStar[i].transform.name + " " + aStar[i].F);
        }
    }
Exemplo n.º 5
0
        public override void OnScannedRobot(ScannedRobotEvent evnt)
        {
            _evnt = evnt;


            // Get enemy X,Y coordinates
            var absoluteBearing = HeadingRadians + evnt.BearingRadians;

            var enemyX = X + Math.Sin(absoluteBearing) * evnt.Distance;
            var enemyY = Y + Math.Cos(absoluteBearing) * evnt.Distance;

            var myX = (int)(X / 50);
            var myY = (int)(Y / 50);

            var myNode = new Node(11 - myY, myX);

            var enemyNode = new Node(11 - ((int)enemyY / 50), (int)enemyX / 50);

            Console.WriteLine("EnemeyX: " + enemyNode.X + " EnemyY: " + enemyNode.Y);


            if (_graph.NodeGraph[enemyNode.X, enemyNode.Y].Connections.Count > 0 && _findPath)
            {
                _path = _pathfinder.AStar(_graph, _graph.NodeGraph[myNode.X, myNode.Y],
                                          _graph.NodeGraph[enemyNode.X, enemyNode.Y], new Heuristics(enemyNode));
                _lenght      = 0;
                _findPath    = false;
                _readyToMove = true;
            }

            Console.WriteLine("\n\n_________PATH________");
            foreach (var node in _path)
            {
                Console.WriteLine("Coords: " + node.GetToNode().X + ", " + node.GetToNode().Y);
            }


            // Radar lock
            var angleToEnemy = HeadingRadians + evnt.BearingRadians;
            var radarTurn    = Utils.NormalRelativeAngle(angleToEnemy - RadarHeadingRadians);
            var extraTurn    = Math.Min(Math.Atan(50.0 / evnt.Distance), Rules.RADAR_TURN_RATE_RADIANS);

            radarTurn += (radarTurn < 0 ? -extraTurn : extraTurn);

            SetTurnRadarRightRadians(radarTurn);
        }
Exemplo n.º 6
0
    /// <summary>
    /// Organizes the specified amphipods into the correct burrows.
    /// </summary>
    /// <param name="diagram">The diagram of the burrows occupied by the amphipods.</param>
    /// <param name="unfoldDiagram">Whether to unfold the diagram to reveal the missing lines.</param>
    /// <returns>
    /// The least energy required to organize the amphipods.
    /// </returns>
    public static int Organize(IList <string> diagram, bool unfoldDiagram)
    {
        string amber  = $"{diagram[2][3]}{diagram[3][3]}";
        string bronze = $"{diagram[2][5]}{diagram[3][5]}";
        string copper = $"{diagram[2][7]}{diagram[3][7]}";
        string desert = $"{diagram[2][9]}{diagram[3][9]}";

        if (unfoldDiagram)
        {
            amber  = amber.Insert(1, "DD");
            bronze = bronze.Insert(1, "CB");
            copper = copper.Insert(1, "BA");
            desert = desert.Insert(1, "AC");
        }

        string emptyHallway = new(' ', 11);
        int    depth        = unfoldDiagram ? 4 : 2;

        var start = new State(emptyHallway, amber, bronze, copper, desert);
        var goal  = new State(emptyHallway, new('A', depth), new('B', depth), new('C', depth), new('D', depth));

        return((int)PathFinding.AStar(new Burrow(), start, goal));
    }
Exemplo n.º 7
0
 public void MakePath()
 {
     moveQue = PathFinding.AStar(startNode, endNode);
 }
Exemplo n.º 8
0
    // Update is called once per frame
    void Update()
    {
        if (currentState != State.DEFAULT)
        {
            if (pacman == null)
            {
                pacman = GameObject.FindGameObjectWithTag("pacman");
            }
            if (Time.time - startTime >= currentEndTime && currentEndIndex < waveEndTimes.Length && currentState != State.FRIGHTENED && respawn == false)
            {
                currentEndIndex++;
                currentEndTime = waveEndTimes.Length > currentEndIndex ? waveEndTimes[currentEndIndex] : -1f;
                currentState   = waveStates[currentEndIndex];
                startTime      = Time.time;
            }
            else if (currentState == State.FRIGHTENED)
            {
                if (Time.time - startTime >= frightenedTime)
                {
                    currentState = waveStates[currentEndIndex];
                    startTime    = Time.time;
                    animator.SetBool("flash", false);
                }
                // End of state is near
                else if (frightenedTime - (Time.time - startTime) <= 2f)
                {
                    animator.SetBool("flash", true);
                }
            }

            // Ghost reached respawn
            if (respawn == true && currentNode == pathFinder.grid[13][12])
            {
                GetComponent <SpriteRenderer>().color = Color.white;
                respawn = false;
            }

            if (lerpTime > 1f)
            {
                currentNode = pathFinder.GetNodeInDirection(currentNode, direction);
            }

            HandleCollisions();

            if ((pathFinder.IsNodeTurnable(currentNode, respawn) || pathFinder.GetNodeInDirection(currentNode, direction).isWall) && (lerpTime > 1f || lerpTime == 0f))
            {
                switch (currentState)
                {
                case State.CHASE:

                    if (respawn == false)
                    {
                        DetermineTargetForChase();
                    }

                    currentPath = pathFinder.AStar(currentNode, targetPoint, direction);
                    direction   = GetDirectionBetweenNodes(currentNode, currentPath[1]);
                    break;

                case State.FRIGHTENED:
                    currentPath = null;

                    List <Node> neighbors = pathFinder.GetNeighbors(currentNode);

                    List <Direction> possibleDirections = new List <Direction>();
                    foreach (Node neighbor in neighbors)
                    {
                        if (!neighbor.isWall)
                        {
                            if (neighbor.pos.y > currentNode.pos.y && direction != Direction.Down)
                            {
                                possibleDirections.Add(Direction.Up);
                            }
                            if (neighbor.pos.y < currentNode.pos.y && direction != Direction.Up)
                            {
                                possibleDirections.Add(Direction.Down);
                            }
                            if (neighbor.pos.x > currentNode.pos.x && direction != Direction.Left)
                            {
                                possibleDirections.Add(Direction.Right);
                            }
                            if (neighbor.pos.x < currentNode.pos.x && direction != Direction.Right)
                            {
                                possibleDirections.Add(Direction.Left);
                            }
                        }
                    }

                    direction = possibleDirections[Random.Range(0, possibleDirections.Count)];

                    break;

                case State.SCATTER:
                    GetScatterTarget();
                    currentPath = pathFinder.AStar(currentNode, targetPoint, direction);

                    direction = GetDirectionBetweenNodes(currentNode, currentPath[1]);
                    break;

                default:

                    break;
                }

                if (currentState != State.FRIGHTENED)
                {
                    switch (direction)
                    {
                    case (Direction.Right):
                        animator.SetTrigger("goright");
                        break;

                    case (Direction.Left):
                        animator.SetTrigger("goleft");
                        break;

                    case (Direction.Up):
                        animator.SetTrigger("goup");
                        break;

                    case (Direction.Down):
                        animator.SetTrigger("godown");
                        break;
                    }
                }
            }
            transform.position = PathFollow();

            if (currentState == State.FRIGHTENED)
            {
                lerpTime += Time.deltaTime * maxVelocity * 0.5f;
            }
            else
            {
                lerpTime += Time.deltaTime * maxVelocity;
            }
        }
        else
        {
            if (startExitTime + exitTime <= Time.time)
            {
                currentState = waveStates[currentEndIndex];
            }
            else if (Time.time - startTime >= currentEndTime && currentEndIndex < waveEndTimes.Length)
            {
                currentEndIndex++;
                currentEndTime = waveEndTimes.Length > currentEndIndex ? waveEndTimes[currentEndIndex] : -1f;
                startTime      = Time.time;
            }
        }
    }
Exemplo n.º 9
0
    // Update is called once per frame
    void Update()
    {
        if (!initialized)
        {
            return;
        }

        if (ghostMap["blinky"].getGhostObject() == null)
        {
            Init();
        }

        if (pacman == null)
        {
            pacman = GameObject.FindGameObjectWithTag("pacman");
        }

        pacmanNode = pathFinder.WorldPosToNode(pacman.transform.position);



        foreach (GhostData data in ghostMap.Values)
        {
            if (data.getGhostState() != State.DEFAULT)
            {
                if (Time.time - startTime >= currentEndTime && currentEndIndex < waveEndTimes.Length && currentEndTime != -1f && data.getGhostState() != State.FRIGHTENED && !data.getRespawn())
                {
                    currentEndIndex++;
                    currentEndTime = waveEndTimes.Length > currentEndIndex ? waveEndTimes[currentEndIndex] : -1f;
                    foreach (GhostData data2 in ghostMap.Values)
                    {
                        if (data2.getGhostState() != State.DEFAULT)
                        {
                            data2.setGhostState(waveStates[currentEndIndex]);
                        }
                    }
                    startTime = Time.time;
                }
                else if (data.getGhostState() == State.FRIGHTENED)
                {
                    if (Time.time - startTime >= frightenedTime)
                    {
                        foreach (GhostData data2 in ghostMap.Values)
                        {
                            data2.getGhostAnimator().SetBool("flash", false);
                            if (data2.getGhostState() != State.DEFAULT)
                            {
                                data2.setGhostState(waveStates[currentEndIndex]);
                            }
                        }

                        //data.setGhostState(waveStates[currentEndIndex]);
                        startTime = Time.time;
                    }
                    // End of state is near
                    else if (frightenedTime - (Time.time - startTime) <= 2f)
                    {
                        data.getGhostAnimator().SetBool("flash", true);
                    }
                }

                // Ghost reached respawn
                if (data.getRespawn() == true && data.getCurrentNode() == pathFinder.grid[13][12])
                {
                    data.getGhostObject().GetComponent <SpriteRenderer>().color = Color.white;
                    data.setRespawn(false);
                }


                if (data.getLerpTime() > 1f)
                {
                    data.setCurrentNode(pathFinder.GetNodeInDirection(data.getCurrentNode(), data.getDirection()));
                }

                HandleCollisions(data);

                if ((pathFinder.IsNodeTurnable(data.getCurrentNode(), data.getRespawn()) || pathFinder.GetNodeInDirection(data.getCurrentNode(), data.getDirection()).isWall) && (data.getLerpTime() > 1f || data.getLerpTime() == 0f))
                {
                    switch (data.getGhostState())
                    {
                    case State.CHASE:
                        Node targetPoint;
                        if (data.getRespawn())
                        {
                            targetPoint = pathFinder.grid[13][12];
                        }
                        else
                        {
                            targetPoint = DetermineTargetForChase(data.getName(), data.getRespawn());
                        }

                        List <Node> currentPath = pathFinder.AStar(data.getCurrentNode(), targetPoint, data.getDirection());

                        data.setDirection(GetDirectionBetweenNodes(data.getCurrentNode(), currentPath[1]));
                        break;

                    case State.FRIGHTENED:
                        currentPath = null;


                        List <Node>      neighbors          = pathFinder.GetNeighbors(data.getCurrentNode());
                        List <Direction> possibleDirections = new List <Direction>();
                        foreach (Node neighbor in neighbors)
                        {
                            if (!neighbor.isWall)
                            {
                                if (neighbor.pos.y > data.getCurrentNode().pos.y&& data.getDirection() != Direction.Down)
                                {
                                    possibleDirections.Add(Direction.Up);
                                }
                                if (neighbor.pos.y < data.getCurrentNode().pos.y&& data.getDirection() != Direction.Up)
                                {
                                    possibleDirections.Add(Direction.Down);
                                }
                                if (neighbor.pos.x > data.getCurrentNode().pos.x&& data.getDirection() != Direction.Left)
                                {
                                    possibleDirections.Add(Direction.Right);
                                }
                                if (neighbor.pos.x < data.getCurrentNode().pos.x&& data.getDirection() != Direction.Right)
                                {
                                    possibleDirections.Add(Direction.Left);
                                }
                            }
                        }

                        data.setDirection(possibleDirections[Random.Range(0, possibleDirections.Count)]);

                        break;

                    case State.SCATTER:
                        Node scatterPoint = GetScatterTarget(data.getName());
                        currentPath = pathFinder.AStar(data.getCurrentNode(), scatterPoint, data.getDirection());
                        data.setDirection(GetDirectionBetweenNodes(data.getCurrentNode(), currentPath[1]));
                        break;

                    default:

                        break;
                    }

                    if (data.getGhostState() != State.FRIGHTENED)
                    {
                        switch (data.getDirection())
                        {
                        case (Direction.Right):
                            data.getGhostAnimator().SetTrigger("goright");
                            break;

                        case (Direction.Left):
                            data.getGhostAnimator().SetTrigger("goleft");
                            break;

                        case (Direction.Up):
                            data.getGhostAnimator().SetTrigger("goup");
                            break;

                        case (Direction.Down):
                            data.getGhostAnimator().SetTrigger("godown");
                            break;
                        }
                    }
                }

                data.getGhostObject().transform.position = LerpMovement(data);

                data.setLerpTime(data.getLerpTime() + Time.deltaTime * (data.getGhostState() == State.FRIGHTENED ? maxVelocity * 0.5f : maxVelocity));
            }
            else
            {
                if (startExitTime + data.getExitTime() <= Time.time)
                {
                    data.setGhostState(waveStates[currentEndIndex]);
                }
                if (Time.time - startTime >= currentEndTime && currentEndIndex < waveEndTimes.Length)
                {
                    currentEndIndex++;
                    currentEndTime = waveEndTimes.Length > currentEndIndex ? waveEndTimes[currentEndIndex] : -1f;
                    startTime      = Time.time;
                }
            }
        }
    }
Exemplo n.º 10
0
    void DrawPath()
    {
        if (selectedUnit == null || selectedUnit.state != Unit.UnitState.Friendly)
        {
            return;
        }
        List <Tile> path;
        int         lap = 0;
        int         pathCost;
        Tile        start = selectedUnit.standingTile;

        while (Input.GetMouseButton(1))
        {
            if (lap < 1)
            {
                path = PathFinding.AStar(map, selectedUnit.standingTile, Hoover.hooveredTile, out pathCost);
                int length = path.Count;
                //cc/cu/cl/cd/cr.../pv/ph..../eld/elu/eru/erd/....fu/fl/fd/fr



                for (int i = 0; i < path.Count; i++)
                {
                    Tile    currentTile = path[i];
                    Vector3 currentPos  = currentTile.transform.position;
                    if (i == 0)
                    {
                        if (i == path.Count - 1)
                        {
                            instantited.Add(Instantiate(prefabs[0], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        Tile    nextTile = path[i + 1];
                        Vector3 nextTPos = nextTile.transform.position;
                        if (currentPos.y + 1 == nextTPos.y)
                        {
                            instantited.Add(Instantiate(prefabs[1], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.x + 1 == nextTPos.x)
                        {
                            instantited.Add(Instantiate(prefabs[2], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.y - 1 == nextTPos.y)
                        {
                            instantited.Add(Instantiate(prefabs[3], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.x - 1 == nextTPos.x)
                        {
                            instantited.Add(Instantiate(prefabs[4], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                    }
                    else if (i == path.Count - 1)
                    {
                        Tile    prevTile = path[i - 1];
                        Vector3 prevTPos = prevTile.transform.position;

                        if (currentPos.y == prevTPos.y + 1)
                        {
                            instantited.Add(Instantiate(prefabs[11], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.x == prevTPos.x + 1)
                        {
                            instantited.Add(Instantiate(prefabs[12], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.y == prevTPos.y - 1)
                        {
                            instantited.Add(Instantiate(prefabs[13], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.x == prevTPos.x - 1)
                        {
                            instantited.Add(Instantiate(prefabs[14], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                    }
                    else
                    {
                        Tile    prevTile = path[i - 1];
                        Tile    nextTile = path[i + 1];
                        Vector3 nextPos  = nextTile.transform.position;
                        Vector3 prevPos  = prevTile.transform.position;

                        if (currentPos.x == prevPos.x && currentPos.x == nextPos.x)
                        {
                            instantited.Add(Instantiate(prefabs[5], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.y == prevPos.y && currentPos.y == nextPos.y)
                        {
                            instantited.Add(Instantiate(prefabs[6], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        //-------------------------------
                        else if (currentPos.x == prevPos.x - 1 && currentPos.y == nextPos.y - 1 || currentPos.x == nextPos.x - 1 && currentPos.y == prevPos.y - 1)
                        {
                            instantited.Add(Instantiate(prefabs[7], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.x == prevPos.x - 1 && currentPos.y == nextPos.y + 1 || currentPos.x == nextPos.x - 1 && currentPos.y == prevPos.y + 1)
                        {
                            instantited.Add(Instantiate(prefabs[8], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.x == prevPos.x + 1 && currentPos.y == nextPos.y + 1 || currentPos.x == nextPos.x + 1 && currentPos.y == prevPos.y + 1)
                        {
                            instantited.Add(Instantiate(prefabs[9], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                        else if (currentPos.x == prevPos.x + 1 && currentPos.y == nextPos.y - 1 || currentPos.x == nextPos.x + 1 && currentPos.y == prevPos.y - 1)
                        {
                            instantited.Add(Instantiate(prefabs[8], currentPos, Quaternion.identity, this.transform));
                            continue;
                        }
                    }
                }
            }
            if (hoover.HasChange())
            {
                Hoover.hooveredTile.terrain.movementCost
            }
            lap++;
        }
    }