00001
00002
00003
00004
00005
00006 namespace PendulumGame
00007 {
00008 using System;
00009 using System.Threading;
00010 using System.Collections.Generic;
00011 using System.Text;
00012 using Microsoft.Xna.Framework;
00013 using Microsoft.Xna.Framework.Graphics;
00014 using NewGamePhysics.Networking;
00015 using NewGamePhysics.StateManager;
00016
00020 public class PendulumGame : Microsoft.Xna.Framework.Game
00021 {
00025 public const string PlayTrulyRandomAgentName = "PendulumGame_v3";
00026
00030 private static PendulumGameState state;
00031
00035 private InfoLinkTransmitter infoLinkTransmitter;
00036
00040 private PlayTrulyRandom playTrulyRandomWebservice;
00041
00045 private Dictionary<string, InfoLink[]> infoLinkCollections;
00046
00050 private ScreenManager screenManager;
00051
00055 public PendulumGame()
00056 {
00057
00058 bool fullscreen = false;
00059 bool oneGamepad = false;
00060 bool disableShaders = false;
00061 bool xboxResolution = false;
00062 bool audioOnDemand = false;
00063 string[] args = System.Environment.GetCommandLineArgs();
00064 if (args.Length > 1)
00065 {
00066 for (int i = 1; i < args.Length; i++)
00067 {
00068 if (!string.IsNullOrEmpty(args[i]))
00069 {
00070 if (args[i].Equals("/fullscreen", StringComparison.InvariantCultureIgnoreCase))
00071 {
00072 fullscreen = true;
00073 }
00074 else if (args[i].Equals("/onegamepad", StringComparison.InvariantCultureIgnoreCase))
00075 {
00076 oneGamepad = true;
00077 }
00078 else if (args[i].Equals("/twogamepads", StringComparison.InvariantCultureIgnoreCase))
00079 {
00080 oneGamepad = false;
00081 }
00082 else if (args[i].Equals("/noshaders", StringComparison.InvariantCultureIgnoreCase))
00083 {
00084 disableShaders = true;
00085 }
00086 else if (args[i].Equals("/xboxscreen", StringComparison.InvariantCultureIgnoreCase))
00087 {
00088 xboxResolution = true;
00089 }
00090 else if (args[i].Equals("/audioondemand", StringComparison.InvariantCultureIgnoreCase))
00091 {
00092 audioOnDemand = true;
00093 }
00094 else
00095 {
00096 System.Console.WriteLine("PendulumGame [/fullscreen] [/xboxscreen] [/noshaders] [/onegamepad|/twogamepads] [/audioondemand]");
00097 }
00098 }
00099 }
00100 }
00101
00102
00103 state = new PendulumGameState();
00104
00105
00106 Content.RootDirectory = "Content";
00107
00108
00109 GraphicsDeviceManager graphics = new GraphicsDeviceManager(this);
00110
00111
00112 if (xboxResolution)
00113 {
00114
00115 graphics.PreferredBackBufferWidth = 853;
00116 graphics.PreferredBackBufferHeight = 480;
00117 PendulumGame.State.Scale = new Vector2(1.0f, 1.0f);
00118 }
00119 else
00120 {
00121
00122 graphics.PreferredBackBufferWidth = 1024;
00123 graphics.PreferredBackBufferHeight = 768;
00124 PendulumGame.State.Scale = new Vector2(
00125 (float)1024 / (float)853,
00126 (float)768 / (float)480);
00127 }
00128
00129
00130 graphics.IsFullScreen = fullscreen;
00131
00132
00133 graphics.PreferMultiSampling = true;
00134
00135
00136 PendulumGame.State.RealtimeAudioOnDemand = audioOnDemand;
00137
00138
00139 PendulumGame.State.UseOnlyOneGamepad = oneGamepad;
00140
00141
00142 PendulumGame.State.DisablePixelShaderEffects = disableShaders;
00143
00144
00145 this.IsMouseVisible = true;
00146
00147
00148 screenManager = new ScreenManager(this);
00149 Components.Add(screenManager);
00150 }
00151
00155 public static PendulumGameState State
00156 {
00157 get { return PendulumGame.state; }
00158 }
00159
00163 protected override void Initialize()
00164 {
00165 base.Initialize();
00166 }
00167
00171 protected override void LoadContent()
00172 {
00173
00174 screenManager.Textures.Add("rivet", Content.Load<Texture2D>("Sprites/rivet"));
00175 screenManager.Textures.Add("metal", Content.Load<Texture2D>("Sprites/metal_texture"));
00176
00177 screenManager.Textures.Add("apple", Content.Load<Texture2D>("Sprites/red_apple"));
00178 screenManager.Textures.Add("copperball", Content.Load<Texture2D>("Sprites/copperball"));
00179 screenManager.Textures.Add("silverball", Content.Load<Texture2D>("Sprites/silverball"));
00180 screenManager.Textures.Add("goldball", Content.Load<Texture2D>("Sprites/goldball"));
00181 screenManager.Textures.Add("duck", Content.Load<Texture2D>("Sprites/yellow_duck"));
00182
00183
00184 screenManager.Fonts.Add("retro", Content.Load<SpriteFont>("Fonts/retroMedium"));
00185 screenManager.Fonts.Add("courier", Content.Load<SpriteFont>("Fonts/courier"));
00186
00187
00188 string fileContent;
00189 this.infoLinkCollections = new Dictionary<string, InfoLink[]>();
00190 string[] infoLinkTopics = { "Gravity", "DoublePendulum", "Randomness" };
00191 foreach (string topic in infoLinkTopics)
00192 {
00193 fileContent =
00194 System.IO.File.ReadAllText(@"InfoLinkData\" + topic + ".xml", Encoding.Unicode);
00195 if (!string.IsNullOrEmpty(fileContent))
00196 {
00197 InfoLink[] infoLinks =
00198 InfoLinkSerializer.DeserializeArray(fileContent);
00199 if (infoLinks != null)
00200 {
00201 this.infoLinkCollections.Add(topic, infoLinks);
00202 }
00203 }
00204 }
00205
00206
00207 this.playTrulyRandomWebservice = new PlayTrulyRandom(
00208 PlayTrulyRandomAgentName);
00209 this.playTrulyRandomWebservice.RegisterAgent();
00210
00211
00212 infoLinkTransmitter = new InfoLinkTransmitter();
00213 infoLinkTransmitter.StartTransmitter();
00214
00215
00216 screenManager.AddScreen(new PendulumBackgroundScreen(), null);
00217 screenManager.AddScreen(new PendulumMainMenuScreen(), null);
00218 }
00219
00223 protected override void UnloadContent()
00224 {
00225
00226 infoLinkTransmitter.StopTransmitter();
00227
00228 base.UnloadContent();
00229 }
00230
00235 protected override void Update(GameTime gameTime)
00236 {
00237 base.Update(gameTime);
00238 }
00239
00244 protected override void Draw(GameTime gameTime)
00245 {
00246
00247 this.GraphicsDevice.Clear(Color.Black);
00248
00249
00250 base.Draw(gameTime);
00251 }
00252
00257 internal void SendRandomInfoLink(string topic)
00258 {
00259 if (this.infoLinkCollections.ContainsKey(topic))
00260 {
00261 InfoLink[] infoLinks = this.infoLinkCollections[topic];
00262 if ((infoLinks != null) && (infoLinks.Length > 0))
00263 {
00264 int randomPick =
00265 PendulumGame.state.PhysicalRandomNumberGenerator.Next(
00266 0,
00267 infoLinks.Length - 1);
00268 this.infoLinkTransmitter.Transmit(infoLinks[randomPick]);
00269 }
00270 }
00271 }
00272
00278 internal void SubmitEntropyBits(string entropy)
00279 {
00280 Thread thread;
00281 thread = new System.Threading.Thread(
00282 this.playTrulyRandomWebservice.OnlineSubmit);
00283 thread.Start(entropy.Clone());
00284 }
00285 }
00286 }
00287