]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Human.cs
Display should now follow correct player in a network game(Not Tested Yet!)
[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 Game game;
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(Game theGame, String Name, Texture2D model, Texture2D projectile, Display mDisplay, Point position)
44 {
45 game = theGame;
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, 8.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
69 public void Update(TimeSpan timeSpan)
70 {
71 }
72 /// <summary>
73 /// This method will draw a character to the screen.
74 /// </summary>
75 /// <param name="spriteBatch"></param>
76 public void Draw(SpriteBatch spriteBatch)
77 {
78 Rectangle position = game.State.Map.GetRectangleFromCoordinates(mMotion.Position);
79 spriteBatch.Draw(charModel, position, Color.White);
80 }
81
82 public int Health { get { return health; } }
83 public int Score { get { return score; } }
84 public bool alive { get { return health > 0; } }
85
86 public Vector2 Position { get { return mMotion.Position; } }
87 public Point Coordinates { get { return mMotion.Coordinates; } }
88
89 public void causeDamageTo(int amount)
90 {
91 health -= amount;
92 }
93
94 /// <summary>
95 /// Moves the current player being controlled based on a given set of key presses.
96 /// The player can only move one grid space per movePlayer call. Thus this method
97 /// is made to be called ever update. The player will only move if the grid space
98 /// that is being moved to is an open space.
99 /// </summary>
100 /// <param name="keysPressed">A general list of keys that are pressed. Other keys can be included but only direction keys will be used</param>
101 public void MovePlayer(TimeSpan timeSpan, List<Keys> keysPressed)
102 {
103 bool moveLeft = keysPressed.Contains(Keys.Left);
104 bool moveRight = keysPressed.Contains(Keys.Right);
105 bool moveUp = keysPressed.Contains(Keys.Up);
106 bool moveDown = keysPressed.Contains(Keys.Down);
107 if (moveLeft)
108 state = State.left;
109 else if (moveRight)
110 state = State.right;
111 else if (moveUp)
112 state = State.up;
113 else if (moveDown)
114 state = State.down;
115
116 Point destination = MovementManager.GetNeighborCell(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown);
117 if (!keysPressed.Contains(Keys.LeftControl))
118 {
119 if (game.IsCellOpen(destination))
120 {
121 mMotion.Update(timeSpan, moveLeft, moveRight, moveUp, moveDown);
122 }
123 else
124 {
125 mMotion.Update(timeSpan);
126 }
127 }
128 else
129 {
130 mMotion.LockUpdate(timeSpan, moveLeft, moveRight, moveUp, moveDown);
131 }
132
133
134 if (projectileCoolDown > 0)
135 projectileCoolDown--;
136 else if (projectileCoolDown == 0)
137 {
138 if (keysPressed.Contains<Keys>(Keys.Space))
139 {
140 float velocityX = 0;
141 float velocityY = 0;
142 int startX = Coordinates.X;
143 int startY = Coordinates.Y;
144 if (mMotion.Direction == Direction.Down || mMotion.Direction == Direction.LowerLeft || mMotion.Direction == Direction.LowerRight)
145 {
146 velocityY = 1;
147 startY = Coordinates.Y + 1;
148 }
149 else if (mMotion.Direction == Direction.Up || mMotion.Direction == Direction.UpperLeft || mMotion.Direction == Direction.UpperRight)
150 {
151 velocityY = -1;
152 startY = Coordinates.Y - 1;
153 }
154 if (mMotion.Direction == Direction.Right || mMotion.Direction == Direction.LowerRight || mMotion.Direction == Direction.UpperRight)
155 {
156 velocityX = 1;
157 startX = Coordinates.X + 1;
158 }
159 else if (mMotion.Direction == Direction.Left || mMotion.Direction == Direction.LowerLeft || mMotion.Direction == Direction.UpperLeft)
160 {
161 velocityX = -1;
162 startX = Coordinates.X - 1;
163 }
164 Vector2 toShoot = new Vector2(velocityX, velocityY);
165 toShoot.Normalize();
166 toShoot *= projectileSpeed;
167 projectileCoolDown = shootCoolDown;
168 theDisplay.AddProjectiles(new Projectile(game.State.Map, projectileModel,
169 toShoot, new Point(startX, startY)));
170
171 /*
172 if (state == State.up)
173 {
174 projectileCoolDown = shootCoolDown;
175 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
176 new Vector2(0, -projectileSpeed), new Point(Coordinates.X, Coordinates.Y - 1)));
177 }
178 if (state == State.down)
179 {
180 projectileCoolDown = shootCoolDown;
181 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
182 new Vector2(0, projectileSpeed), new Point(Coordinates.X, Coordinates.Y + 1)));
183 }
184 if (state == State.right)
185 {
186 projectileCoolDown = shootCoolDown;
187 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
188 new Vector2(projectileSpeed, 0), new Point (Coordinates.X + 1, Coordinates.Y)));
189 }
190 if (state == State.left)
191 {
192 projectileCoolDown = shootCoolDown;
193 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
194 new Vector2(-projectileSpeed, 0), new Point(Coordinates.X - 1, Coordinates.Y)));
195 }
196 */
197 }
198 }
199 }
200
201
202 public void powerUp(int amount)
203 {
204 health += amount;
205 }
206
207 public void Spawn(Vector2 spawn)
208 {
209 //gridX = (int)spawn.X;
210 //gridY = (int)spawn.Y;
211 visible = true;
212 }
213
214
215 }
216 }
This page took 0.040542 seconds and 4 git commands to generate.