root/Code/oMaE/oMaE/GameDriver.cs @ 107

Revision 107, 12.2 KB (checked in by acarter, 2 years ago)

Moved map into xml file

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using Microsoft.Xna.Framework;
5using Microsoft.Xna.Framework.Audio;
6using Microsoft.Xna.Framework.Content;
7using Microsoft.Xna.Framework.GamerServices;
8using Microsoft.Xna.Framework.Graphics;
9using Microsoft.Xna.Framework.Input;
10using Microsoft.Xna.Framework.Media;
11
12namespace oMaE
13{
14    using MiniGames;
15    using Widgets;
16    using Screens;
17    using Overworld;
18    using Sequence;
19    /// <summary>
20    /// This is the main driver for the game
21    /// </summary>
22    public class GameDriver : Game
23    {
24        /// <summary>
25        ///  The device manager for the 3D graphics.
26        /// </summary>
27        private GraphicsDeviceManager graphics;
28        /// <summary>
29        ///  The sprite manager for the 2D graphics.
30        /// </summary>
31        private SpriteBatch spriteBatch;
32        /// <summary>
33        /// Holds the current gamestate with respect to User Input.
34        /// </summary>
35        private GameState gamestate = new GameState();
36        /// <summary>
37        /// The map of the world.
38        /// </summary>
39        public Map Map = new Map(0);
40        /// <summary>
41        /// The display for the 3D game.
42        /// </summary>
43        public Display3D World3D = new Display3D();
44        /// <summary>
45        /// All of the 2D world components.
46        /// </summary>
47        public GameObject World2D = new GameObject();
48        /// <summary>
49        /// The Minigame factory for making minigames.
50        /// </summary>
51        public MinigameFactory Minigames = new MinigameFactory();
52        /// <summary>
53        /// The screen factory for the screens of the game.
54        /// </summary>
55        public ScreenFactory Screens = new ScreenFactory();
56        /// <summary>
57        /// Projection matrix for the game.
58        /// </summary>
59        public static Matrix ProjectionMatrix;
60        /// <summary>
61        /// The display for the game.
62        /// </summary>
63        private Screen2D display = new Screen2D(1000, 500);
64        /// <summary>
65        /// The main screen when the game is loaded.
66        /// </summary>
67        private MainScreen mainScreen = new MainScreen();
68        /// <summary>
69        /// The menu for the game.
70        /// </summary>
71        private GameObject gameMenu = new GameMenu();
72        /// <summary>
73        /// The current screen that is displayed.
74        /// </summary>
75        private GameObject currentScreen = null;
76        /// <summary>
77        /// The current screen that is displayed.
78        /// </summary>
79        private GameObject currentMinigame = null;
80        private GameObject openingSequence = new Opening();
81        /// <summary>
82        /// Creates a new default game driver.
83        /// </summary>
84        public GameDriver()
85        {
86            graphics = new GraphicsDeviceManager(this);
87            graphics.PreferredBackBufferWidth = 1000;
88            graphics.PreferredBackBufferHeight = 500;
89            Content.RootDirectory = "Content";
90        }
91
92        /// <summary>
93        /// Allows the game to perform any initialization it needs to before starting to run.
94        /// This is where it can query for any required services and load any non-graphic
95        /// related content.  Calling base.Initialize will enumerate through any components
96        /// and initialize them as well.
97        /// </summary>
98        protected override void Initialize()
99        {
100            // TODO: Add your initialization logic here
101            ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), GraphicsDevice.DisplayMode.AspectRatio, 1.0f, 10000.0f);
102            World2D.Add(Minigames);
103            World2D.Add(Screens);
104            World2D.Add(gameMenu);
105            World2D.Add(Map);
106            World2D.Add(Play2DGame(mainScreen, new Vector2(0,0), Map.mStart));
107            World2D.Add(openingSequence);
108
109            this.IsMouseVisible = true;
110            World2D.InitializeAll(this);
111            base.Initialize();
112        }
113
114        /// <summary>
115        /// LoadContent will be called once per game and is the place to load
116        /// all of your content.
117        /// </summary>
118        protected override void LoadContent()
119        {
120            // Create a new SpriteBatch, which can be used to draw textures.
121            spriteBatch = new SpriteBatch(GraphicsDevice);
122            display.LoadContent(spriteBatch, Content);
123            World2D.LoadAllContent(Content);
124            World3D.InitializeAll(this);
125            World3D.LoadAllContent(Content);
126            if (MainScreen.speech != null)
127            {
128                MainScreen.speech.IsLooped = true;
129                MainScreen.speech.Play();
130            }
131        }
132
133        /// <summary>
134        /// UnloadContent will be called once per game and is the place to unload
135        /// all content.
136        /// </summary>
137        protected override void UnloadContent()
138        {
139            // TODO: Unload any non ContentManager content here
140        }
141
142        /// <summary>
143        /// Allows the game to run logic such as updating the world,
144        /// checking for collisions, gathering input, and playing audio.
145        /// </summary>
146        /// <param name="gameTime">Provides a snapshot of timing values.</param>
147        protected override void Update(GameTime gameTime)
148        {
149            // Allows the game to exit
150            gamestate.Update(gameTime, Keyboard.GetState(), Mouse.GetState());
151            // TODO move this into Display3D
152            if (gamestate.CurrentKeyboardState.IsKeyDown(Keys.Escape))
153                this.Exit();
154            // updates world
155            World2D.UpdateAll(gamestate);
156            World3D.UpdateAll(gamestate);
157            base.Update(gameTime);
158        }
159
160        /// <summary>
161        /// This is called when the game should draw itself.
162        /// </summary>
163        /// <param name="gameTime">Provides a snapshot of timing values.</param>
164        protected override void Draw(GameTime gameTime)
165        {
166            GraphicsDevice.Clear(Color.MediumPurple);
167            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
168            World3D.DrawAll(display, gamestate);
169            World2D.DrawAll(display, gamestate);
170            base.Draw(gameTime);
171        }
172
173        /// <summary>
174        /// A wrapper function for Minigame Minigames's getMinigame.
175        /// </summary>
176        /// <returns>Returns a new minigame.</returns>
177        public MiniGame GetMinigame()
178        {
179            return Minigames.getMiniGame();
180        }
181
182        /// <summary>
183        /// Starts the game.
184        /// </summary>
185        /// <param name="mainMenu">The main menu.</param>
186        /// <returns>The input game object.</returns>
187        public GameObject PlayGame(GameObject mainMenu)
188        {
189            mainMenu.Visible = false;
190            if (MainScreen.speech != null)
191                MainScreen.speech.Stop();
192            World3D.reset();
193            openingSequence.Visible = true;
194            // EndSequence(); // Remove this when playing opening sequence
195            return mainMenu;
196        }
197        /// <summary>
198        /// Ends the sequence.
199        /// </summary>
200        public void EndSequence()
201        {
202            World3D.Visible = true;
203            Play2DGame(Map.Entrance.DoorLock, new Vector2(0, 0), Map.mStart);
204            // FIXME
205            Map.Entrance.DoorLock.Back.Visible = false;
206        }
207
208        private Point mLoc;
209        private Vector2 otherSide = Vector2.Zero;
210        /// <summary>
211        /// Starts a minigame.
212        /// </summary>
213        /// <param name="game">The minigame that was started.</param>
214        /// <param name="otherSide">The location to move to if successful.</param>
215        /// <param name="mLoc">The location of the door on the minimap.</param>
216        /// <returns>The input game object.</returns>
217        public GameObject Play2DGame(GameObject game, Vector2 otherSide, Point mLoc)
218        {
219            game.Visible = true;
220            this.otherSide = otherSide;
221            this.mLoc = mLoc;
222            World3D.Visible = false;
223            try
224            {
225                MiniGame temp = (MiniGame)game;
226                if (temp.Completed)
227                    CompleteMinigame(game);
228            }
229            catch (Exception)
230            {
231            }
232            return game;
233        }
234
235        /// <summary>
236        /// Displays the game menu.
237        /// </summary>
238        public void DisplayGameMenu()
239        {
240            World3D.Updating = false;
241            gameMenu.Visible = true;
242        }
243        /// <summary>
244        /// Displays the map.
245        /// </summary>
246        public void DisplayMap()
247        {
248            World3D.Updating = false;
249            Map.Visible = true;
250        }
251        /// <summary>
252        /// Closes the map.
253        /// </summary>
254        public void CloseMap()
255        {
256            World3D.Visible = true;
257            Map.Visible = false;
258        }
259        /// <summary>
260        /// Closes the game menu.
261        /// </summary>
262        public void CloseGameMenu()
263        {
264            World3D.Visible = true;
265            gameMenu.Visible = false;
266        }
267        /// <summary>
268        /// Displays the main menu.
269        /// </summary>
270        public void DisplayMainMenu()
271        {
272            World3D.reset();
273            World3D.Visible = false;
274            if (currentMinigame != null)
275                currentMinigame.Visible = false;
276            if (currentScreen != null)
277                currentScreen.Visible = false;
278            gameMenu.Visible = false;
279
280            if(MainScreen.speech != null)
281                MainScreen.speech.Play();
282            mainScreen.Visible = true;
283        }
284        /// <summary>
285        /// Grab a new item.
286        /// </summary>
287        public void GetNewItem()
288        {
289            if (World3D.UI.MapVisible)
290            {
291                Screens.Inventory.FindPart();
292                DisplayScreen(Screens.Inventory);
293
294            }
295            else
296            {
297                World3D.UI.MapVisible = true;
298                DisplayMap();
299            }
300        }
301        /// <summary>
302        /// Displays a screen such as the inventory, or encyclophant.
303        /// </summary>
304        /// <param name="screen">The screen to be displayed.</param>
305        /// <returns>The screen that is now displayed.</returns>
306        public GameObject DisplayScreen(GameObject screen)
307        {
308            if (currentScreen != null)
309                CloseScreen();
310            currentScreen = screen;
311            screen.Visible = true;
312            World3D.Visible = false;
313            return screen;
314        }
315
316        /// <summary>
317        /// Ends a minigame without solving it.
318        /// </summary>
319        /// <param name="game">The minigame that was ended.</param>
320        /// <returns>The input game object.</returns>
321        public GameObject QuitMinigame(GameObject game)
322        {
323            game.Visible = false;
324            World3D.Visible = true;
325            return game;
326        }
327
328        /// <summary>
329        /// Succesfully completed a minigame.
330        /// </summary>
331        /// <param name="game">The minigame that was completed.</param>
332        /// <returns>The input game object.</returns>
333        public GameObject CompleteMinigame(GameObject game)
334        {
335            // FIXME
336            Map.Entrance.DoorLock.Back.Visible = true;
337            game.Visible = false;
338            World3D.Visible = true;
339            World3D.SetLocation(otherSide);
340            Map.EnterRoom(this.mLoc);
341            return game;
342        }
343        /// <summary>
344        /// Closes the current screen.
345        /// </summary>
346        ///
347        /// <returns>The screen that was closed.</returns>
348        public GameObject CloseScreen()
349        {
350            GameObject screen = currentScreen;
351            currentScreen = null;
352            if (screen != null)
353            {
354                screen.Visible = false;
355                World3D.Visible = true;
356            }
357            return screen;
358        }
359    }
360}
Note: See TracBrowser for help on using the browser.