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

Revision 4751, 10.4 KB checked in by dezhidki, 6 years ago (diff)

Lisätty vihollisen kunikaan elämäpalkki

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 readonly Team TEAM1 = new Team("Team1", 0);
108        public static readonly Team TEAM2 = new Team("Team2", 1);
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        }
236
237        protected override void UnloadContent()
238        {
239            RenderHelper.EmptyTexture.Dispose();
240        }
241
242        protected override void BeginRun()
243        {
244            base.BeginRun();
245
246            debugScreen = new GuiDebug(guiManager, this);
247            guiManager.AddGui(debugScreen, false);
248
249            mainScreen = new GuiMainScreen(guiManager, this);
250            OpenMainMenu();
251        }
252
253        protected override void Update(GameTime gameTime)
254        {
255            input.Update();
256
257            if (InputHandler.HasKeyBeenPressed(Keys.F1))
258            {
259                debugScreen.IsActive = !debugScreen.IsActive;
260            }
261            if (InputHandler.HasKeyBeenPressed(Keys.F4))
262                IsFixedTimeStep = !IsFixedTimeStep;
263            if (InputHandler.HasKeyBeenPressed(Keys.Escape) && (guiManager.ImportantGui == null || !(guiManager.ImportantGui is GuiWinLooseMessage)))
264            {
265                if (isGameRunning)
266                {
267                    if (!mainScreen.IsActive)
268                        guiManager.AddGui(mainScreen, true, true);
269                    else
270                        mainScreen.Remove();
271
272                    IsGamePaused = mainScreen.IsActive;
273                }
274                else Exit();
275            }
276
277            if (isGameRunning && !IsGamePaused)
278            {
279                if (InputHandler.MouseScrollDelta > 1)
280                    camera.Zoom(Viewport.ZOOM_STEP);
281                else if (InputHandler.MouseScrollDelta < 0)
282                    camera.Zoom(-Viewport.ZOOM_STEP);
283
284                foreach (Player p in players)
285                    if (p != null)
286                        p.Update();
287
288                camera.Update();
289
290                level.Update();
291            }
292
293            guiManager.Update();
294
295            updates++;
296            currentTime += gameTime.ElapsedGameTime;
297            if (currentTime >= ONE_SECOND)
298            {
299                currentTime = TimeSpan.Zero;
300                fps = frames;
301                ups = updates;
302                frames = updates = 0;
303            }
304
305            base.Update(gameTime);
306        }
307
308        protected override void Draw(GameTime gameTime)
309        {
310            GraphicsDevice.Clear(Color.Black);
311
312            renderer.BeginRender();
313
314            if (isGameRunning)
315            {
316                level.RenderBackground(camera, renderer);
317                camera.RenderSelelctor(renderer);
318                level.RenderEntities(camera, renderer);
319            }
320
321            guiManager.Render(renderer);
322            if (isGameRunning)
323                camera.RenderCursor(renderer);
324            renderer.EndRender();
325
326            frames++;
327            base.Draw(gameTime);
328        }
329    }
330}
Note: See TracBrowser for help on using the repository browser.