Пример #1
0
        public static void ExecuteCoroutineThread(CoroutineHandle handle)
        {
            try
            {
                while (!handle.AbortRequested)
                {
                    if (handle.Coroutine.Current != null)
                    {
                        WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds;
                        if (wfs != null)
                        {
                            Thread.Sleep((int)(wfs.TotalTime * 1000));
                        }
                        else
                        {
                            switch ((CoroutineStatus)handle.Coroutine.Current)
                            {
                            case CoroutineStatus.Success:
                                return;

                            case CoroutineStatus.Failure:
                                DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has failed");
                                return;
                            }
                        }
                    }

                    Thread.Yield();
                    if (!handle.Coroutine.MoveNext())
                    {
                        return;
                    }
                }
            }
            catch (ThreadAbortException)
            {
                //not an error, don't worry about it
            }
            catch (Exception e)
            {
                handle.Exception = e;
                DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has thrown an exception", e);
            }
        }
Пример #2
0
        public CameraTransition(ISpatialEntity targetEntity, Camera cam, Alignment?cameraStartPos, Alignment?cameraEndPos, bool fadeOut = true, float duration = 10.0f, float?startZoom = null, float?endZoom = null)
        {
            Duration            = duration;
            FadeOut             = fadeOut;
            this.cameraStartPos = cameraStartPos;
            this.cameraEndPos   = cameraEndPos;
            this.startZoom      = startZoom;
            this.endZoom        = endZoom;
            AssignedCamera      = cam;

            if (targetEntity == null)
            {
                return;
            }

            Running = true;
            CoroutineManager.StopCoroutines("CameraTransition");
            updateCoroutine = CoroutineManager.StartCoroutine(Update(targetEntity, cam), "CameraTransition");
        }
Пример #3
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            GraphicsWidth  = GraphicsDevice.Viewport.Width;
            GraphicsHeight = GraphicsDevice.Viewport.Height;

            ConvertUnits.SetDisplayUnitToSimUnitRatio(Physics.DisplayToSimRation);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            TextureLoader.Init(GraphicsDevice);

            loadingScreenOpen = true;
            TitleScreen       = new LoadingScreen(GraphicsDevice);

            loadingCoroutine = CoroutineManager.StartCoroutine(Load());

            var myForm = (System.Windows.Forms.Form)System.Windows.Forms.Form.FromHandle(Window.Handle);

            myForm.Deactivate += new EventHandler(HandleDefocus);
            myForm.Activated  += new EventHandler(HandleFocus);
        }
Пример #4
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            GraphicsWidth  = GraphicsDevice.Viewport.Width;
            GraphicsHeight = GraphicsDevice.Viewport.Height;

            ConvertUnits.SetDisplayUnitToSimUnitRatio(Physics.DisplayToSimRation);

            spriteBatch = new SpriteBatch(GraphicsDevice);
            TextureLoader.Init(GraphicsDevice);

            loadingScreenOpen = true;
            TitleScreen       = new LoadingScreen(GraphicsDevice);

            bool canLoadInSeparateThread = false;

#if WINDOWS
            canLoadInSeparateThread = true;
#endif

            loadingCoroutine = CoroutineManager.StartCoroutine(Load(), "", canLoadInSeparateThread);
        }
Пример #5
0
        public static CoroutineHandle StartCoroutine(IEnumerable <object> func, string name = "", bool useSeparateThread = false)
        {
            var handle = new CoroutineHandle(func.GetEnumerator(), name);

            lock (Coroutines)
            {
                Coroutines.Add(handle);
            }

            handle.Thread = null;
            if (useSeparateThread)
            {
                handle.Thread = new Thread(() => { ExecuteCoroutineThread(handle); })
                {
                    Name         = "Coroutine Thread (" + handle.Name + ")",
                    IsBackground = true
                };
                handle.Thread.Start();
            }

            return(handle);
        }
Пример #6
0
        private static bool IsDone(CoroutineHandle handle)
        {
            try
            {
                if (handle.Coroutine.Current != null)
                {
                    WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds;
                    if (wfs != null)
                    {
                        if (!wfs.CheckFinished(UnscaledDeltaTime))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        switch ((CoroutineStatus)handle.Coroutine.Current)
                        {
                        case CoroutineStatus.Success:
                            return(true);

                        case CoroutineStatus.Failure:
                            DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has failed");
                            return(true);
                        }
                    }
                }

                handle.Coroutine.MoveNext();
                return(false);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Coroutine " + handle.Name + " threw an exception: " + e.Message + "\n" + e.StackTrace.ToString());
                return(true);
            }
        }
Пример #7
0
 public static void StopCoroutines(CoroutineHandle handle)
 {
     Coroutines.RemoveAll(c => c == handle);
 }
Пример #8
0
 public static bool IsCoroutineRunning(CoroutineHandle handle)
 {
     return(Coroutines.Contains(handle));
 }
Пример #9
0
        private static bool IsDone(CoroutineHandle handle)
        {
#if !DEBUG
            try
            {
#endif
            if (handle.Thread == null)
            {
                if (handle.AbortRequested)
                {
                    return(true);
                }
                if (handle.Coroutine.Current != null)
                {
                    WaitForSeconds wfs = handle.Coroutine.Current as WaitForSeconds;
                    if (wfs != null)
                    {
                        if (!wfs.CheckFinished(UnscaledDeltaTime))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        switch ((CoroutineStatus)handle.Coroutine.Current)
                        {
                        case CoroutineStatus.Success:
                            return(true);

                        case CoroutineStatus.Failure:
                            DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has failed");
                            return(true);
                        }
                    }
                }

                handle.Coroutine.MoveNext();
                return(false);
            }
            else
            {
                if (handle.Thread.ThreadState.HasFlag(ThreadState.Stopped))
                {
                    if (handle.Exception != null || (CoroutineStatus)handle.Coroutine.Current == CoroutineStatus.Failure)
                    {
                        DebugConsole.ThrowError("Coroutine \"" + handle.Name + "\" has failed");
                    }
                    return(true);
                }
                return(false);
            }
#if !DEBUG
        }

        catch (Exception e)
        {
#if CLIENT && WINDOWS
            if (e is SharpDX.SharpDXException)
            {
                throw;
            }
#endif
            DebugConsole.ThrowError("Coroutine " + handle.Name + " threw an exception: " + e.Message + "\n" + e.StackTrace.ToString());
            handle.Exception = e;
            return(true);
        }
#endif
        }