]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Display.cs
fc2489530e8f53caea9de9b88f5f8ff99be9fb78
[chaz/carfire] / CarFire / CarFire / CarFire / Display.cs
1 #undef SINGLE_TEST
2
3 // Define INGAME_ZOOM to allow zooming in and out with
4 // the PageUp and PageDown keys.
5 #define INGAME_ZOOM
6
7 using System;
8 using System.Collections.Generic;
9 using System.Linq;
10 using System.Text;
11 using Microsoft.Xna.Framework;
12 using Microsoft.Xna.Framework.Content;
13 using Microsoft.Xna.Framework.Graphics;
14 using Microsoft.Xna.Framework.Input;
15
16 namespace CarFire
17 {
18 /// <summary>
19 /// This class is responsible for controlling what draws to the screen when the game is running.
20 /// </summary>
21 public class Display
22 {
23 bool playerChosen = false;
24 List<Projectile> mProjectiles = new List<Projectile>();
25 //List<IPlayer> mCharacters = new List<IPlayer>();
26 IPlayer[] mCharacters = new IPlayer[4];
27 Texture2D everything;
28 Texture2D projectile1;
29 Map mMap;
30 int currentCenterX = 5;
31 int currentCenterY = 5;
32 #if SINGLE_TEST
33 List<Keys> mLastPressedKeys = new List<Keys>();
34 #endif
35 public Display()
36 {
37 /*
38 mMap = aMap;
39 mCharacters = characters;
40 */
41 }
42
43 /// <summary>
44 /// LoadContent will be called once per game and is the place to load
45 /// all of your content.
46 /// </summary>
47 public void LoadContent(ContentManager contentManager)
48 {
49 everything = contentManager.Load<Texture2D>("cs");
50 projectile1 = contentManager.Load<Texture2D>("projectile");
51 mMap = contentManager.Load<Map>("Maps/stable");
52 Map.DefaultTile = contentManager.Load<Texture2D>("default");
53 mMap.CenterCell = new Vector2(currentCenterX,currentCenterY);
54 //Debugging... Spawn eight projectiles.
55 //Diagonals
56 /*
57 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5,5), 10, 10, 300, 300));
58 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 5), 10, 10, 300, 300));
59 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, -5), 10, 10, 300, 300));
60 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, -5), 10, 10, 300, 300));
61 //Vertical and horizontal
62 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, 5), 10, 10, 300, 300));
63 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5, 0), 10, 10, 300, 300));
64 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(5, 0), 10, 10, 300, 300));
65 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(0, -5), 10, 10, 300, 300));
66 */
67
68
69 // TODO: use this.Content to load your game content here
70 }
71
72 /// <summary>
73 /// UnloadContent will be called once per game and is the place to unload
74 /// all content.
75 /// </summary>
76 public void UnloadContent()
77 {
78 // TODO: Unload any non ContentManager content here
79 }
80
81 /// <summary>
82 /// Allows the game to run logic such as updating the world,
83 /// checking for collisions, gathering input, and playing audio.
84 /// </summary>
85 /// <param name="gameTime">Provides a snapshot of timing values.</param>
86 public void Update(TimeSpan timespan, GameState state)
87 {
88
89 //INPUT - testing input... has to be through network later
90 #if SINGLE_TEST
91 KeyboardState keyState = Keyboard.GetState();
92
93 List<Keys> pressedKeys = new List<Keys>();
94 List<Keys> releasedKeys = new List<Keys>();
95
96 Keys[] pressedKeysArray = keyState.GetPressedKeys();
97 foreach (Keys k in pressedKeysArray)
98 {
99 if (!mLastPressedKeys.Contains(k)) pressedKeys.Add(k);
100 else mLastPressedKeys.Remove(k);
101 }
102
103 releasedKeys = mLastPressedKeys;
104 mLastPressedKeys = new List<Keys>(pressedKeysArray);
105 //Just apply input for the first player
106 mCharacters[0].MovePlayer(pressedKeys);
107 if (pressedKeys.Contains(Keys.Enter) && !releasedKeys.Contains(Keys.Enter))
108 {
109 mProjectiles.Add(new Projectile(mMap, everything, new Vector2(-5,0), mCharacters[0].GridX +1, mCharacters[0].GridY+1, (int)mCharacters[0].GridX*(int)Map.PixelsToUnitSquares,(int)mCharacters[0].GridY*(int)Map.PixelsToUnitSquares));
110 }
111 mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY);
112 #endif
113
114 //Handle projectiles - update and check for wall collisions
115 for (int i = 0; i < mProjectiles.Count; i++ )
116 {
117 bool removed = false;
118 if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY)))
119 {
120
121 mProjectiles.RemoveAt(i);
122 removed = true;
123 i--;
124 }
125 if(!removed)
126 mProjectiles[i].Update(timespan);
127
128 }
129 //Check for collisons
130 for (int j = 0; j < mCharacters.Length; j++)
131 {
132
133 if(mCharacters[j] != null)
134 for (int i = 0; i < mProjectiles.Count; i++)
135 {
136 if (mProjectiles[i].GridX == mCharacters[j].GridX && mProjectiles[i].GridY == mCharacters[j].GridY)
137 {
138 mCharacters[j].causeDamageTo(mProjectiles[i].Damage);
139 Console.WriteLine(mCharacters[j].Health);
140 mProjectiles.RemoveAt(i);
141 i--;
142 }
143 }
144 }
145 //Update input for each player
146 for (int i = 0; i < 4; i++)
147 {
148 //If player has not selected a player yet let them select one.
149 if (mCharacters[i] == null)
150 {
151 if (state.keysDown[i].Contains(Keys.Enter))
152 {
153 mCharacters[i] = (new Human(mMap, "", everything, projectile1, this));
154 mCharacters[i].GridX = mMap.GetStartingPositionForPlayer(i + 1).X;
155 mCharacters[i].GridY = mMap.GetStartingPositionForPlayer(i + 1).Y;
156 }
157 }
158 //Regular player input updates
159 else
160 {
161
162 mCharacters[i].MovePlayer(state.keysDown[i]);
163 mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY);
164 }
165 }
166 //Handle wall collisions of projectiles again...
167 for (int i = 0; i < mProjectiles.Count; i++)
168 {
169 if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY)))
170 {
171 mProjectiles.RemoveAt(i);
172 i--;
173 }
174
175 }
176
177 #if INGAME_ZOOM
178 if (Keyboard.GetState().IsKeyDown(Keys.PageUp)) mMap.Zoom = mMap.Zoom + 0.5f;
179 if (Keyboard.GetState().IsKeyDown(Keys.PageDown)) mMap.Zoom = mMap.Zoom - 0.5f;
180 #endif
181 }
182
183 /// <summary>
184 /// This is called when the game should draw itself.
185 /// </summary>
186 /// <param name="spriteBatch">Used to draw with</param>
187 public void Draw(SpriteBatch spriteBatch)
188 {
189 mMap.Draw(spriteBatch);
190 foreach(Projectile projectile in mProjectiles)
191 {
192 projectile.Draw(spriteBatch);
193
194 }
195 for(int i = 0; i < 4; i++)//IPlayer character in mCharacters)
196 {
197 if(mCharacters[i] != null)
198 mCharacters[i].Draw(spriteBatch);
199 }
200 }
201 /// <summary>
202 /// Add a projectile to the Display.
203 /// </summary>
204 /// <param name="projectile"></param>
205 public void AddProjectiles(Projectile projectile)
206 {
207 mProjectiles.Add(projectile);
208 }
209 }
210 }
211
This page took 0.03904 seconds and 3 git commands to generate.