]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Human.cs
Smooth player movement... still has some issues.
[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 const int moveCoolDown = 15;
22 const int shootCoolDown = 10;
23 //Member Variables
24 State state;
25 String CharName;
26 Map theMap;
27 int movementSpeed;
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 float nextPixelX;
42 float nextPixelY;
43 int movementSteps;
44 int movementCoolDown;
45 float changeX;
46 float changeY;
47
48 bool visible;
49 Display theDisplay;
50
51 //Used to draw projectiles
52 int projectileSpeed;
53 int projectileCoolDown;
54
55
56 public Human(Map _theMap, String Name, Texture2D model, Texture2D projectile, Display mDisplay)
57 {
58 theMap = _theMap;
59 CharName = Name;
60 theDisplay = mDisplay;
61
62 movementSpeed = 12; // randomly set now
63 health = 100;
64 score = 0;
65 visible = false;
66 //default state
67 state = State.up;
68 charModel = model;
69 projectileModel = projectile;
70 projectileSpeed = 30;
71 movementSteps = moveCoolDown -2;
72
73 }
74
75 public void LoadContent(ContentManager contentManager)
76 {
77 charModel = contentManager.Load<Texture2D>("deselectBox"); //change to charModel when designed
78 projectileModel = contentManager.Load<Texture2D>("emptySelectBox"); //change to a projectile model later
79
80 }
81
82 public void UnloadContent()
83 {
84
85 }
86
87 public long Update(GameTime gameTime, NetworkManager networkGame)
88 {
89 return 0;
90
91 }
92
93 public long Draw(SpriteBatch spriteBatch)
94 {
95 if (isMoving && movementSteps > 0)
96 {
97 movementSteps--;
98 pixelX = pixelX + changeX;
99 pixelY = pixelY + changeY;
100 Rectangle position3 = theMap.GetRectangleFromCoordinates(new Vector2(pixelX / Map.PixelsToUnitSquares, pixelY / Map.PixelsToUnitSquares));
101 spriteBatch.Draw(charModel, position3, Color.White);
102 }
103 else
104 {
105 pixelX = gridX * Map.PixelsToUnitSquares;
106 pixelY = gridY * Map.PixelsToUnitSquares;
107 changeX = 0;
108 changeY = 0;
109 isMoving = false;
110 movementSteps = moveCoolDown - 2;
111 spriteBatch.Draw(charModel, theMap.GetRectangleFromCoordinates(gridX, gridY), Color.White);
112 }
113 return 0;
114 }
115
116 public int GridX { get { return gridX; } set { gridX = value; } }
117 public int GridY { get { return gridY; } set { gridY = value; } }
118 public float PixelX { get { return pixelX; } }
119 public float PixelY { get { return pixelY; } }
120 public int Health { get { return health; } }
121 public bool IsMoving { get { return isMoving; } }
122 public int Score { get { return score; } }
123 public bool alive { get { return health > 0; } }
124
125 public void causeDamageTo(int amount)
126 {
127 health -= amount;
128 }
129
130 /// <summary>
131 /// Moves the current player being controlled based on a given set of key presses.
132 /// The player can only move one grid space per movePlayer call. Thus this method
133 /// is made to be called ever update. The player will only move if the grid space
134 /// that is being moved to is an open space.
135 /// </summary>
136 /// <param name="keysPressed">A general list of keys that are pressed. Other keys can be included but only direction keys will be used</param>
137 public void MovePlayer(List<Keys> keysPressed)
138 {
139 if(movementCoolDown > 0)
140 movementCoolDown--;
141 else if (movementCoolDown == 0)
142 {
143 // move upleft
144 keysPressed.Contains<Keys>(Keys.Left);
145 if (keysPressed.Contains<Keys>(Keys.Up) && keysPressed.Contains<Keys>(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY - 1))
146 {
147 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
148 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
149 gridX -= 1;
150 gridY -= 1;
151 movementCoolDown = moveCoolDown;
152 isMoving = true;
153 nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);
154 nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);
155
156 }
157 // move upright
158 else if (keysPressed.Contains<Keys>(Keys.Up) && keysPressed.Contains<Keys>(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY - 1))
159 {
160 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
161 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
162 gridX += 1;
163 gridY -= 1;
164 movementCoolDown = moveCoolDown;
165 isMoving = true;
166 nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);
167 nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);
168
169 }
170 // move downleft
171 else if (keysPressed.Contains<Keys>(Keys.Down) && keysPressed.Contains<Keys>(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY + 1))
172 {
173 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
174 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
175 gridX -= 1;
176 gridY += 1;
177 movementCoolDown = moveCoolDown;
178 isMoving = true;
179 nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);
180 nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);
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 nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);
192 nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);
193 }
194 // move up
195 else if (keysPressed.Contains<Keys>(Keys.Up) && theMap.IsCellOpen(gridX, gridY - 1))
196 {
197 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
198 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
199 state = State.up;
200 gridY -= 1;
201 movementCoolDown = moveCoolDown;
202 isMoving = true;
203 nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);
204 nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);
205 changeY = (float)(-Map.PixelsToUnitSquares / moveCoolDown);
206 }
207 // move down
208 else if (keysPressed.Contains<Keys>(Keys.Down) && theMap.IsCellOpen(gridX, gridY + 1))
209 {
210 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
211 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
212 state = State.down;
213 gridY += 1;
214 movementCoolDown = moveCoolDown;
215 isMoving = true;
216 nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);
217 nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);
218 changeY = (float)(Map.PixelsToUnitSquares / moveCoolDown);
219 }
220 // move left
221 else if (keysPressed.Contains<Keys>(Keys.Left) && theMap.IsCellOpen(gridX - 1, gridY))
222 {
223 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
224 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
225 state = State.left;
226 gridX -= 1;
227 movementCoolDown = moveCoolDown;
228 isMoving = true;
229 nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);
230 nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);
231 changeX = (float)(-Map.PixelsToUnitSquares / moveCoolDown);
232 }
233 // move right
234 else if (keysPressed.Contains<Keys>(Keys.Right) && theMap.IsCellOpen(gridX + 1, gridY))
235 {
236 pixelX = (float)(gridX * Map.PixelsToUnitSquares);
237 pixelY = (float)(gridY * Map.PixelsToUnitSquares);
238 state = State.right;
239 gridX += 1;
240 movementCoolDown = moveCoolDown;
241 isMoving = true;
242 nextPixelX = (float)(gridX * Map.PixelsToUnitSquares);
243 nextPixelY = (float)(gridY * Map.PixelsToUnitSquares);
244 changeX = (float)(Map.PixelsToUnitSquares / moveCoolDown);
245 }
246 }
247 if (projectileCoolDown > 0)
248 projectileCoolDown--;
249 else if (projectileCoolDown == 0)
250 {
251 if (keysPressed.Contains<Keys>(Keys.Space))
252 {
253 //TODO spawn projectile... needs to be added to display though
254 if (state == State.up)
255 {
256 projectileCoolDown = shootCoolDown;
257 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(0, -projectileSpeed), GridX, GridY - 1));
258 }
259 if (state == State.down)
260 {
261 projectileCoolDown = shootCoolDown;
262 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(0, projectileSpeed), GridX, GridY + 1));
263 }
264 if (state == State.right)
265 {
266 projectileCoolDown = shootCoolDown;
267 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(projectileSpeed, 0), GridX + 1, GridY));
268 }
269 if (state == State.left)
270 {
271 projectileCoolDown = shootCoolDown;
272 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel, new Vector2(-projectileSpeed, 0), GridX - 1, GridY));
273 }
274 }
275 }
276 }
277
278
279 public void powerUp(int amount)
280 {
281 health += amount;
282 }
283
284 public void Spawn(Vector2 spawn)
285 {
286 gridX = (int)spawn.X;
287 gridY = (int)spawn.Y;
288 visible = true;
289 }
290
291
292 }
293 }
This page took 0.049457 seconds and 5 git commands to generate.