]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Display.cs
git-svn-id: https://bd85.net/svn/cs3505_group@114 92bb83a3-7c8f-8a45-bc97-515c4e399668
[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 }
55
56 /// <summary>
57 /// UnloadContent will be called once per game and is the place to unload
58 /// all content.
59 /// </summary>
60 public void UnloadContent()
61 {
62 // TODO: Unload any non ContentManager content here
63 }
64
65 /// <summary>
66 /// Allows the game to run logic such as updating the world,
67 /// checking for collisions, gathering input, and playing audio.
68 /// </summary>
69 /// <param name="gameTime">Provides a snapshot of timing values.</param>
70 public void Update(TimeSpan timespan, GameState state)
71 {
72
73 //INPUT - testing input... has to be through network later
74 #if SINGLE_TEST
75 KeyboardState keyState = Keyboard.GetState();
76
77 List<Keys> pressedKeys = new List<Keys>();
78 List<Keys> releasedKeys = new List<Keys>();
79
80 Keys[] pressedKeysArray = keyState.GetPressedKeys();
81 foreach (Keys k in pressedKeysArray)
82 {
83 if (!mLastPressedKeys.Contains(k)) pressedKeys.Add(k);
84 else mLastPressedKeys.Remove(k);
85 }
86
87 releasedKeys = mLastPressedKeys;
88 mLastPressedKeys = new List<Keys>(pressedKeysArray);
89 //Just apply input for the first player
90 mCharacters[0].MovePlayer(pressedKeys);
91 if (pressedKeys.Contains(Keys.Enter) && !releasedKeys.Contains(Keys.Enter))
92 {
93 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));
94 }
95 mMap.CenterCell = new Vector2(mCharacters[0].GridX, mCharacters[0].GridY);
96 #endif
97
98 //Handle projectiles - update and check for wall collisions
99 for (int i = 0; i < mProjectiles.Count; i++ )
100 {
101 bool removed = false;
102 if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY)))
103 {
104
105 mProjectiles.RemoveAt(i);
106 removed = true;
107 i--;
108 }
109 if(!removed)
110 mProjectiles[i].Update(timespan);
111
112 }
113 //Check for collisons
114 for (int j = 0; j < mCharacters.Length; j++)
115 {
116
117 if(mCharacters[j] != null)
118 for (int i = 0; i < mProjectiles.Count; i++)
119 {
120 if (mProjectiles[i].GridX == mCharacters[j].Coordinates.X && mProjectiles[i].GridY == mCharacters[j].Coordinates.Y)
121 {
122 mCharacters[j].causeDamageTo(mProjectiles[i].Damage);
123 Console.WriteLine(mCharacters[j].Health);
124 mProjectiles.RemoveAt(i);
125 i--;
126 }
127 }
128 }
129 //Update input for each player
130 for (int i = 0; i < 4; i++)
131 {
132 //If player has not selected a player yet let them select one.
133 if (mCharacters[i] == null)
134 {
135 if (state.keysDown[i].Contains(Keys.Enter))
136 {
137 mCharacters[i] = new Human(mMap, "", everything, projectile1, this, mMap.GetStartingPositionForPlayer(i+1));
138 }
139 }
140 //Regular player input updates
141 else
142 {
143
144 mCharacters[i].MovePlayer(timespan, state.keysDown[i]);
145
146 }
147 }
148 if (mCharacters[0] != null)
149 {
150 mMap.CenterCell = mCharacters[0].Position;
151 }
152 //Handle wall collisions of projectiles again...
153 for (int i = 0; i < mProjectiles.Count; i++)
154 {
155 if (!mMap.IsCellOpen(new Point(mProjectiles[i].GridX, mProjectiles[i].GridY)))
156 {
157 mProjectiles.RemoveAt(i);
158 i--;
159 }
160
161 }
162
163 #if INGAME_ZOOM
164 if (Keyboard.GetState().IsKeyDown(Keys.PageUp)) mMap.Zoom = mMap.Zoom + 0.5f;
165 if (Keyboard.GetState().IsKeyDown(Keys.PageDown)) mMap.Zoom = mMap.Zoom - 0.5f;
166 #endif
167 }
168
169 /// <summary>
170 /// This is called when the game should draw itself.
171 /// </summary>
172 /// <param name="spriteBatch">Used to draw with</param>
173 public void Draw(SpriteBatch spriteBatch)
174 {
175 mMap.Draw(spriteBatch);
176 foreach(Projectile projectile in mProjectiles)
177 {
178 projectile.Draw(spriteBatch);
179 }
180 for(int i = 0; i < 4; i++)//IPlayer character in mCharacters)
181 {
182
183 if (mCharacters[i] != null)
184 {
185 mCharacters[i].Draw(spriteBatch);
186
187 }
188 }
189
190 }
191 /// <summary>
192 /// Add a projectile to the Display.
193 /// </summary>
194 /// <param name="projectile"></param>
195 public void AddProjectiles(Projectile projectile)
196 {
197 mProjectiles.Add(projectile);
198 }
199 }
200 }
201
This page took 0.043943 seconds and 4 git commands to generate.