Example #1
0
    bool CelestialInsideTrajectory()
    {
        int     i            = 0;
        Vector3 celestialPos = IPlantCelestialsSC.getCelestialsQueue().Peek().transform.position;
        // Debug.Log(celestialPos);
        bool a, b, c, d;                                                                //switches so that each statement only can be true once

        a = b = c = d = true;
        for (int j = 0; j < nbrOfTrajectoryPoints; j++)
        {
            int     behindJ       = j + nbrOfTrajectoryPoints - 1;
            int     infrontJ      = j + nbrOfTrajectoryPoints + 1;
            Vector2 behindJVector = trajectoryPoints[behindJ % (nbrOfTrajectoryPoints)] - trajectoryPoints[j];
            Vector2 infronJVector = trajectoryPoints[infrontJ % (nbrOfTrajectoryPoints)] - trajectoryPoints[j];


            float deltaXInfront = Mathf.Abs(trajectoryPoints[j].x - trajectoryPoints[infrontJ % (nbrOfTrajectoryPoints)].x);
            float deltaYInfront = Mathf.Abs(trajectoryPoints[j].y - trajectoryPoints[infrontJ % (nbrOfTrajectoryPoints)].y);
            float deltaXBehind  = Mathf.Abs(trajectoryPoints[j].x - trajectoryPoints[behindJ % (nbrOfTrajectoryPoints)].x);
            float deltaYBehind  = Mathf.Abs(trajectoryPoints[j].y - trajectoryPoints[behindJ % (nbrOfTrajectoryPoints)].y);


            float shorterDX = deltaXInfront < deltaXBehind ? deltaXInfront : deltaXBehind;
            float longerDX  = deltaXInfront > deltaXBehind ? deltaXInfront : deltaXBehind;
            float shorterDY = deltaYInfront < deltaYBehind ? deltaYInfront : deltaYBehind;
            float longerDY  = deltaYInfront > deltaYBehind ? deltaYInfront : deltaYBehind;

            if (Mathf.Abs(trajectoryPoints[j].y - celestialPos.y) <= shorterDY / 2 || Mathf.Abs(trajectoryPoints[j].y - celestialPos.y) <= longerDY / 2)
            {
                if (trajectoryPoints[j].x > celestialPos.x && a)
                {
                    i++;
                    a = false;
                }
                else if (trajectoryPoints[j].x < celestialPos.x && b)
                {
                    i++;
                    b = false;
                }
            }
            else if (Mathf.Abs(trajectoryPoints[j].x - celestialPos.x) <= shorterDX / 2 || Mathf.Abs(trajectoryPoints[j].x - celestialPos.x) <= longerDX / 2)
            {
                if (trajectoryPoints[j].y > celestialPos.y && c)
                {
                    i++;
                    c = false;
                }
                else if (trajectoryPoints[j].y < celestialPos.y && d)
                {
                    i++;
                    d = false;
                }
            }
        }
        Debug.Log("i: " + i + ", a: " + a + ", b: " + b + ", c: " + c + ", d: " + d);
        if (i == 4)
        {
            successfulCelestial = IPlantCelestialsSC.getCelestialsQueue().Peek();
            camera2DFollowSC.SetTarget(successfulCelestial.transform.position);
            camera2DFollowSC.SpeedIncrease();
            celestialInside = true;
            return(true);
        }
        else
        {
            i = 0;
        }
        return(false);
    }