Beispiel #1
0
        protected void LookAtNearestEnemy(List <GameObject> enemies)
        {
            if (enemies.Count < 1)
            {
                return;
            }

            float minDistance       = DirectionAndDistanceCalculator.CalculateDistance(player.transform.Get2DPosition(), enemies[0].transform.Get2DPosition());
            int   nearestEnemyIndex = 0;

            for (int i = 1; i < enemies.Count; i++)
            {
                if (DirectionAndDistanceCalculator.CalculateDistance(player.transform.Get2DPosition(), enemies[i].transform.Get2DPosition()) < minDistance)
                {
                    minDistance       = DirectionAndDistanceCalculator.CalculateDistance(player.transform.Get2DPosition(), enemies[i].transform.Get2DPosition());
                    nearestEnemyIndex = i;
                }
            }

            Vector2 enemyPos = enemies[nearestEnemyIndex].transform.Get2DPosition();

            if ((player.transform.Get2DPosition().IsAtLeftOf(enemyPos) && !player.IsFacingRight) || (!player.transform.Get2DPosition().IsAtLeftOf(enemyPos) && player.IsFacingRight))
            {
                player.Flip();
            }
        }
Beispiel #2
0
    public Vector2[] RefinePath(GameObject go, Vector2[] path, LayerMask unwalkableLayer)
    {
        if (path == null || path.Length <= 0)
        {
            return(path);
        }

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

        int i = 1;

        while (i < refinedPath.Count)
        {
            Vector2 startPos  = refinedPath[i - 1];
            Vector2 endPos    = refinedPath[i];
            Vector2 direction = DirectionAndDistanceCalculator.CalculateSignedDirection(startPos, endPos);
            if (direction.x != 0 && direction.y != 0)
            {
                if (ObstacleFinder.Instance.CheckObstacles(go, startPos, endPos, unwalkableLayer, out colliderPosition))
                {
                    Vector2 middle = DirectionAndDistanceCalculator.GetMiddlePosOfVector(startPos, endPos);
                    Vector2 newPoint;
                    if (colliderPosition.IsAbove(middle))
                    {
                        if (direction.y > 0)
                        {
                            newPoint = new Vector2(endPos.x, startPos.y);
                        }
                        else
                        {
                            newPoint = new Vector2(startPos.x, endPos.y);
                        }
                    }
                    else
                    {
                        if (direction.y > 0)
                        {
                            newPoint = new Vector2(startPos.x, endPos.y);
                        }
                        else
                        {
                            newPoint = new Vector2(endPos.x, startPos.y);
                        }
                    }
                    refinedPath.Insert(i, newPoint);
                }
            }
            i++;
        }
        return(refinedPath.ToArray());
    }
Beispiel #3
0
        protected bool IsFacing(Vector2 playerPos, Vector2 enemyPos)
        {
            float angle           = DirectionAndDistanceCalculator.CalculateAngle(playerPos, enemyPos);
            float playerDirection = player.IsFacingRight ? 0 : Mathf.PI; // this will depend on the blend tree

            float rawfinal = playerDirection - angle;
            float final    = Mathf.Abs(rawfinal);

            if (Mathf.Abs(rawfinal - 2 * Mathf.PI) < final)
            {
                final = Mathf.Abs(rawfinal - 2 * Mathf.PI);
            }
            return(final < Mathf.PI / 2);
        }
Beispiel #4
0
        // todo : random next patrol point
        protected void Patrol()
        {
            if (movementHandler.PathEmpty())
            {
                PathRequestManager.RequestPath(enemy.transform.position, enemy.PatrolWayPoints[nextWaypointIndex].Get2DPosition(), enemy.gameObject, enemy.UnwalkableLayer, OnPathFound);
            }
            if (patrolWaitTimer > 0)
            {
                patrolWaitTimer -= Time.deltaTime;
                return;
            }

            FlipIfNeeded();
            enemy.Anim.SetBool(enemy.WalkingAnimationBool, true);
            movementHandler.MoveAlongPath();
            if (DirectionAndDistanceCalculator.CalculateDistance(enemy.transform.Get2DPosition(), enemy.PatrolWayPoints[nextWaypointIndex].Get2DPosition()) < GameManager.Instance.aiReachingPrecision)
            {
                nextWaypointIndex = (nextWaypointIndex + 1) % enemy.PatrolWayPoints.Length;
                PathRequestManager.RequestPath(enemy.transform.position, enemy.PatrolWayPoints[nextWaypointIndex].Get2DPosition(), enemy.gameObject, enemy.UnwalkableLayer, OnPathFound);
                patrolWaitTimer = enemy.PatrolDelay;
                enemy.Anim.SetBool(enemy.WalkingAnimationBool, false);
            }
        }
Beispiel #5
0
 protected bool PlayerInChasingRange()
 {
     return(DirectionAndDistanceCalculator.CalculateDistance(enemy.transform.Get2DPosition(), enemy.Player.Get2DPosition()) <= enemy.chasingRange);
 }
 public Vector2 GetMovementDirection()
 {
     return(DirectionAndDistanceCalculator.CalculateSignedDirection(gameObject.transform.Get2DPosition(), CurrentWayPoint));
 }
Beispiel #7
0
        protected bool CheckObstables()
        {
            Vector2 castDirection = DirectionAndDistanceCalculator.CalculateDirection(player.transform.Get2DPosition(), rollTargetPosition);

            return(Physics2D.CircleCast(player.transform.Get2DPosition(), 0.15f, castDirection, 0.25f, GameManager.Instance.UnwalkableLayer));
        }
    public bool CheckObstacles(GameObject go, Vector2 startPos, Vector2 targetPos, LayerMask unwalkableLayer, out Vector2 obstaclePosition)
    {
        BoxCollider2D boxCollider = go.GetComponent <BoxCollider2D>();
        Vector2       unitScale   = new Vector2(Mathf.Abs(go.transform.localScale.x), Mathf.Abs(go.transform.localScale.y));
        RaycastHit2D  hit;
        Vector2       rayOrigin;
        Vector2       boxColliderCenter = new Vector2(startPos.x - boxCollider.offset.x * unitScale.x, startPos.y + boxCollider.offset.y * unitScale.y);
        float         xGap = (boxCollider.size.x * unitScale.x) / 2;
        float         yGap = (boxCollider.size.y * unitScale.y) / 2;
        Vector2       rayCastingCenter;
        Vector2       direction = DirectionAndDistanceCalculator.CalculateDirection(startPos, targetPos);
        float         distance  = DirectionAndDistanceCalculator.CalculateDistance(startPos, targetPos);

        if (direction.x == 0)    // walking vertically
        {
            if (direction.y > 0) // going up
            {
                rayCastingCenter = boxColliderCenter + new Vector2(0, yGap);
            }
            else // going down
            {
                rayCastingCenter = boxColliderCenter - new Vector2(0, yGap);
            }

            for (int i = -1; i < 2; i++)
            {
                rayOrigin = rayCastingCenter + new Vector2(i * xGap, 0);
                hit       = Physics2D.Raycast(rayOrigin, direction, distance, unwalkableLayer);
                Debug.DrawLine(rayOrigin, rayOrigin + direction);
                if (hit.collider != null)
                {
                    //Debug.Log(go.name + " is gonna hit an obstacle !");
                    obstaclePosition = hit.point;
                    return(true);
                }
            }
        }
        else
        {
            if (direction.x > 0)
            {
                rayCastingCenter = boxColliderCenter + new Vector2(xGap, 0);
            }
            else
            {
                rayCastingCenter = boxColliderCenter - new Vector2(xGap, 0);
            }
            for (int i = -1; i < 2; i++)
            {
                rayOrigin = rayCastingCenter + new Vector2(0, i * yGap);
                hit       = Physics2D.Raycast(rayOrigin, direction, distance, unwalkableLayer);
                Debug.DrawLine(rayOrigin, rayOrigin + direction);
                if (hit.collider != null)
                {
                    //Debug.Log(go.name + " is gonna hit an obstacle !");
                    obstaclePosition = hit.point;
                    return(true);
                }
            }

            if (direction.y != 0)
            {
                Vector2 lastRayOrigin = boxColliderCenter + new Vector2(-xGap * Math.Sign(direction.x), yGap * Math.Sign(direction.y));
                hit = Physics2D.Raycast(lastRayOrigin, direction, distance, unwalkableLayer);
                Debug.DrawLine(lastRayOrigin, lastRayOrigin + direction);
                if (hit.collider != null)
                {
                    //Debug.Log(go.name + " is gonna hit an obstacle !");
                    obstaclePosition = hit.point;
                    return(true);
                }
            }
        }

        obstaclePosition = new Vector2(short.MinValue, short.MinValue);
        return(false);
    }