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

Revision 4749, 10.2 KB checked in by dezhidki, 6 years ago (diff)

Yksinkertainen virheiden loggaus (tallentee virheitä logs kansioon)
Kaupassa voi nyt myydä kaikki puut kerralla
Lisätty tarkemmat ohjeet Game.cs:ään

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