コード例 #1
0
        // Use this for initialization
        void Start()
        {
            currentTime = clock.getCurrentTime();
            //recordar que tanto el sol como la luna se tienen que dejar en el editor en la posicion de medianoche

            Time startDaySunrise = clock.getSunriseTime();
            Time startDaySunset  = clock.getSunsetTime();

            Time start = previousTime;
            Time end   = clock.getCurrentTime();

            if (debug)
            {
                Debug.Log("INITIALIZING SUN/MOON POSITION-----------------------");
                Debug.Log("dayTime: " + daytime + "\n" +
                          "start: " + start.ToString() + "\t end: " + end.ToString());
            }

            //the first speed will always be the night speed becouse we are forcing the user to set the environment as midnight
            //TODO: change sun moon rotation so that the initial position is at midday not midnight

            //night speed:
            Date yesterday       = clock.getCurrentDate().DateWithDays(-1);
            Time yesterdaySunset = clock.getSunsetTime(yesterday);

            if (debug)
            {
                Debug.Log("  CALCULATING INITIAL SPEED -------------------------");
                Debug.Log("SUN/MOON ROTATION" + this.name + ": yesterday: " + yesterday);
                Debug.Log("SUN/MOON ROTATION" + this.name + ": Yesterda's sunset: " + yesterdaySunset.ToString() + "\n" +
                          "today sunrise: " + startDaySunrise);
                Debug.Log("SUN/MOON ROTATION" + this.name + ": Time between them: " + yesterdaySunset.SecondsBetween(startDaySunrise));
            }
            int PhaseTotalTime = yesterdaySunset.SecondsBetween(startDaySunrise);

            rotationSpeed = 180.0f / PhaseTotalTime; //speed in degrees per second
            int   timePased;
            float rotationAngle;

            if (end.CompareTo(startDaySunrise) >= 0)
            {
                if (debug)
                {
                    Debug.Log("SUN/MOON ROTATION" + this.name + ": end time is past sunrise, therefore the total movement will be seccioned. \n" +
                              "the firs secction is from start: " + start.ToString() + "  to sunrise: " + startDaySunrise.ToString());
                    Debug.Break();
                }
                Time provisionalEnd = startDaySunrise.Clone();
                timePased     = start.SecondsBetween(provisionalEnd);
                rotationAngle = rotationSpeed * timePased;
                if (debug)
                {
                    Debug.Log("SUN/MOON ROTATION" + this.name + ": soconds passed from start to end: " + timePased + "\n"
                              + " the speed used is: " + rotationSpeed + "degrees per second");
                    Debug.Log("SUN/MOON ROTATION" + this.name + ": new rotation angle generated: " + rotationAngle);
                    Debug.Break();
                }
                transform.RotateAround(Vector3.zero, Vector3.right, rotationAngle);
                start = startDaySunrise.Clone();

                daytime = true;
                //dayTime speed calculations

                PhaseTotalTime = startDaySunrise.SecondsBetween(startDaySunset);
                rotationSpeed  = 180.0f / PhaseTotalTime;

                if (end.CompareTo(startDaySunset) >= 0)
                {
                    if (debug)
                    {
                        Debug.Log("SUN/MOON ROTATION" + this.name + ": end time is past sunset, therefore the remaining movement will be secctioned. \n" +
                                  "The firs section is from start: " + start.ToString() + "  to sunset: " + startDaySunset.ToString());
                        Debug.Break();
                    }
                    provisionalEnd = startDaySunset.Clone();
                    timePased      = start.SecondsBetween(provisionalEnd);
                    rotationAngle  = rotationSpeed * timePased;
                    if (debug)
                    {
                        Debug.Log("SUN/MOON ROTATION" + this.name + ": soconds passed since sunrise: " + timePased + "\n" +
                                  "new rotation angle generated: " + rotationAngle);
                        Debug.Break();
                    }
                    transform.RotateAround(Vector3.zero, Vector3.right, rotationAngle);
                    start = startDaySunset.Clone();

                    daytime = false;

                    PhaseTotalTime = startDaySunset.SecondsBetween(clock.getNextSunriseTime());
                    rotationSpeed  = 180.0f / PhaseTotalTime;
                    timePased      = start.SecondsBetween(end);
                    rotationAngle  = rotationSpeed * timePased;

                    if (debug)
                    {
                        Debug.Log("SUN/MOON ROTATION" + this.name + ": soconds passed since sunset: " + timePased + "\n" +
                                  "new rotation angle generated: " + rotationAngle);
                        Debug.Break();
                    }
                    transform.RotateAround(Vector3.zero, Vector3.right, rotationAngle);
                }
                else
                {
                    timePased     = start.SecondsBetween(end);
                    rotationAngle = rotationSpeed * timePased;
                    if (debug)
                    {
                        Debug.Log("SUN/MOON ROTATION" + this.name + ": soconds passed since sunrise: " + timePased + "\n" +
                                  "new rotation angle generated: " + rotationAngle);
                        Debug.Break();
                    }
                    transform.RotateAround(Vector3.zero, Vector3.right, rotationAngle);

                    daytime = true;
                }
            }
            else
            {
                timePased     = start.SecondsBetween(end);
                rotationAngle = rotationSpeed * timePased;
                if (debug)
                {
                    Debug.Log("SUN/MOON ROTATION" + this.name + ": soconds passed since midnight: " + timePased + "\n" +
                              "new rotation angle generated: " + rotationAngle);
                    Debug.Break();
                }
                transform.RotateAround(Vector3.zero, Vector3.right, rotationAngle);

                daytime = false;
            }

            previousTime = currentTime.Clone();
        }