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