source: 2013/30/DenisZ/CastleMaster/CastleMaster/CastleMaster/Game.cs @ 4752

Revision 4752, 10.7 KB checked in by dezhidki, 6 years ago (diff)
  • Muokattu AStarin debuggausta
  • Väritetty pelaajien pelinappuloita
Line 
1#region Information
2/*  Castle Master by Denis Zhidkikh
3 *
4 * Controls:
5 * Use W, A, S, D to move around the map
6 * Hold Shift while moving to move faster
7 * Hold middle mouse button and drag to move the map
8 * Scroll to zoom in/out
9 * Click on unit with right mouse button to select it
10 * Hold Ctrl while clicking to choose multiple units
11 * Give orders to your units by clicking left mouse button (ordering button) on other units/entities
12 *
13 * Classes:
14 * Woodcutter:
15 * - Costs 5 gold
16 * - Has 20 health
17 * - Really fast
18 * - Order to cut by clicking on the tree with the ordering button
19 * - Order to walk by clicking on the empty tile with the ordering button
20 * - After cutting first tree woodcutter will begin looking for nearby trees to cut
21 *
22 * Warrior:
23 * - Costs 20 gold
24 * - Has 50 health
25 * - Slow
26 * - Order to attack enemy units by clicking on them with the ordering button
27 * - After killing will stay in place until he either gets another order or gets attacked by the enemy
28 *
29 * Ranger:
30 * - Costs 15 gold
31 * - Has 30 health
32 * - Medium speed
33 * - Attacks the same way the warrior does
34 *
35 * Units:
36 * King (and the castle):
37 * - Has 400 health
38 * - Doesn't do anything
39 * - The game ends after being destroyed
40 *
41 * Store (large wooden building):
42 * - Has 400 health
43 * - Can exchange wood to gold
44 * - Selecting it will open the exchange window
45 * - Cannot exchange wood to gold after being destroyed
46 *
47 * Armory (small stone building):
48 * - Has 400 health
49 * - Uses money to buy woodcutters, warriors and rangers
50 * - Selecting it will open the armory window
51 * - Cannot buy more units after being destroyed
52 *
53 * Objective:
54 * Win game by destroying enemy's castle. Make it easier by killing enemy's woodcutters and destroying the store and/or armory.
55 *
56 */
57
58#endregion
59
60using CastleMaster.Graphics;
61using CastleMaster.Input;
62using CastleMaster.World;
63using Microsoft.Xna.Framework;
64using Microsoft.Xna.Framework.Graphics;
65using SharpNeatLib.Maths;
66using System;
67using System.Text;
68using System.Windows.Forms;
69using Keys = System.Windows.Forms.Keys;
70using Viewport = CastleMaster.Graphics.Viewport;
71using CastleMaster.Players;
72using CastleMaster.Guis;
73
74// NOTES:
75// If VSYNC problems/Lag problems go to line 73 to enable/disable VSYNC
76namespace CastleMaster
77{
78    public class Game : Microsoft.Xna.Framework.Game
79    {
80        public const int WIDTH = 800;
81        public const int HEIGHT = 600;
82        public const string TITLE = "Castle Master";
83        public const string VERSION = "Alpha";
84
85        private GraphicsDeviceManager graphics;
86        private SpriteBatch spriteBatch;
87        private InputHandler input;
88        private RenderHelper renderer;
89        private Camera camera;
90        private LevelForest level;
91        private static Player[] players;
92        private static GuiManager guiManager;
93        private GuiDebug debugScreen;
94        private GuiPlayer ingame;
95        private GuiMainScreen mainScreen;
96        private bool isGameRunning = false;
97        private static Game instance;
98
99        #region Counter
100        private readonly TimeSpan ONE_SECOND = TimeSpan.FromSeconds(1.0);
101        private TimeSpan currentTime = TimeSpan.Zero;
102        private int frames = 0, updates = 0, fps = 0, ups = 0;
103        #endregion
104
105        public static FastRandom Random { get; private set; }
106
107        public static Team TEAM1 { get; private set; }
108        public static Team TEAM2 { get; private set; }
109
110        public Game()
111        {
112            graphics = new GraphicsDeviceManager(this);
113            Content.RootDirectory = "Content";
114            input = new InputHandler(Window);
115            Random = new FastRandom();
116            players = new Player[2];
117            guiManager = new GuiManager();
118            IsGamePaused = true;
119            instance = this;
120        }
121
122        public static GuiManager GuiManager { get { return guiManager; } }
123
124        public int FPS { get { return fps; } }
125
126        public int UPS { get { return ups; } }
127
128        public bool IsGameRunning { get { return isGameRunning; } }
129
130        public bool IsGamePaused { get; set; }
131
132        public static Game Instance { get { return instance; } }
133
134        protected override void Initialize()
135        {
136            graphics.PreferredBackBufferWidth = WIDTH;
137            graphics.PreferredBackBufferHeight = HEIGHT;
138            // NOTE: If enabled on some computers, the FPS will drop too much to play
139            graphics.SynchronizeWithVerticalRetrace = false;
140            graphics.ApplyChanges();
141            Window.Title = TITLE;
142
143            InitializeInput();
144
145            base.Initialize();
146        }
147
148        private void InitializeInput()
149        {
150            input.RegisterKeyboardKey(Keys.Escape);
151            input.RegisterKeyboardKey(Keys.W);
152            input.RegisterKeyboardKey(Keys.A);
153            input.RegisterKeyboardKey(Keys.S);
154            input.RegisterKeyboardKey(Keys.D);
155            input.RegisterKeyboardKey(Keys.F4);
156            input.RegisterKeyboardKey(Keys.F1);
157            input.RegisterKeyboardKey(Keys.F2);
158            input.RegisterKeyboardKey(Keys.ShiftKey);
159            input.RegisterKeyboardKey(Keys.ControlKey);
160            input.RegisterKeyboardKey(Keys.Up);
161            input.RegisterKeyboardKey(Keys.Down);
162            input.RegisterKeyboardKey(Keys.Left);
163            input.RegisterKeyboardKey(Keys.Right);
164            input.RegisterKeyboardKey(Keys.Enter);
165            input.RegisterKeyboardKey(Keys.X);
166
167            input.RegisterMouseKey(MouseButtons.Middle);
168            input.RegisterMouseKey(MouseButtons.Right);
169            input.RegisterMouseKey(MouseButtons.Left);
170        }
171
172        public void OpenMainMenu()
173        {
174            ResetGame();
175            guiManager.AddGui(mainScreen, true, true);
176        }
177
178        public void ResetGame()
179        {
180            IsGamePaused = true;
181            isGameRunning = false;
182            level = null;
183            camera = null;
184            players = new Player[2];
185            if (ingame != null)
186                ingame.Remove();
187            GC.Collect();
188        }
189
190        public void StartGame()
191        {
192            if (isGameRunning)
193                ResetGame();
194
195            IsGamePaused = false;
196            isGameRunning = true;
197
198            mainScreen.Remove();
199
200            level = new LevelForest(Content.Load<Texture2D>("levels/levelForest"));
201            camera = new Camera(level);
202            AddPlayer(new PlayerReal(TEAM1, level, camera));
203            AddPlayer(new PlayerAI(TEAM2, level, camera, PlayerAI.DIFFICULTY_EASY, players[0]));
204            level.InitLevel();
205            foreach (Player p in players)
206                if (p != null) p.OnLevelLoaded();
207            camera.CenterOn(100 * Viewport.TILESIZE, 15 * Viewport.TILESIZE);
208
209            ingame = new GuiPlayer(guiManager, players[0]);
210            guiManager.AddGui(ingame, true);
211        }
212
213        private void AddPlayer(Player p)
214        {
215            players[p.Team.ID] = p;
216            level.SetPlayer(p, p.Team.ID);
217        }
218
219        public static Team GetEnemyTeam(Team team)
220        {
221            return team == TEAM1 ? TEAM2 : TEAM1;
222        }
223
224        public static Player GetEnemyPlayer(Player player)
225        {
226            return player.Team == TEAM1 ? players[1] : players[0];
227        }
228
229        protected override void LoadContent()
230        {
231            spriteBatch = new SpriteBatch(GraphicsDevice);
232            renderer = new RenderHelper(spriteBatch);
233
234            Resources.LoadResources(Content, renderer);
235            TEAM1 = new Team("BLUE", 0, Resources.SPRITESHEET_WOODCUTTER_BLU, Resources.SPRITESHEET_WARRIOR_BLU, Resources.SPRITESHEET_RANGER_BLU);
236            TEAM2 = new Team("RED", 1, Resources.SPRITESHEET_WOODCUTTER_RED, Resources.SPRITESHEET_WARRIOR_RED, Resources.SPRITESHEET_RANGER_RED);
237        }
238
239        protected override void UnloadContent()
240        {
241            RenderHelper.EmptyTexture.Dispose();
242        }
243
244        protected override void BeginRun()
245        {
246            base.BeginRun();
247
248            debugScreen = new GuiDebug(guiManager, this);
249            guiManager.AddGui(debugScreen, false);
250
251            mainScreen = new GuiMainScreen(guiManager, this);
252            OpenMainMenu();
253        }
254
255        protected override void Update(GameTime gameTime)
256        {
257            input.Update();
258
259            if (InputHandler.HasKeyBeenPressed(Keys.F1))
260            {
261                debugScreen.IsActive = !debugScreen.IsActive;
262            }
263            if (InputHandler.HasKeyBeenPressed(Keys.F4))
264                IsFixedTimeStep = !IsFixedTimeStep;
265            if (InputHandler.HasKeyBeenPressed(Keys.Escape) && (guiManager.ImportantGui == null || !(guiManager.ImportantGui is GuiWinLooseMessage)))
266            {
267                if (isGameRunning)
268                {
269                    if (!mainScreen.IsActive)
270                        guiManager.AddGui(mainScreen, true, true);
271                    else
272                        mainScreen.Remove();
273
274                    IsGamePaused = mainScreen.IsActive;
275                }
276                else Exit();
277            }
278
279            if (isGameRunning && !IsGamePaused)
280            {
281                if (InputHandler.MouseScrollDelta > 1)
282                    camera.Zoom(Viewport.ZOOM_STEP);
283                else if (InputHandler.MouseScrollDelta < 0)
284                    camera.Zoom(-Viewport.ZOOM_STEP);
285
286                foreach (Player p in players)
287                    if (p != null)
288                        p.Update();
289
290                camera.Update();
291
292                level.Update();
293            }
294
295            guiManager.Update();
296
297            updates++;
298            currentTime += gameTime.ElapsedGameTime;
299            if (currentTime >= ONE_SECOND)
300            {
301                currentTime = TimeSpan.Zero;
302                fps = frames;
303                ups = updates;
304                frames = updates = 0;
305            }
306
307            base.Update(gameTime);
308        }
309
310        protected override void Draw(GameTime gameTime)
311        {
312            GraphicsDevice.Clear(Color.Black);
313
314            renderer.BeginRender();
315
316            if (isGameRunning)
317            {
318                level.RenderBackground(camera, renderer);
319                camera.RenderSelelctor(renderer);
320                level.RenderEntities(camera, renderer);
321            }
322
323            guiManager.Render(renderer);
324            if (isGameRunning)
325                camera.RenderCursor(renderer);
326            renderer.EndRender();
327
328            frames++;
329            base.Draw(gameTime);
330        }
331    }
332}
Note: See TracBrowser for help on using the repository browser.