PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/SMLimitless/SMLEngine/Sml/Sml/SmlProgram.cs

#
C# | 308 lines | 204 code | 39 blank | 65 comment | 18 complexity | 0cc65b827efff8cf758b3e2534572f7b MD5 | raw file
  1. #region File Description
  2. /* SmlProgram.cs
  3. * Sets up and runs SML
  4. * Version 1.1
  5. * */
  6. #endregion
  7. #region Using Statements
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.IO;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Audio;
  14. using Microsoft.Xna.Framework.Content;
  15. using Microsoft.Xna.Framework.GamerServices;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Input;
  18. using Microsoft.Xna.Framework.Media;
  19. using Sml.Screens;
  20. using SmlEngine.UI.Input;
  21. using SmlEngine.UI.Managers;
  22. using SmlEngine.UI.Screens;
  23. #endregion
  24. namespace Sml
  25. {
  26. /// <summary>
  27. /// This is the class that sets up and runs SML.
  28. /// </summary>
  29. public class SmlProgram : Game
  30. {
  31. #region Fields
  32. private const string Title = "SML v0.04 - Alpha";
  33. private GraphicsDeviceManager graphics;
  34. private SpriteBatch spriteBatch;
  35. private ScreenManager screenManager;
  36. private List<Screen> Screens; // used to store screens for now because I can't see how to make a screen inactive in ScreenManager yet
  37. private TitleScreen titleScreen;
  38. private MainMenuScreen mainMenuScreen;
  39. private GameScreen gameScreen;
  40. private WorldScreen worldScreen;
  41. private int worldIndex = -1; // used when entering levels to keep track of which world is being used
  42. private int worldSectionIndex = -1; // which section the world is above
  43. private List<int> worldExitNumber = new List<int>(); // which exits this level will trigger on clear
  44. #endregion
  45. #region Methods
  46. /// <summary>
  47. /// Constructs a new SmlProgram instance.
  48. /// </summary>
  49. public SmlProgram()
  50. {
  51. graphics = new GraphicsDeviceManager(this);
  52. Content.RootDirectory = "Content";
  53. Screens = new List<Screen>();
  54. graphics.DeviceReset += new EventHandler<EventArgs>(graphics_DeviceReset);
  55. }
  56. /// <summary>
  57. /// Loads a level file, world file, or project file.
  58. /// </summary>
  59. /// <param name="filePath">The path of the file to open.</param>
  60. /// <returns>Success state.</returns>
  61. private bool OpenFile(string filePath)
  62. {
  63. // First, determine what type of file this is.
  64. // Currently, this is inferred via file extension,
  65. // but this will change once file handling is more solidifed.
  66. if (File.Exists(filePath))
  67. {
  68. string extension = Path.GetExtension(filePath);
  69. if (extension == ".lvl")
  70. {
  71. TitleScreen t = new TitleScreen();
  72. screenManager.SetActiveGroup(screenManager.AddScreenGroup(new ScreenGroup(screenManager, t)));
  73. foreach (Sml.Scenes.LevelScene scene in t.Scenes)
  74. {
  75. scene.Level.LevelCleared += new SmlEngine.Sprites.Collections.LevelClearedEventHandler(Level_LevelCleared);
  76. }
  77. return true;
  78. }
  79. else if (extension == ".wld")
  80. {
  81. WorldScreen w = new WorldScreen(new List<string>() { filePath, Directory.GetCurrentDirectory() + @"\resources\world4.wld" }, new Vector2(Window.ClientBounds.Height, Window.ClientBounds.Width));
  82. screenManager.SetActiveGroup(screenManager.AddScreenGroup(new ScreenGroup(screenManager, w)));
  83. foreach (Sml.Scenes.WorldScene scene in w.Scenes)
  84. {
  85. foreach (SmlEngine.Sprites.Collections.World world in scene.Worlds)
  86. {
  87. world.LevelEntered += new SmlEngine.Sprites.Collections.LevelEnteredEventHandler(world_LevelEntered);
  88. }
  89. }
  90. return true;
  91. }
  92. // eventual project file handling code here
  93. else
  94. {
  95. // We really need an error handling method that does not involve exceptions.
  96. // ERROR
  97. throw new Exception(string.Format("The file at {0} is not a valid level, project, or world file.", filePath));
  98. }
  99. }
  100. else
  101. {
  102. // ERROR
  103. throw new Exception(string.Format("The file at {0} does not exist. Please check your paths.", filePath));
  104. }
  105. }
  106. /// <summary>
  107. /// Allows the game to perform any initialization it needs to before starting to run.
  108. /// This is where it can query for any required services and load any non-graphic
  109. /// related content. Calling base.Initialize will enumerate through any components
  110. /// and initialize them as well.
  111. /// </summary>
  112. protected override void Initialize()
  113. {
  114. // Add initialization logic here
  115. Window.Title = Title;
  116. IsMouseVisible = true;
  117. //Initialize components
  118. InputSettings.Defaults();
  119. screenManager = new ScreenManager(this, "default");
  120. screenManager.Initialize();
  121. //Initialize screens
  122. // titleScreen = new TitleScreen(); //TitleScreen
  123. // screenManager.AddScreen(titleScreen);
  124. mainMenuScreen = new MainMenuScreen(); //MainMenuScreen
  125. gameScreen = new GameScreen(); //GameScreen
  126. // worldScreen = new WorldScreen(new List<String>(){@"D:\Documents\Files\SML\world4.wld"}, new Vector2(Window.ClientBounds.Height, Window.ClientBounds.Width)); //WorldScreen
  127. //screenManager.AddScreen(worldScreen);
  128. if (OpenFile(Directory.GetCurrentDirectory() + @"\resources\world3.wld") == null)
  129. {
  130. // null is the current fail condition because exceptions stop a method
  131. TitleScreen t = new TitleScreen(); // just for now we'll do this
  132. screenManager.SetActiveGroup(screenManager.AddScreenGroup(new ScreenGroup(screenManager, t)));
  133. Screens.Add(t);
  134. }
  135. base.Initialize();
  136. }
  137. /// <summary>
  138. /// LoadContent will be called once per game and is the place to load
  139. /// all of your content.
  140. /// </summary>
  141. protected override void LoadContent()
  142. {
  143. // Create a new SpriteBatch, which can be used to draw textures.
  144. spriteBatch = new SpriteBatch(GraphicsDevice);
  145. // Use this.Content to load game content here.
  146. mainMenuScreen.LoadContent(Content);
  147. gameScreen.LoadContent(Content);
  148. foreach (Screen s in screenManager.ScreenGroups[screenManager.activeGroupIndex].Screens)
  149. {
  150. //s.LoadContent(Content);
  151. }
  152. }
  153. /// <summary>
  154. /// UnloadContent will be called once per game and is the place to unload
  155. /// all content.
  156. /// </summary>
  157. protected override void UnloadContent()
  158. {
  159. // Unload non-ContentManager content here
  160. }
  161. void graphics_DeviceReset(object sender, EventArgs e)
  162. {
  163. foreach (Screen s in screenManager.ScreenGroups[screenManager.activeGroupIndex].Screens)
  164. {
  165. if (s is WorldScreen)
  166. {
  167. (s as WorldScreen).Scenes[0].ReloadContent();
  168. }
  169. }
  170. }
  171. void world_LevelEntered(object sender, SmlEngine.Sprites.Base.WorldMap.LevelEnteredEventArgs e)
  172. {
  173. if (!File.Exists(e.filePath))
  174. {
  175. System.Windows.Forms.MessageBox.Show(string.Format("The following level file could not be found:{0}{1}", Environment.NewLine, e.filePath), "File Not Found", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
  176. return;
  177. }
  178. try
  179. {
  180. foreach (Screen s in screenManager.ScreenGroups[screenManager.activeGroupIndex].Screens)
  181. {
  182. if (s is WorldScreen)
  183. {
  184. foreach (Sml.Scenes.WorldScene scene in s.Scenes)
  185. {
  186. if (scene.Worlds.Contains(sender))
  187. {
  188. worldIndex = screenManager.GroupIndexFromScreen(s);
  189. worldSectionIndex = scene.Worlds.IndexOf((SmlEngine.Sprites.Collections.World)sender);
  190. continue;
  191. }
  192. }
  193. }
  194. }
  195. worldExitNumber = e.exitNumber;
  196. OpenFile(e.filePath);
  197. }
  198. catch (Exception ex)
  199. {
  200. System.Windows.Forms.MessageBox.Show(ex.Message);
  201. }
  202. }
  203. void Level_LevelCleared(object sender, SmlEngine.Sprites.Collections.LevelClearedEventArgs e)
  204. {
  205. if (screenManager.resourceState == SmlEngine.ResourceState.WorldStandalone)
  206. {
  207. screenManager.SetActiveGroup(worldIndex);
  208. (screenManager.ScreenGroups[worldIndex].Screens[0] as WorldScreen).GetScene(0).SwapWorld(worldSectionIndex);
  209. if (worldExitNumber.Any())
  210. {
  211. (screenManager.ScreenGroups[worldIndex].Screens[0] as WorldScreen).GetScene(0).Worlds[worldSectionIndex].StartReveal(worldExitNumber[0]);
  212. }
  213. else
  214. {
  215. (screenManager.ScreenGroups[worldIndex].Screens[0] as WorldScreen).GetScene(0).Worlds[worldSectionIndex].UnlockTile();
  216. }
  217. }
  218. else
  219. {
  220. // Default case for no world loaded (i.e. standalone level)
  221. screenManager.RemoveAllScreenGroups();
  222. TitleScreen t = new TitleScreen();
  223. screenManager.SetActiveGroup(screenManager.AddScreenGroup(new ScreenGroup(screenManager, t)));
  224. }
  225. }
  226. /// <summary>
  227. /// Allows the game to run logic such as updating the world,
  228. /// checking for collisions, gathering input, and playing audio.
  229. /// </summary>
  230. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  231. protected override void Update(GameTime gameTime)
  232. {
  233. // Allows the game to exit
  234. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  235. this.Exit();
  236. // Add update logic here
  237. base.Update(gameTime);
  238. }
  239. /// <summary>
  240. /// This is called when the game should draw itself.
  241. /// </summary>
  242. /// <param name="gameTime">Provides a snapshot of timing values.</param>
  243. protected override void Draw(GameTime gameTime)
  244. {
  245. GraphicsDevice.Clear(Color.Black);
  246. // Add drawing code here
  247. base.Draw(gameTime);
  248. //worldScreen.Draw(gameTime);
  249. }
  250. #endregion
  251. }
  252. #region Entry Point
  253. #if WINDOWS || XBOX
  254. static class Program
  255. {
  256. /// <summary>
  257. /// The main entry point for the application.
  258. /// </summary>
  259. static void Main(string[] args)
  260. {
  261. using (SmlProgram game = new SmlProgram())
  262. {
  263. game.Run();
  264. }
  265. }
  266. }
  267. #endif
  268. #endregion
  269. }