Jypeli  9
The simple game programming library
DelayedActions.cs
Siirry tämän tiedoston dokumentaatioon.
1 #region MIT License
2 /*
3  * Copyright (c) 2013 University of Jyväskylä, Department of Mathematical
4  * Information Technology.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #endregion
25 
26 /*
27  * Authors: Tero Jäntti, Tomi Karppinen, Janne Nikkanen.
28  */
29 
30 using System;
31 using System.Collections.Generic;
32 
33 namespace Jypeli
34 {
35  public partial class Game
36  {
37  Queue<Action> PendingActions = new Queue<Action>();
38 
42  public static event Action InstanceInitialized;
43 
47  public static new event Action Exiting;
48 
53  public static void AssertInitialized( Action actionMethod )
54  {
55  if ( Instance != null )
56  actionMethod();
57  else
58  InstanceInitialized += actionMethod;
59  }
60 
65  public static void DoNextUpdate( Action action )
66  {
67  if ( Instance != null )
68  Instance.PendingActions.Enqueue( action );
69  else
70  InstanceInitialized += action;
71  }
72 
79  public static void DoNextUpdate<T1>( Action<T1> action, T1 p1 )
80  {
81  DoNextUpdate( delegate { action( p1 ); } );
82  }
83 
92  public static void DoNextUpdate<T1, T2>( Action<T1, T2> action, T1 p1, T2 p2 )
93  {
94  DoNextUpdate( delegate { action( p1, p2 ); } );
95  }
96 
103  public static void AssertInitialized<T1>( Action<T1> actionMethod, T1 o1 )
104  {
105  if ( Instance != null )
106  actionMethod( o1 );
107  else
108  InstanceInitialized += delegate { actionMethod( o1 ); };
109  }
110 
114  public void ConfirmExit()
115  {
116 #if !WINRT // Win8 programs cannot be closed with Exit()
117 
118  YesNoWindow kyselyIkkuna = new YesNoWindow( "Do you want to quit?" );
119  kyselyIkkuna.Yes += Exit;
120  kyselyIkkuna.Closed += delegate { IsPaused = false; };
121  Add( kyselyIkkuna );
122 
123  IsPaused = true;
124 #endif
125  }
126 
127 #if WINRT
128  public new void Exit()
129  {
130  ClearAll();
131 
132  Label exitLabel = new Label("Game over. You can close the app now.");
133  Add( exitLabel );
134  }
135 #endif
136 
137  protected override void OnExiting( object sender, EventArgs args )
138  {
139  if ( Exiting != null )
140  Exiting();
141 
142  base.OnExiting( sender, args );
143  }
144 
145  private void ExecutePendingActions()
146  {
147  while ( PendingActions.Count > 0 )
148  PendingActions.Dequeue()();
149  }
150  }
151 }
Jypeli.YesNoWindow.Yes
Action Yes
Tapahtuu kun käyttäjä valitsee "kyllä"-vaihtoehdon.
Definition: YesNoWindow.cs:42
Jypeli.Game.DoNextUpdate< T1, T2 >
static void DoNextUpdate< T1, T2 >(Action< T1, T2 > action, T1 p1, T2 p2)
Suorittaa aliohjelman seuraavalla päivityksellä.
Definition: DelayedActions.cs:92
Jypeli.Game.IsPaused
bool IsPaused
Onko peli pysähdyksissä.
Definition: Time.cs:17
Jypeli.Game.ConfirmExit
void ConfirmExit()
Kysyy haluaako lopettaa pelin ja lopettaa jos vastataan kyllä.
Definition: DelayedActions.cs:114
Jypeli
Definition: Automobile.cs:5
Jypeli.Label
Tekstikenttä.
Definition: Label.cs:66
Jypeli.Game.ClearAll
virtual void ClearAll()
Nollaa kaiken.
Definition: Game.cs:389
Jypeli.HoverState.Off
@ Off
Ei olion päällä.
Jypeli.Window.Closed
WindowHandler Closed
Tapahtuu kun ikkuna suljetaan. TODO: ClearAllin kutsuminen samalla updatella kuin Closed-eventti tapa...
Definition: Window.cs:92
Jypeli.Game.AssertInitialized
static void AssertInitialized(Action actionMethod)
Suorittaa aliohjelman kun peli on varmasti alustettu.
Definition: DelayedActions.cs:53
Jypeli.Game.OnExiting
override void OnExiting(object sender, EventArgs args)
Definition: DelayedActions.cs:137
Jypeli.Game.ExecutePendingActions
void ExecutePendingActions()
Definition: DelayedActions.cs:145
Jypeli.Game.DoNextUpdate
static void DoNextUpdate(Action action)
Suorittaa aliohjelman seuraavalla päivityksellä.
Definition: DelayedActions.cs:65
Jypeli.Game.Instance
static Game Instance
Käynnissä olevan pelin pääolio.
Definition: Game.cs:90
Jypeli.Game.AssertInitialized< T1 >
static void AssertInitialized< T1 >(Action< T1 > actionMethod, T1 o1)
Suorittaa aliohjelman kun peli on varmasti alustettu.
Definition: DelayedActions.cs:103
Jypeli.Game.Add
void Add(Light light)
Lisää valon peliin. Nykyisellään valoja voi olla ainoastaan yksi kappale. Toistaiseksi ei tuettu Wind...
Definition: Effects.cs:27
Jypeli.Game.Exiting
static new Action Exiting
Tapahtuu kun peli lopetetaan.
Definition: DelayedActions.cs:47
Jypeli.Game.InstanceInitialized
static Action InstanceInitialized
Tapahtuu kun Game.Instance on alustettu.
Definition: DelayedActions.cs:42
Jypeli.YesNoWindow
Ikkuna, joka kysyy käyttäjältä kyllä tai ei -kysymyksen.
Definition: YesNoWindow.cs:38
Jypeli.Game.PendingActions
Queue< Action > PendingActions
Definition: DelayedActions.cs:37
System
Definition: CFFauxAttributes.cs:29
Jypeli.Game.DoNextUpdate< T1 >
static void DoNextUpdate< T1 >(Action< T1 > action, T1 p1)
Suorittaa aliohjelman seuraavalla päivityksellä.
Definition: DelayedActions.cs:79