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

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

Moved map into xml file

RevLine 
[3]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{
[4]14    using MiniGames;
[7]15    using Widgets;
[15]16    using Screens;
[18]17    using Overworld;
[51]18    using Sequence;
[3]19    /// <summary>
[37]20    /// This is the main driver for the game
[3]21    /// </summary>
[40]22    public class GameDriver : Game
[3]23    {
[37]24        /// <summary>
25        ///  The device manager for the 3D graphics.
26        /// </summary>
[41]27        private GraphicsDeviceManager graphics;
[37]28        /// <summary>
29        ///  The sprite manager for the 2D graphics.
30        /// </summary>
[41]31        private SpriteBatch spriteBatch;
[37]32        /// <summary>
33        /// Holds the current gamestate with respect to User Input.
34        /// </summary>
[41]35        private GameState gamestate = new GameState();
[37]36        /// <summary>
[44]37        /// The map of the world.
38        /// </summary>
39        public Map Map = new Map(0);
40        /// <summary>
[40]41        /// The display for the 3D game.
[37]42        /// </summary>
[41]43        public Display3D World3D = new Display3D();
[37]44        /// <summary>
[40]45        /// All of the 2D world components.
46        /// </summary>
47        public GameObject World2D = new GameObject();
48        /// <summary>
[41]49        /// The Minigame factory for making minigames.
[39]50        /// </summary>
[41]51        public MinigameFactory Minigames = new MinigameFactory();
[39]52        /// <summary>
[41]53        /// The screen factory for the screens of the game.
54        /// </summary>
55        public ScreenFactory Screens = new ScreenFactory();
56        /// <summary>
[37]57        /// Projection matrix for the game.
58        /// </summary>
[19]59        public static Matrix ProjectionMatrix;
[41]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>
[63]67        private MainScreen mainScreen = new MainScreen();
[37]68        /// <summary>
[41]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;
[51]80        private GameObject openingSequence = new Opening();
[41]81        /// <summary>
[37]82        /// Creates a new default game driver.
83        /// </summary>
84        public GameDriver()
[3]85        {
86            graphics = new GraphicsDeviceManager(this);
[15]87            graphics.PreferredBackBufferWidth = 1000;
88            graphics.PreferredBackBufferHeight = 500;
[3]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
[42]101            ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), GraphicsDevice.DisplayMode.AspectRatio, 1.0f, 10000.0f);
[41]102            World2D.Add(Minigames);
103            World2D.Add(Screens);
104            World2D.Add(gameMenu);
[44]105            World2D.Add(Map);
[68]106            World2D.Add(Play2DGame(mainScreen, new Vector2(0,0), Map.mStart));
[51]107            World2D.Add(openingSequence);
[18]108
[5]109            this.IsMouseVisible = true;
[40]110            World2D.InitializeAll(this);
[3]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);
[41]122            display.LoadContent(spriteBatch, Content);
[40]123            World2D.LoadAllContent(Content);
[107]124            World3D.InitializeAll(this);
[39]125            World3D.LoadAllContent(Content);
[63]126            if (MainScreen.speech != null)
127            {
128                MainScreen.speech.IsLooped = true;
129                MainScreen.speech.Play();
130            }
[3]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
[15]150            gamestate.Update(gameTime, Keyboard.GetState(), Mouse.GetState());
[37]151            // TODO move this into Display3D
[15]152            if (gamestate.CurrentKeyboardState.IsKeyDown(Keys.Escape))
[3]153                this.Exit();
[37]154            // updates world
[40]155            World2D.UpdateAll(gamestate);
156            World3D.UpdateAll(gamestate);
[3]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        {
[15]166            GraphicsDevice.Clear(Color.MediumPurple);
[40]167            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
[41]168            World3D.DrawAll(display, gamestate);
169            World2D.DrawAll(display, gamestate);
[3]170            base.Draw(gameTime);
171        }
[24]172
[37]173        /// <summary>
[41]174        /// A wrapper function for Minigame Minigames's getMinigame.
[39]175        /// </summary>
176        /// <returns>Returns a new minigame.</returns>
[57]177        public MiniGame GetMinigame()
[39]178        {
[41]179            return Minigames.getMiniGame();
[39]180        }
[40]181
[39]182        /// <summary>
[40]183        /// Starts the game.
[37]184        /// </summary>
[40]185        /// <param name="mainMenu">The main menu.</param>
186        /// <returns>The input game object.</returns>
[41]187        public GameObject PlayGame(GameObject mainMenu)
[24]188        {
[40]189            mainMenu.Visible = false;
[63]190            if (MainScreen.speech != null)
191                MainScreen.speech.Stop();
[40]192            World3D.reset();
[75]193            openingSequence.Visible = true;
194            // EndSequence(); // Remove this when playing opening sequence
[40]195            return mainMenu;
[24]196        }
[51]197        /// <summary>
198        /// Ends the sequence.
199        /// </summary>
200        public void EndSequence()
201        {
[63]202            World3D.Visible = true;
[72]203            Play2DGame(Map.Entrance.DoorLock, new Vector2(0, 0), Map.mStart);
[58]204            // FIXME
205            Map.Entrance.DoorLock.Back.Visible = false;
[51]206        }
[40]207
[68]208        private Point mLoc;
[57]209        private Vector2 otherSide = Vector2.Zero;
[40]210        /// <summary>
211        /// Starts a minigame.
212        /// </summary>
213        /// <param name="game">The minigame that was started.</param>
[99]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>
[40]216        /// <returns>The input game object.</returns>
[68]217        public GameObject Play2DGame(GameObject game, Vector2 otherSide, Point mLoc)
[40]218        {
219            game.Visible = true;
[57]220            this.otherSide = otherSide;
[68]221            this.mLoc = mLoc;
[40]222            World3D.Visible = false;
[60]223            try
224            {
225                MiniGame temp = (MiniGame)game;
226                if (temp.Completed)
227                    CompleteMinigame(game);
228            }
229            catch (Exception)
230            {
231            }
[40]232            return game;
233        }
234
[41]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>
[44]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>
[41]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;
[63]279
280            if(MainScreen.speech != null)
281                MainScreen.speech.Play();
[41]282            mainScreen.Visible = true;
283        }
[99]284        /// <summary>
285        /// Grab a new item.
286        /// </summary>
[61]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        }
[41]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        }
[40]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>
[41]321        public GameObject QuitMinigame(GameObject game)
[40]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>
[41]333        public GameObject CompleteMinigame(GameObject game)
[40]334        {
[58]335            // FIXME
336            Map.Entrance.DoorLock.Back.Visible = true;
[40]337            game.Visible = false;
338            World3D.Visible = true;
[57]339            World3D.SetLocation(otherSide);
[68]340            Map.EnterRoom(this.mLoc);
[40]341            return game;
342        }
[41]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        }
[3]359    }
360}
Note: See TracBrowser for help on using the browser.