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

Revision 59, 10.8 KB (checked in by acarter, 2 years ago)

Updating Map with Key, and varying the minigames.

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 GameObject 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)));
107            World2D.Add(openingSequence);
108
109            this.IsMouseVisible = true;
110            World2D.InitializeAll(this);
111            World3D.InitializeAll(this);
112            base.Initialize();
113        }
114
115        /// <summary>
116        /// LoadContent will be called once per game and is the place to load
117        /// all of your content.
118        /// </summary>
119        protected override void LoadContent()
120        {
121            // Create a new SpriteBatch, which can be used to draw textures.
122            spriteBatch = new SpriteBatch(GraphicsDevice);
123            display.LoadContent(spriteBatch, Content);
124            World2D.LoadAllContent(Content);
125            World3D.LoadAllContent(Content);
126        }
127
128        /// <summary>
129        /// UnloadContent will be called once per game and is the place to unload
130        /// all content.
131        /// </summary>
132        protected override void UnloadContent()
133        {
134            // TODO: Unload any non ContentManager content here
135        }
136
137        /// <summary>
138        /// Allows the game to run logic such as updating the world,
139        /// checking for collisions, gathering input, and playing audio.
140        /// </summary>
141        /// <param name="gameTime">Provides a snapshot of timing values.</param>
142        protected override void Update(GameTime gameTime)
143        {
144            // Allows the game to exit
145            gamestate.Update(gameTime, Keyboard.GetState(), Mouse.GetState());
146            // TODO move this into Display3D
147            if (gamestate.CurrentKeyboardState.IsKeyDown(Keys.Escape))
148                this.Exit();
149            // updates world
150            World2D.UpdateAll(gamestate);
151            World3D.UpdateAll(gamestate);
152            base.Update(gameTime);
153        }
154
155        /// <summary>
156        /// This is called when the game should draw itself.
157        /// </summary>
158        /// <param name="gameTime">Provides a snapshot of timing values.</param>
159        protected override void Draw(GameTime gameTime)
160        {
161            GraphicsDevice.Clear(Color.MediumPurple);
162            GraphicsDevice.DepthStencilState = DepthStencilState.Default;
163            World3D.DrawAll(display, gamestate);
164            World2D.DrawAll(display, gamestate);
165            base.Draw(gameTime);
166        }
167
168        /// <summary>
169        /// A wrapper function for Minigame Minigames's getMinigame.
170        /// </summary>
171        /// <returns>Returns a new minigame.</returns>
172        public MiniGame GetMinigame()
173        {
174            return Minigames.getMiniGame();
175        }
176
177        /// <summary>
178        /// Starts the game.
179        /// </summary>
180        /// <param name="mainMenu">The main menu.</param>
181        /// <returns>The input game object.</returns>
182        public GameObject PlayGame(GameObject mainMenu)
183        {
184            mainMenu.Visible = false;
185            World3D.reset();
186            openingSequence.Visible = true;
187            return mainMenu;
188        }
189        /// <summary>
190        /// Ends the sequence.
191        /// </summary>
192        public void EndSequence()
193        {
194            Play2DGame(Map.Entrance.DoorLock, new Vector2(0, 0));
195            // FIXME
196            Map.Entrance.DoorLock.Back.Visible = false;
197        }
198
199        private Vector2 otherSide = Vector2.Zero;
200        /// <summary>
201        /// Starts a minigame.
202        /// </summary>
203        /// <param name="game">The minigame that was started.</param>
204        /// <returns>The input game object.</returns>
205        public GameObject Play2DGame(GameObject game, Vector2 otherSide)
206        {
207            game.Visible = true;
208            this.otherSide = otherSide;
209            World3D.Visible = false;
210            return game;
211        }
212
213        /// <summary>
214        /// Displays the game menu.
215        /// </summary>
216        public void DisplayGameMenu()
217        {
218            World3D.Updating = false;
219            gameMenu.Visible = true;
220        }
221        /// <summary>
222        /// Displays the map.
223        /// </summary>
224        public void DisplayMap()
225        {
226            World3D.Updating = false;
227            Map.Visible = true;
228        }
229        /// <summary>
230        /// Closes the map.
231        /// </summary>
232        public void CloseMap()
233        {
234            World3D.Visible = true;
235            Map.Visible = false;
236        }
237        /// <summary>
238        /// Closes the game menu.
239        /// </summary>
240        public void CloseGameMenu()
241        {
242            World3D.Visible = true;
243            gameMenu.Visible = false;
244        }
245        /// <summary>
246        /// Displays the main menu.
247        /// </summary>
248        public void DisplayMainMenu()
249        {
250            World3D.reset();
251            World3D.Visible = false;
252            if (currentMinigame != null)
253                currentMinigame.Visible = false;
254            if (currentScreen != null)
255                currentScreen.Visible = false;
256            gameMenu.Visible = false;
257            mainScreen.Visible = true;
258        }
259
260        /// <summary>
261        /// Displays a screen such as the inventory, or encyclophant.
262        /// </summary>
263        /// <param name="screen">The screen to be displayed.</param>
264        /// <returns>The screen that is now displayed.</returns>
265        public GameObject DisplayScreen(GameObject screen)
266        {
267            if (currentScreen != null)
268                CloseScreen();
269            currentScreen = screen;
270            screen.Visible = true;
271            World3D.Visible = false;
272            return screen;
273        }
274
275        /// <summary>
276        /// Ends a minigame without solving it.
277        /// </summary>
278        /// <param name="game">The minigame that was ended.</param>
279        /// <returns>The input game object.</returns>
280        public GameObject QuitMinigame(GameObject game)
281        {
282            game.Visible = false;
283            World3D.Visible = true;
284            return game;
285        }
286
287        /// <summary>
288        /// Succesfully completed a minigame.
289        /// </summary>
290        /// <param name="game">The minigame that was completed.</param>
291        /// <returns>The input game object.</returns>
292        public GameObject CompleteMinigame(GameObject game)
293        {
294            // FIXME
295            Map.Entrance.DoorLock.Back.Visible = true;
296            game.Visible = false;
297            World3D.Visible = true;
298            World3D.SetLocation(otherSide);
299            Map.EnterRoom();
300            return game;
301        }
302        /// <summary>
303        /// Closes the current screen.
304        /// </summary>
305        ///
306        /// <returns>The screen that was closed.</returns>
307        public GameObject CloseScreen()
308        {
309            GameObject screen = currentScreen;
310            currentScreen = null;
311            if (screen != null)
312            {
313                screen.Visible = false;
314                World3D.Visible = true;
315            }
316            return screen;
317        }
318    }
319}
Note: See TracBrowser for help on using the browser.