Ejemplo n.º 1
0
    public void MoveAlongPath()
    {
        int2    pos;
        Vector2 current;

        if (findingPath == true)
        {
            pathFollow = entityManager.GetComponentData <PathFollow>(entity);

            pathPositionBuffer = entityManager.GetBuffer <PathPosition>(entity);



            reachedDestination = false;

            if (pathPositionBuffer.IsCreated == false || pathFollow.NewPath == false)
            {
                //TODO: may want to do something here, as we are waiting currently
                return;
            }
            else
            {
                findingPath        = false;
                pathFollow.NewPath = false;

                //Make sure to update the entity here, as the pathfollow is not a reference!!
                entityManager.SetComponentData(entity, pathFollow);

                if (pathPositionBuffer.Length == 0)
                {
                    Debug.LogError("path buffer has no length");
                    reachedDestination = true;
                    return;
                }


                pos     = pathPositionBuffer[pathFollow.pathIndex].position;
                current = new Vector2((pos.x * chunkSize) + chunkSize / 2, (pos.y * chunkSize) + chunkSize / 2);

                AIMovementHandler.SetDirection(current - (Vector2)transform.position);

                return;
            }
        }



        if (Vector2.Distance(transform.position, AIMovementHandler.targetPosition) < turnStartDistance)
        {
            //We will reach the destanation with movement to spare, so do we just start moving to the next target? lets try that
            pathFollow.pathIndex -= 1;
            if (pathFollow.pathIndex == -1)
            {
                reachedDestination = true;
                return;
            }

            pos     = pathPositionBuffer[pathFollow.pathIndex].position;
            current = new Vector2((pos.x * chunkSize) + chunkSize / 2, (pos.y * chunkSize) + chunkSize / 2);

            AIMovementHandler.SetNewWaypoint(current - (Vector2)transform.position);
        }
    }