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 | |
---|
14 | namespace CastleMaster |
---|
15 | { |
---|
16 | /// <summary> |
---|
17 | /// This is the main type for your game |
---|
18 | /// </summary> |
---|
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 Alpha 0.2"; |
---|
24 | |
---|
25 | private const string UPS_TEXT = " UPS: "; |
---|
26 | private const string FPS_TEXT = " | FPS: "; |
---|
27 | |
---|
28 | private GraphicsDeviceManager graphics; |
---|
29 | private SpriteBatch spriteBatch; |
---|
30 | private InputHandler input; |
---|
31 | private RenderHelper renderer; |
---|
32 | private Camera camera; |
---|
33 | private LevelForest level; |
---|
34 | private Player[] players; |
---|
35 | |
---|
36 | #region Counter |
---|
37 | private readonly TimeSpan ONE_SECOND = TimeSpan.FromSeconds(1.0); |
---|
38 | private TimeSpan currentTime = TimeSpan.Zero; |
---|
39 | private int frames = 0, updates = 0; |
---|
40 | #endregion |
---|
41 | |
---|
42 | public static FastRandom Random { get; private set; } |
---|
43 | |
---|
44 | public static readonly Team TEAM1 = new Team("Team1", 0); |
---|
45 | public static readonly Team TEAM2 = new Team("Team2", 1); |
---|
46 | |
---|
47 | public Game() |
---|
48 | { |
---|
49 | graphics = new GraphicsDeviceManager(this); |
---|
50 | Content.RootDirectory = "Content"; |
---|
51 | input = new InputHandler(Window); |
---|
52 | Random = new FastRandom(); |
---|
53 | players = new Player[2]; |
---|
54 | } |
---|
55 | |
---|
56 | /// <summary> |
---|
57 | /// Allows the game to perform any initialization it needs to before starting to run. |
---|
58 | /// This is where it can query for any required services and load any non-graphic |
---|
59 | /// related content. Calling base.Initialize will enumerate through any components |
---|
60 | /// and initialize them as well. |
---|
61 | /// </summary> |
---|
62 | protected override void Initialize() |
---|
63 | { |
---|
64 | graphics.PreferredBackBufferWidth = WIDTH; |
---|
65 | graphics.PreferredBackBufferHeight = HEIGHT; |
---|
66 | graphics.SynchronizeWithVerticalRetrace = true; |
---|
67 | graphics.ApplyChanges(); |
---|
68 | Window.Title = TITLE; |
---|
69 | |
---|
70 | InitializeInput(); |
---|
71 | |
---|
72 | base.Initialize(); |
---|
73 | } |
---|
74 | |
---|
75 | private void InitializeInput() |
---|
76 | { |
---|
77 | input.RegisterKeyboardKey(Keys.Escape); |
---|
78 | input.RegisterKeyboardKey(Keys.W); |
---|
79 | input.RegisterKeyboardKey(Keys.A); |
---|
80 | input.RegisterKeyboardKey(Keys.S); |
---|
81 | input.RegisterKeyboardKey(Keys.D); |
---|
82 | input.RegisterKeyboardKey(Keys.F4); |
---|
83 | input.RegisterKeyboardKey(Keys.ShiftKey); |
---|
84 | input.RegisterKeyboardKey(Keys.ControlKey); |
---|
85 | |
---|
86 | input.RegisterMouseKey(MouseButtons.Middle); |
---|
87 | input.RegisterMouseKey(MouseButtons.Right); |
---|
88 | input.RegisterMouseKey(MouseButtons.Left); |
---|
89 | } |
---|
90 | |
---|
91 | private void AddPlayer(Player p) |
---|
92 | { |
---|
93 | players[p.Team.ID] = p; |
---|
94 | level.SetPlayer(p, p.Team.ID); |
---|
95 | } |
---|
96 | |
---|
97 | public static Team GetEnemyTeam(Team team) |
---|
98 | { |
---|
99 | return team == TEAM1 ? TEAM2 : TEAM1; |
---|
100 | } |
---|
101 | |
---|
102 | /// <summary> |
---|
103 | /// LoadContent will be called once per game and is the place to load |
---|
104 | /// all of your content. |
---|
105 | /// </summary> |
---|
106 | protected override void LoadContent() |
---|
107 | { |
---|
108 | spriteBatch = new SpriteBatch(GraphicsDevice); |
---|
109 | renderer = new RenderHelper(spriteBatch); |
---|
110 | |
---|
111 | Resources.LoadResources(Content, renderer); |
---|
112 | } |
---|
113 | |
---|
114 | /// <summary> |
---|
115 | /// UnloadContent will be called once per game and is the place to unload |
---|
116 | /// all content. |
---|
117 | /// </summary> |
---|
118 | protected override void UnloadContent() |
---|
119 | { |
---|
120 | // TODO: Unload any non ContentManager content here |
---|
121 | } |
---|
122 | |
---|
123 | protected override void BeginRun() |
---|
124 | { |
---|
125 | base.BeginRun(); |
---|
126 | |
---|
127 | level = new LevelForest(Content.Load<Texture2D>("levels/levelForest")); |
---|
128 | camera = new Camera(level); |
---|
129 | AddPlayer(new PlayerReal(TEAM1, level, camera)); |
---|
130 | AddPlayer(new PlayerAI(TEAM2, level, camera, PlayerAI.DIFFICULTY_HARD, players[0])); |
---|
131 | level.InitLevel(); |
---|
132 | foreach (Player p in players) |
---|
133 | if (p != null) p.OnLevelLoaded(); |
---|
134 | camera.CenterOn(level.Width / 2 * Viewport.TILESIZE, level.Height / 2 * Viewport.TILESIZE); |
---|
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 | input.Update(); |
---|
145 | |
---|
146 | if (InputHandler.HasKeyBeenPressed(Keys.F4)) |
---|
147 | IsFixedTimeStep = !IsFixedTimeStep; |
---|
148 | if (InputHandler.HasKeyBeenPressed(Keys.Escape)) |
---|
149 | this.Exit(); |
---|
150 | |
---|
151 | if (InputHandler.MouseScrollDelta > 1) |
---|
152 | camera.Zoom(Viewport.ZOOM_STEP); |
---|
153 | else if (InputHandler.MouseScrollDelta < 0) |
---|
154 | camera.Zoom(-Viewport.ZOOM_STEP); |
---|
155 | |
---|
156 | foreach (Player p in players) |
---|
157 | if (p != null) |
---|
158 | p.Update(); |
---|
159 | |
---|
160 | camera.Update(); |
---|
161 | |
---|
162 | level.Update(); |
---|
163 | |
---|
164 | updates++; |
---|
165 | currentTime += gameTime.ElapsedGameTime; |
---|
166 | if (currentTime >= ONE_SECOND) |
---|
167 | { |
---|
168 | currentTime = TimeSpan.Zero; |
---|
169 | Window.Title = new StringBuilder(TITLE).Append(FPS_TEXT).Append(frames).Append(UPS_TEXT).Append(updates).ToString(); |
---|
170 | frames = updates = 0; |
---|
171 | } |
---|
172 | |
---|
173 | base.Update(gameTime); |
---|
174 | } |
---|
175 | |
---|
176 | /// <summary> |
---|
177 | /// This is called when the game should draw itself. |
---|
178 | /// </summary> |
---|
179 | /// <param name="gameTime">Provides a snapshot of timing values.</param> |
---|
180 | protected override void Draw(GameTime gameTime) |
---|
181 | { |
---|
182 | GraphicsDevice.Clear(Color.Black); |
---|
183 | |
---|
184 | renderer.BeginRender(); |
---|
185 | |
---|
186 | level.RenderBackground(camera, renderer); |
---|
187 | camera.RenderSelelctor(renderer); |
---|
188 | level.RenderEntities(camera, renderer); |
---|
189 | |
---|
190 | camera.RenderCursor(renderer); |
---|
191 | renderer.EndRender(); |
---|
192 | |
---|
193 | frames++; |
---|
194 | base.Draw(gameTime); |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|