]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Human.cs
Player can now stop and 'aim' by pressing left control.
[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 projectile is spawned.
22 const int shootCoolDown = 10;
23 State state;
24 String CharName;
25 Map theMap;
26 Texture2D charModel;
27 Texture2D projectileModel;
28 int health;
29 int damage;
30 int range;
31 int score;
32
33 MovementManager mMotion;
34
35 bool visible;
36 Display theDisplay;
37
38 //Used to draw projectiles
39 int projectileSpeed;
40 int projectileCoolDown;
41
42
43 public Human(Map _theMap, String Name, Texture2D model, Texture2D projectile, Display mDisplay, Point position)
44 {
45 theMap = _theMap;
46 CharName = Name;
47 theDisplay = mDisplay;
48 health = 100;
49 score = 0;
50 visible = false;
51 //default state
52 state = State.up;
53 charModel = model;
54 projectileModel = projectile;
55 projectileSpeed = 30;
56
57 // Speed is the number of grid cells you can move through per second.
58 mMotion = new MovementManager(position, 5.0f);
59 }
60
61 public void LoadContent(ContentManager contentManager)
62 {
63 charModel = contentManager.Load<Texture2D>("deselectBox"); //change to charModel when designed
64 projectileModel = contentManager.Load<Texture2D>("emptySelectBox"); //change to a projectile model later
65
66 }
67
68 public void UnloadContent()
69 {
70
71 }
72
73 public long Update(GameTime gameTime, NetworkManager networkGame)
74 {
75 return 0;
76
77 }
78 /// <summary>
79 /// This method will draw a character to the screen.
80 /// </summary>
81 /// <param name="spriteBatch"></param>
82 /// <returns></returns>
83 public long Draw(SpriteBatch spriteBatch)
84 {
85 Rectangle position = theMap.GetRectangleFromCoordinates(mMotion.Position);
86 spriteBatch.Draw(charModel, position, Color.White);
87 return 0;
88 }
89
90 public int Health { get { return health; } }
91 public int Score { get { return score; } }
92 public bool alive { get { return health > 0; } }
93
94 public Vector2 Position { get { return mMotion.Position; } }
95 public Point Coordinates { get { return mMotion.Coordinates; } }
96
97 public void causeDamageTo(int amount)
98 {
99 health -= amount;
100 }
101
102 /// <summary>
103 /// Moves the current player being controlled based on a given set of key presses.
104 /// The player can only move one grid space per movePlayer call. Thus this method
105 /// is made to be called ever update. The player will only move if the grid space
106 /// that is being moved to is an open space.
107 /// </summary>
108 /// <param name="keysPressed">A general list of keys that are pressed. Other keys can be included but only direction keys will be used</param>
109 public void MovePlayer(TimeSpan timeSpan, List<Keys> keysPressed)
110 {
111 bool moveLeft = keysPressed.Contains(Keys.Left);
112 bool moveRight = keysPressed.Contains(Keys.Right);
113 bool moveUp = keysPressed.Contains(Keys.Up);
114 bool moveDown = keysPressed.Contains(Keys.Down);
115 if (moveLeft)
116 state = State.left;
117 else if (moveRight)
118 state = State.right;
119 else if (moveUp)
120 state = State.up;
121 else if (moveDown)
122 state = State.down;
123
124 Point destination = MovementManager.GetNeighborCell(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown);
125 if (!keysPressed.Contains(Keys.LeftControl))
126 {
127 if (theMap.IsCellOpen(destination))
128 {
129 mMotion.Update(timeSpan, moveLeft, moveRight, moveUp, moveDown);
130 }
131 else
132 {
133 mMotion.Update(timeSpan);
134 }
135 }
136 else
137 {
138 mMotion.LockUpdate(timeSpan, moveLeft, moveRight, moveUp, moveDown);
139 }
140
141
142 if (projectileCoolDown > 0)
143 projectileCoolDown--;
144 else if (projectileCoolDown == 0)
145 {
146 if (keysPressed.Contains<Keys>(Keys.Space))
147 {
148 float velocityX = 0;
149 float velocityY = 0;
150 int startX = Coordinates.X;
151 int startY = Coordinates.Y;
152 if (mMotion.Direction == Direction.Down || mMotion.Direction == Direction.LowerLeft || mMotion.Direction == Direction.LowerRight)
153 {
154 velocityY = 1;
155 startY = Coordinates.Y + 1;
156 }
157 else if (mMotion.Direction == Direction.Up || mMotion.Direction == Direction.UpperLeft || mMotion.Direction == Direction.UpperRight)
158 {
159 velocityY = -1;
160 startY = Coordinates.Y - 1;
161 }
162 if (mMotion.Direction == Direction.Right || mMotion.Direction == Direction.LowerRight || mMotion.Direction == Direction.UpperRight)
163 {
164 velocityX = 1;
165 startX = Coordinates.X + 1;
166 }
167 else if (mMotion.Direction == Direction.Left || mMotion.Direction == Direction.LowerLeft || mMotion.Direction == Direction.UpperLeft)
168 {
169 velocityX = -1;
170 startX = Coordinates.X - 1;
171 }
172 Vector2 toShoot = new Vector2(velocityX, velocityY);
173 toShoot.Normalize();
174 toShoot *= projectileSpeed;
175 projectileCoolDown = shootCoolDown;
176 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
177 toShoot, new Point(startX, startY)));
178
179 /*
180 if (state == State.up)
181 {
182 projectileCoolDown = shootCoolDown;
183 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
184 new Vector2(0, -projectileSpeed), new Point(Coordinates.X, Coordinates.Y - 1)));
185 }
186 if (state == State.down)
187 {
188 projectileCoolDown = shootCoolDown;
189 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
190 new Vector2(0, projectileSpeed), new Point(Coordinates.X, Coordinates.Y + 1)));
191 }
192 if (state == State.right)
193 {
194 projectileCoolDown = shootCoolDown;
195 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
196 new Vector2(projectileSpeed, 0), new Point (Coordinates.X + 1, Coordinates.Y)));
197 }
198 if (state == State.left)
199 {
200 projectileCoolDown = shootCoolDown;
201 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
202 new Vector2(-projectileSpeed, 0), new Point(Coordinates.X - 1, Coordinates.Y)));
203 }
204 */
205 }
206 }
207 }
208
209
210 public void powerUp(int amount)
211 {
212 health += amount;
213 }
214
215 public void Spawn(Vector2 spawn)
216 {
217 //gridX = (int)spawn.X;
218 //gridY = (int)spawn.Y;
219 visible = true;
220 }
221
222
223 }
224 }
This page took 0.041619 seconds and 4 git commands to generate.