Exemplo n.º 1
0
    void BuildShortestPath(List <Vector2> path, NavigationAI.Node node)
    {
        if (node.parent == null)
        {
            return;
        }

        path.Add(node.position);
        BuildShortestPath(path, node.parent);
    }
Exemplo n.º 2
0
    public List <Vector2Int> GetPathFromTo(Vector2Int from, Vector2Int target, bool canDig = false)
    {
        NavigationAI.Node start = navigationGraph.graph[from.x, from.y];
        NavigationAI.Node end   = navigationGraph.graph[target.x, target.y];

        //Get A* sorted list
        Astar(start, end, canDig);

        List <Vector2Int> path = new List <Vector2Int>();

        path.Add(end.positionInt);

        BuildShortestPath(path, end);

        path.Reverse();

        return(path);
    }
Exemplo n.º 3
0
    public List <Vector2> GetPathFromTo(Transform from, Transform target)
    {
        NavigationAI.Node start = navigationGraph.GetClosestNode(from.position);
        NavigationAI.Node end   = navigationGraph.GetClosestNode(target.position);

        //Get A* sorted list
        Astar(start, end);

        List <Vector2> path = new List <Vector2>();

        path.Add(end.position);

        BuildShortestPath(path, end);

        path.Reverse();

        return(path);
    }
Exemplo n.º 4
0
    public List <Vector2> GetPathFromTo(Vector2 from, Vector2 target, bool canDig = false)
    {
        Vector2Int startTile = MapController.Vector2TilePos(from);

        NavigationAI.Node start   = navigationGraph.graph[startTile.x, startTile.y];
        Vector2Int        endTile = MapController.Vector2TilePos(target);

        NavigationAI.Node end = navigationGraph.graph[endTile.x, endTile.y];

        //Get A* sorted list
        Astar(start, end, canDig);

        List <Vector2> path = new List <Vector2>();

        path.Add(end.position);

        BuildShortestPath(path, end);

        path.Reverse();

        return(path);
    }
Exemplo n.º 5
0
    public List <Vector2> GetPathFromTo(Transform from, NavigationAI.Node target, bool canDig = false)
    {
        NavigationAI.Node start = navigationGraph.GetClosestNode(from.position);
        NavigationAI.Node end   = target;

        //Get A* sorted list
        Astar(start, end, canDig);

        List <Vector2> path = new List <Vector2>();

        path.Add(end.position);

        BuildShortestPath(path, end);

        path.Reverse();

        //No path founded
        if (path.Count == 1)
        {
            path = null;
        }

        return(path);
    }
Exemplo n.º 6
0
    // Update is called once per frame
    void Update()
    {
        bool playerSeen = CheckPlayerPresence();

        switch (state)
        {
        case State.IDLE:
            if (playerSeen)
            {
                state = State.MOVE_TOWARDS_PLAYER;
                break;
            }

            //Find new points to patrol
            NavigationAI.Node node = graph.GetRandomPatrolsPoint();
            path = null;
            path = aStart.GetPathFromTo(transform, node, false);
            if (path == null)
            {
            }
            else
            {
                state         = State.MOVING;
                lastsPosition = new List <Vector2>();
            }

            break;

        case State.MOVING:
            if (playerSeen)
            {
                path  = null;
                state = State.MOVE_TOWARDS_PLAYER;
                break;
            }

            if (Vector2.Distance(transform.position, path[0]) < 0.1f)
            {
                path.RemoveAt(0);

                if (path.Count == 0)
                {
                    state = State.IDLE;
                    break;
                }
            }

            Vector2 direction = path[0] - (Vector2)transform.position;

            direction.Normalize();

            body.velocity = speed * direction;

            if (body.velocity.magnitude > maxSpeed)
            {
                body.velocity = body.velocity.normalized * maxSpeed;
            }

            lastsPosition.Add(transform.position);

            if (lastsPosition.Count > 10)
            {
                Vector2 pos       = lastsPosition[lastsPosition.Count - 1];
                bool    notMoving = true;
                for (int i = 1; i < 10; i++)
                {
                    if (pos != lastsPosition[lastsPosition.Count - 1 - i])
                    {
                        notMoving = false;
                        continue;
                    }
                }

                if (notMoving)
                {
                    state = State.IDLE;
                }
            }
            break;

        case State.ATTACKING:
            body.velocity = Vector2.zero;

            timer -= Time.deltaTime;

            if (timer < 0)
            {
                state = State.IDLE;

                Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 1);

                foreach (Collider2D collider in colliders)
                {
                    if (collider.GetComponent <PlayerController>())
                    {
                        collider.GetComponent <PlayerController>().TakeDamage(10);
                    }
                }
            }
            break;

        case State.MOVE_TOWARDS_PLAYER:
            if (Vector2.Distance(transform.position, target.position) < 0.75f)
            {
                state = State.ATTACKING;
                animator.SetTrigger("attack");
                timer = attackTime;
                break;
            }

            if (!playerSeen)
            {
                state = State.IDLE;
                break;
            }


            Vector2 movement2 = (Vector2)target.position - (Vector2)transform.position;

            direction = movement2.normalized;

            body.velocity = direction * 2.5f;
            break;
        }

        if (lookingRight && body.velocity.x < 0)
        {
            animator.SetBool("lookingRight", false);
            lookingRight = false;
        }

        if (!lookingRight && body.velocity.x > 0)
        {
            animator.SetBool("lookingRight", true);
            lookingRight = true;
        }

        animator.SetFloat("speed", body.velocity.x);
    }
Exemplo n.º 7
0
    void Astar(NavigationAI.Node start, NavigationAI.Node end, bool canDig = false)
    {
        if (canDig)
        {
            foreach (NavigationAI.Node node in navigationGraph.graph)
            {
                node.Reset();
                node.SetCost(Vector2.Distance(node.position, end.position));
            }
        }
        else
        {
            foreach (NavigationAI.Node node in navigationGraph.GetGraphOnlyFreeTile())
            {
                node.Reset();
                node.SetCost(Vector2.Distance(node.position, end.position));
            }
        }


        //Make sure start position cost == 0
        start.totalCost = 0;

        List <NavigationAI.Node> openGraph = new List <NavigationAI.Node>();

        openGraph.Add(start);

        do
        {
            openGraph = openGraph.OrderBy(x => x.totalCost + x.cost).ToList();

            NavigationAI.Node node = openGraph.First();
            openGraph.Remove(node);

            foreach (NavigationAI.Node childNode in node.neighbors.OrderBy(x => x.cost + x.totalCost))
            {
                float newCost = node.totalCost + Vector2.Distance(node.position, childNode.position) + childNode.cost;
                if (childNode.visited)
                {
                    continue;
                }
                //if childNode.totalCost = 0 => childNode == end OR if cost is smaller than previous one
                if (childNode.totalCost == 0 || newCost < childNode.totalCost)
                {
                    childNode.SetTotalCost(newCost);
                    childNode.SetParent(node);
                    if (!openGraph.Contains(childNode))
                    {
                        if (!canDig && !childNode.isSolid)
                        {
                            openGraph.Add(childNode);
                        }

                        if (canDig)
                        {
                            openGraph.Add(childNode);
                        }
                    }
                }
            }
            if (node.position == end.position)
            {
                return;
            }
            node.visited = true;
        } while(openGraph.Count != 0);
    }