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