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