1 | using CastleMaster.Graphics; |
---|
2 | using CastleMaster.Input; |
---|
3 | using CastleMaster.World; |
---|
4 | using Microsoft.Xna.Framework; |
---|
5 | using Microsoft.Xna.Framework.Graphics; |
---|
6 | using SharpNeatLib.Maths; |
---|
7 | using System; |
---|
8 | using System.Text; |
---|
9 | using System.Windows.Forms; |
---|
10 | using Keys = System.Windows.Forms.Keys; |
---|
11 | using Viewport = CastleMaster.Graphics.Viewport; |
---|
12 | using CastleMaster.Players; |
---|
13 | using CastleMaster.Guis; |
---|
14 | |
---|
15 | // NOTES: |
---|
16 | // If VSYNC problems/Lag problems go to line 73 to enable/disable VSYNC |
---|
17 | namespace CastleMaster |
---|
18 | { |
---|
19 | public class Game : Microsoft.Xna.Framework.Game |
---|
20 | { |
---|
21 | public const int WIDTH = 800; |
---|
22 | public const int HEIGHT = 600; |
---|
23 | public const string TITLE = "Castle Master"; |
---|
24 | public const string VERSION = "Alpha 1.5"; |
---|
25 | |
---|
26 | private GraphicsDeviceManager graphics; |
---|
27 | private SpriteBatch spriteBatch; |
---|
28 | private InputHandler input; |
---|
29 | private RenderHelper renderer; |
---|
30 | private Camera camera; |
---|
31 | private LevelForest level; |
---|
32 | private Player[] players; |
---|
33 | private static GuiManager guiManager; |
---|
34 | private GuiDebug debugScreen; |
---|
35 | private GuiPlayer ingame; |
---|
36 | private GuiMainScreen mainScreen; |
---|
37 | private bool isGameRunning = false; |
---|
38 | |
---|
39 | #region Counter |
---|
40 | private readonly TimeSpan ONE_SECOND = TimeSpan.FromSeconds(1.0); |
---|
41 | private TimeSpan currentTime = TimeSpan.Zero; |
---|
42 | private int frames = 0, updates = 0, fps = 0, ups = 0; |
---|
43 | #endregion |
---|
44 | |
---|
45 | public static FastRandom Random { get; private set; } |
---|
46 | |
---|
47 | public static readonly Team TEAM1 = new Team("Team1", 0); |
---|
48 | public static readonly Team TEAM2 = new Team("Team2", 1); |
---|
49 | |
---|
50 | public Game() |
---|
51 | { |
---|
52 | graphics = new GraphicsDeviceManager(this); |
---|
53 | Content.RootDirectory = "Content"; |
---|
54 | input = new InputHandler(Window); |
---|
55 | Random = new FastRandom(); |
---|
56 | players = new Player[2]; |
---|
57 | guiManager = new GuiManager(); |
---|
58 | } |
---|
59 | |
---|
60 | public static GuiManager GuiManager { get { return guiManager; } } |
---|
61 | |
---|
62 | public int FPS { get { return fps; } } |
---|
63 | |
---|
64 | public int UPS { get { return ups; } } |
---|
65 | |
---|
66 | public bool IsGameRunning { get { return isGameRunning; } } |
---|
67 | |
---|
68 | protected override void Initialize() |
---|
69 | { |
---|
70 | graphics.PreferredBackBufferWidth = WIDTH; |
---|
71 | graphics.PreferredBackBufferHeight = HEIGHT; |
---|
72 | // NOTE: If enabled on some computers, the FPS will drop too much to play |
---|
73 | graphics.SynchronizeWithVerticalRetrace = false; |
---|
74 | graphics.ApplyChanges(); |
---|
75 | Window.Title = TITLE; |
---|
76 | |
---|
77 | InitializeInput(); |
---|
78 | |
---|
79 | base.Initialize(); |
---|
80 | } |
---|
81 | |
---|
82 | private void InitializeInput() |
---|
83 | { |
---|
84 | input.RegisterKeyboardKey(Keys.Escape); |
---|
85 | input.RegisterKeyboardKey(Keys.W); |
---|
86 | input.RegisterKeyboardKey(Keys.A); |
---|
87 | input.RegisterKeyboardKey(Keys.S); |
---|
88 | input.RegisterKeyboardKey(Keys.D); |
---|
89 | input.RegisterKeyboardKey(Keys.F4); |
---|
90 | input.RegisterKeyboardKey(Keys.F1); |
---|
91 | input.RegisterKeyboardKey(Keys.ShiftKey); |
---|
92 | input.RegisterKeyboardKey(Keys.ControlKey); |
---|
93 | input.RegisterKeyboardKey(Keys.Up); |
---|
94 | input.RegisterKeyboardKey(Keys.Down); |
---|
95 | input.RegisterKeyboardKey(Keys.Left); |
---|
96 | input.RegisterKeyboardKey(Keys.Right); |
---|
97 | input.RegisterKeyboardKey(Keys.Enter); |
---|
98 | |
---|
99 | input.RegisterMouseKey(MouseButtons.Middle); |
---|
100 | input.RegisterMouseKey(MouseButtons.Right); |
---|
101 | input.RegisterMouseKey(MouseButtons.Left); |
---|
102 | } |
---|
103 | |
---|
104 | public void ResetGame() |
---|
105 | { |
---|
106 | isGameRunning = false; |
---|
107 | level = null; |
---|
108 | camera = null; |
---|
109 | players = new Player[2]; |
---|
110 | ingame.Remove(); |
---|
111 | GC.Collect(); |
---|
112 | } |
---|
113 | |
---|
114 | public void StartGame() |
---|
115 | { |
---|
116 | if (isGameRunning) |
---|
117 | ResetGame(); |
---|
118 | |
---|
119 | isGameRunning = true; |
---|
120 | |
---|
121 | mainScreen.Remove(); |
---|
122 | |
---|
123 | level = new LevelForest(Content.Load<Texture2D>("levels/levelForest")); |
---|
124 | camera = new Camera(level); |
---|
125 | AddPlayer(new PlayerReal(TEAM1, level, camera)); |
---|
126 | AddPlayer(new PlayerAI(TEAM2, level, camera, PlayerAI.DIFFICULTY_NORMAL, players[0])); |
---|
127 | level.InitLevel(); |
---|
128 | foreach (Player p in players) |
---|
129 | if (p != null) p.OnLevelLoaded(); |
---|
130 | camera.CenterOn(level.Width / 2 * Viewport.TILESIZE, level.Height / 2 * Viewport.TILESIZE); |
---|
131 | |
---|
132 | ingame = new GuiPlayer(guiManager, players[0]); |
---|
133 | guiManager.AddGui(ingame, true); |
---|
134 | } |
---|
135 | |
---|
136 | private void AddPlayer(Player p) |
---|
137 | { |
---|
138 | players[p.Team.ID] = p; |
---|
139 | level.SetPlayer(p, p.Team.ID); |
---|
140 | } |
---|
141 | |
---|
142 | public static Team GetEnemyTeam(Team team) |
---|
143 | { |
---|
144 | return team == TEAM1 ? TEAM2 : TEAM1; |
---|
145 | } |
---|
146 | |
---|
147 | protected override void LoadContent() |
---|
148 | { |
---|
149 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
150 | renderer = new RenderHelper(spriteBatch); |
---|
151 | |
---|
152 | Resources.LoadResources(Content, renderer); |
---|
153 | } |
---|
154 | |
---|
155 | protected override void UnloadContent() |
---|
156 | { |
---|
157 | RenderHelper.EmptyTexture.Dispose(); |
---|
158 | } |
---|
159 | |
---|
160 | protected override void BeginRun() |
---|
161 | { |
---|
162 | base.BeginRun(); |
---|
163 | |
---|
164 | debugScreen = new GuiDebug(guiManager, this); |
---|
165 | guiManager.AddGui(debugScreen, false); |
---|
166 | |
---|
167 | mainScreen = new GuiMainScreen(guiManager, this); |
---|
168 | guiManager.AddGui(mainScreen, true, true); |
---|
169 | } |
---|
170 | |
---|
171 | protected override void Update(GameTime gameTime) |
---|
172 | { |
---|
173 | input.Update(); |
---|
174 | |
---|
175 | if (InputHandler.HasKeyBeenPressed(Keys.F1)) |
---|
176 | { |
---|
177 | debugScreen.IsActive = !debugScreen.IsActive; |
---|
178 | } |
---|
179 | if (InputHandler.HasKeyBeenPressed(Keys.F4)) |
---|
180 | IsFixedTimeStep = !IsFixedTimeStep; |
---|
181 | if (InputHandler.HasKeyBeenPressed(Keys.Escape)) |
---|
182 | { |
---|
183 | if (isGameRunning) |
---|
184 | { |
---|
185 | if (!mainScreen.IsActive) |
---|
186 | guiManager.AddGui(mainScreen, true, true); |
---|
187 | else |
---|
188 | mainScreen.Remove(); |
---|
189 | } |
---|
190 | else Exit(); |
---|
191 | } |
---|
192 | |
---|
193 | if (isGameRunning && !mainScreen.IsActive) |
---|
194 | { |
---|
195 | if (InputHandler.MouseScrollDelta > 1) |
---|
196 | camera.Zoom(Viewport.ZOOM_STEP); |
---|
197 | else if (InputHandler.MouseScrollDelta < 0) |
---|
198 | camera.Zoom(-Viewport.ZOOM_STEP); |
---|
199 | |
---|
200 | foreach (Player p in players) |
---|
201 | if (p != null) |
---|
202 | p.Update(); |
---|
203 | //players[0].Update(); |
---|
204 | |
---|
205 | camera.Update(); |
---|
206 | |
---|
207 | level.Update(); |
---|
208 | } |
---|
209 | |
---|
210 | guiManager.Update(); |
---|
211 | |
---|
212 | updates++; |
---|
213 | currentTime += gameTime.ElapsedGameTime; |
---|
214 | if (currentTime >= ONE_SECOND) |
---|
215 | { |
---|
216 | currentTime = TimeSpan.Zero; |
---|
217 | fps = frames; |
---|
218 | ups = updates; |
---|
219 | frames = updates = 0; |
---|
220 | } |
---|
221 | |
---|
222 | base.Update(gameTime); |
---|
223 | } |
---|
224 | |
---|
225 | protected override void Draw(GameTime gameTime) |
---|
226 | { |
---|
227 | GraphicsDevice.Clear(Color.Black); |
---|
228 | |
---|
229 | renderer.BeginRender(); |
---|
230 | |
---|
231 | if (isGameRunning) |
---|
232 | { |
---|
233 | level.RenderBackground(camera, renderer); |
---|
234 | camera.RenderSelelctor(renderer); |
---|
235 | level.RenderEntities(camera, renderer); |
---|
236 | } |
---|
237 | |
---|
238 | guiManager.Render(renderer); |
---|
239 | if (isGameRunning) |
---|
240 | camera.RenderCursor(renderer); |
---|
241 | renderer.EndRender(); |
---|
242 | |
---|
243 | frames++; |
---|
244 | base.Draw(gameTime); |
---|
245 | } |
---|
246 | } |
---|
247 | } |
---|