]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Human.cs
git-svn-id: https://bd85.net/svn/cs3505_group@112 92bb83a3-7c8f-8a45-bc97-515c4e399668
[chaz/carfire] / CarFire / CarFire / CarFire / Human.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Content;
7 using Microsoft.Xna.Framework.Graphics;
8 using Microsoft.Xna.Framework.Input;
9
10 namespace CarFire
11 {
12 public class Human : IPlayer
13 {
14 public enum State
15 {
16 left,
17 right,
18 up,
19 down
20 };
21 //The number of frames between each square movements
22 const int moveCoolDown = 15;
23 //The number of frames between each projectile is spawned.
24 const int shootCoolDown = 10;
25 State state;
26 String CharName;
27 Map theMap;
28 int gridX;
29 int gridY;
30 Texture2D charModel;
31 Texture2D projectileModel;
32 int health;
33 int damage;
34 int range;
35 int score;
36
37 //Used to smooth animations
38 bool isMoving;
39 float pixelX;
40 float pixelY;
41 int movementSteps;
42 int movementCoolDown;
43 float changeX;
44 float changeY;
45
46 bool visible;
47 Display theDisplay;
48
49 //Used to draw projectiles
50 int projectileSpeed;
51 int projectileCoolDown;
52
53
54 public Human(Map _theMap, String Name, Texture2D model, Texture2D projectile, Display mDisplay)
55 {
56 theMap = _theMap;
57 CharName = Name;
58 theDisplay = mDisplay;
59 health = 100;
60 score = 0;
61 visible = false;
62 //default state
63 state = State.up;
64 charModel = model;
65 projectileModel = projectile;
66 projectileSpeed = 30;
67 //The number of animation steps between each square movement.
68 movementSteps = moveCoolDown -2;
69
70 }
71
72 public void LoadContent(ContentManager contentManager)
73 {
74 charModel = contentManager.Load<Texture2D>("deselectBox"); //change to charModel when designed
75 projectileModel = contentManager.Load<Texture2D>("emptySelectBox"); //change to a projectile model later
76
77 }
78
79 public void UnloadContent()
80 {
81
82 }
83
84 public long Update(GameTime gameTime, NetworkManager networkGame)
85 {
86 return 0;
87
88 }
89 /// <summary>
90 /// This method will draw a character to the screen.
91 /// </summary>
92 /// <param name="spriteBatch"></param>
93 /// <returns></returns>
94 public long Draw(SpriteBatch spriteBatch)
95 {
96 //If the sprite is moving there are still movement animations to do.
97 if (isMoving && movementSteps > 0)
98 {
99 movementSteps--;
100 pixelX = pixelX + changeX;
101 pixelY = pixelY + changeY;
102 Rectangle position3 = theMap.GetRectangleFromCoordinates(new Vector2(pixelX / Map.PixelsToUnitSquares, pixelY / Map.PixelsToUnitSquares));
103 spriteBatch.Draw(charModel, position3, Color.White);
104 }
105 // If the sprite is not moving then just draw it.
106 else
107 {
108 pixelX = gridX * Map.PixelsToUnitSquares;
109 pixelY = gridY * Map.PixelsToUnitSquares;
110 changeX = 0;
111 changeY = 0;
112 isMoving = false;
113 movementSteps = moveCoolDown - 2;
114 spriteBatch.Draw(charModel, theMap.GetRectangleFromCoordinates(gridX, gridY), Color.White);
115 }
116 return 0;
117 }
118
119 public int GridX { get { return gridX; } set { gridX = value; } }
120 public int GridY { get { return gridY; } set { gridY = value; } }
121 public float PixelX { get { return pixelX; } }
122 public float PixelY { get { return pixelY; } }
123 public int Health { get { return health; } }
124 public bool IsMoving { get { return isMoving; } }
125 public int Score { get { return score; } }
126 public bool alive { get { return health > 0; } }
127
128 public void causeDamageTo(int amount)
129 {
130 health -= amount;
131 }
132
133 /// <summary>
134 /// Moves the current player being controlled based on a given set of key presses.
135 /// The player can only move one grid space per movePlayer call. Thus this method
136 /// is made to be called ever update. The player will only move if the grid space
137 /// that is being moved to is an open space.
138 /// </summary>
139 /// <param name="keysPressed">A general list of keys that are pressed. Other keys can be included but only direction keys will be used</param>
140 public void MovePlayer(List<Keys> keysPressed)
141 {
142 if(movementCoolDown > 0)
143 movementCoolDown--;
144 else if (movementCoolDown == 0)
145 {
146 // move upleft
147 keysPressed.Contains<Keys>(Keys.Left);
148 if (keysPressed.Contains<Keys>(Keys.Up) && keysPressed.Contains<Keys>(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY - 1))
149 {
150 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
151 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
152 gridX -= 1;
153 gridY -= 1;
154 movementCoolDown = moveCoolDown;
155 isMoving = true;
156
157
158 }
159 // move upright
160 else if (keysPressed.Contains<Keys>(Keys.Up) && keysPressed.Contains<Keys>(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY - 1))
161 {
162 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
163 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
164 gridX += 1;
165 gridY -= 1;
166 movementCoolDown = moveCoolDown;
167 isMoving = true;
168
169
170 }
171 // move downleft
172 else if (keysPressed.Contains<Keys>(Keys.Down) && keysPressed.Contains<Keys>(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY + 1))
173 {
174 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
175 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
176 gridX -= 1;
177 gridY += 1;
178 movementCoolDown = moveCoolDown;
179 isMoving = true;
180
181 }
182 // move downright
183 else if (keysPressed.Contains<Keys>(Keys.Down) && keysPressed.Contains<Keys>(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY + 1))
184 {
185 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
186 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
187 gridX += 1;
188 gridY += 1;
189 movementCoolDown = moveCoolDown;
190 isMoving = true;
191
192 }
193 // move up
194 else if (keysPressed.Contains<Keys>(Keys.Up) && theMap.IsCellOpen(gridX, gridY - 1))
195 {
196 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
197 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
198 state = State.up;
199 gridY -= 1;
200 movementCoolDown = moveCoolDown;
201 isMoving = true;
202 changeY = (float)(-Map.PixelsToUnitSquares / moveCoolDown);
203 }
204 // move down
205 else if (keysPressed.Contains<Keys>(Keys.Down) && theMap.IsCellOpen(gridX, gridY + 1))
206 {
207 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
208 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
209 state = State.down;
210 gridY += 1;
211 movementCoolDown = moveCoolDown;
212 isMoving = true;
213 changeY = (float)(Map.PixelsToUnitSquares / moveCoolDown);
214 }
215 // move left
216 else if (keysPressed.Contains<Keys>(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY))
217 {
218 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
219 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
220 state = State.left;
221 gridX -= 1;
222 movementCoolDown = moveCoolDown;
223 isMoving = true;
224 changeX = (float)(-Map.PixelsToUnitSquares / moveCoolDown);
225 }
226 // move right
227 else if (keysPressed.Contains<Keys>(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY))
228 {
229 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
230 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
231 state = State.right;
232 gridX += 1;
233 movementCoolDown = moveCoolDown;
234 isMoving = true;
235 changeX = (float)(Map.PixelsToUnitSquares / moveCoolDown);
236 }
237 }
238 if (projectileCoolDown > 0)
239 projectileCoolDown--;
240 else if (projectileCoolDown == 0)
241 {
242 if (keysPressed.Contains<Keys>(Keys.Space))
243 {
244 //TODO spawn projectile... needs to be added to display though
245 if (state == State.up)
246 {
247 projectileCoolDown = shootCoolDown;
248 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(0, -projectileSpeed), GridX, GridY - 1));
249 }
250 if (state == State.down)
251 {
252 projectileCoolDown = shootCoolDown;
253 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(0, projectileSpeed), GridX, GridY + 1));
254 }
255 if (state == State.right)
256 {
257 projectileCoolDown = shootCoolDown;
258 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(projectileSpeed, 0), GridX + 1, GridY));
259 }
260 if (state == State.left)
261 {
262 projectileCoolDown = shootCoolDown;
263 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(-projectileSpeed, 0), GridX - 1, GridY));
264 }
265 }
266 }
267 }
268
269
270 public void powerUp(int amount)
271 {
272 health += amount;
273 }
274
275 public void Spawn(Vector2 spawn)
276 {
277 gridX = (int)spawn.X;
278 gridY = (int)spawn.Y;
279 visible = true;
280 }
281
282
283 }
284 }
This page took 0.044139 seconds and 5 git commands to generate.