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