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

Revision 4635, 6.5 KB checked in by dezhidki, 6 years ago (diff)

Talletus.

Line 
1using CastleMaster.Graphics;
2using CastleMaster.Input;
3using CastleMaster.World;
4using Microsoft.Xna.Framework;
5using Microsoft.Xna.Framework.Graphics;
6using SharpNeatLib.Maths;
7using System;
8using System.Text;
9using System.Windows.Forms;
10using Keys = System.Windows.Forms.Keys;
11using Viewport = CastleMaster.Graphics.Viewport;
12using CastleMaster.Players;
13
14namespace 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));
131            level.InitLevel();
132            camera.CenterOn(level.Width / 2 * Viewport.TILESIZE, level.Height / 2 * Viewport.TILESIZE);
133        }
134
135        /// <summary>
136        /// Allows the game to run logic such as updating the world,
137        /// checking for collisions, gathering input, and playing audio.
138        /// </summary>
139        /// <param name="gameTime">Provides a snapshot of timing values.</param>
140        protected override void Update(GameTime gameTime)
141        {
142            input.Update();
143
144            if (InputHandler.HasKeyBeenPressed(Keys.F4))
145                IsFixedTimeStep = !IsFixedTimeStep;
146            if (InputHandler.HasKeyBeenPressed(Keys.Escape))
147                this.Exit();
148
149            if (InputHandler.MouseScrollDelta > 1)
150                camera.Zoom(Viewport.ZOOM_STEP);
151            else if (InputHandler.MouseScrollDelta < 0)
152                camera.Zoom(-Viewport.ZOOM_STEP);
153
154            foreach (Player p in players)
155                if (p != null)
156                    p.Update();
157
158            camera.Update();
159
160            level.Update();
161
162            updates++;
163            currentTime += gameTime.ElapsedGameTime;
164            if (currentTime >= ONE_SECOND)
165            {
166                currentTime = TimeSpan.Zero;
167                Window.Title = new StringBuilder(TITLE).Append(FPS_TEXT).Append(frames).Append(UPS_TEXT).Append(updates).ToString();
168                frames = updates = 0;
169            }
170
171            base.Update(gameTime);
172        }
173
174        /// <summary>
175        /// This is called when the game should draw itself.
176        /// </summary>
177        /// <param name="gameTime">Provides a snapshot of timing values.</param>
178        protected override void Draw(GameTime gameTime)
179        {
180            GraphicsDevice.Clear(Color.Black);
181
182            renderer.BeginRender();
183
184            level.RenderBackground(camera, renderer);
185            camera.RenderSelelctor(renderer);
186            level.RenderEntities(camera, renderer);
187
188            camera.RenderCursor(renderer);
189            renderer.EndRender();
190
191            frames++;
192            base.Draw(gameTime);
193        }
194    }
195}
Note: See TracBrowser for help on using the repository browser.