]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Human.cs
Smooth movement between cells.
[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 Texture2D charModel;
29 Texture2D projectileModel;
30 int health;
31 int damage;
32 int range;
33 int score;
34
35 MovementManager mMotion;
36
37 bool visible;
38 Display theDisplay;
39
40 //Used to draw projectiles
41 int projectileSpeed;
42 int projectileCoolDown;
43
44
45 public Human(Map _theMap, String Name, Texture2D model, Texture2D projectile, Display mDisplay, Point position)
46 {
47 theMap = _theMap;
48 CharName = Name;
49 theDisplay = mDisplay;
50 health = 100;
51 score = 0;
52 visible = false;
53 //default state
54 state = State.up;
55 charModel = model;
56 projectileModel = projectile;
57 projectileSpeed = 30;
58
59 // Speed is the number of grid cells you can move through per second.
60 mMotion = new MovementManager(position, 5.0f);
61 }
62
63 public void LoadContent(ContentManager contentManager)
64 {
65 charModel = contentManager.Load<Texture2D>("deselectBox"); //change to charModel when designed
66 projectileModel = contentManager.Load<Texture2D>("emptySelectBox"); //change to a projectile model later
67
68 }
69
70 public void UnloadContent()
71 {
72
73 }
74
75 public long Update(GameTime gameTime, NetworkManager networkGame)
76 {
77 return 0;
78
79 }
80 /// <summary>
81 /// This method will draw a character to the screen.
82 /// </summary>
83 /// <param name="spriteBatch"></param>
84 /// <returns></returns>
85 public long Draw(SpriteBatch spriteBatch)
86 {
87 Rectangle position = theMap.GetRectangleFromCoordinates(mMotion.Position);
88 spriteBatch.Draw(charModel, position, Color.White);
89 return 0;
90 }
91
92 public int Health { get { return health; } }
93 public int Score { get { return score; } }
94 public bool alive { get { return health > 0; } }
95
96 public Vector2 Position { get { return mMotion.Position; } }
97 public Point Coordinates { get { return mMotion.Coordinates; } }
98
99 public void causeDamageTo(int amount)
100 {
101 health -= amount;
102 }
103
104 /// <summary>
105 /// Moves the current player being controlled based on a given set of key presses.
106 /// The player can only move one grid space per movePlayer call. Thus this method
107 /// is made to be called ever update. The player will only move if the grid space
108 /// that is being moved to is an open space.
109 /// </summary>
110 /// <param name="keysPressed">A general list of keys that are pressed. Other keys can be included but only direction keys will be used</param>
111 public void MovePlayer(TimeSpan timeSpan, List<Keys> keysPressed)
112 {
113 bool moveLeft = keysPressed.Contains(Keys.Left);
114 bool moveRight = keysPressed.Contains(Keys.Right);
115 bool moveUp = keysPressed.Contains(Keys.Up);
116 bool moveDown = keysPressed.Contains(Keys.Down);
117
118 Point destination = MovementManager.GetNeighborCell(mMotion.Coordinates, moveLeft, moveRight, moveUp, moveDown);
119 if (theMap.IsCellOpen(destination))
120 {
121 mMotion.Update(timeSpan, moveLeft, moveRight, moveUp, moveDown);
122 }
123 else
124 {
125 mMotion.Update(timeSpan);
126 }
127
128
129 if (projectileCoolDown > 0)
130 projectileCoolDown--;
131 else if (projectileCoolDown == 0)
132 {
133 if (keysPressed.Contains<Keys>(Keys.Space))
134 {
135 //TODO spawn projectile... needs to be added to display though
136 if (state == State.up)
137 {
138 projectileCoolDown = shootCoolDown;
139 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
140 new Vector2(0, -projectileSpeed), Coordinates.X, Coordinates.Y - 1));
141 }
142 if (state == State.down)
143 {
144 projectileCoolDown = shootCoolDown;
145 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
146 new Vector2(0, projectileSpeed), Coordinates.X, Coordinates.Y + 1));
147 }
148 if (state == State.right)
149 {
150 projectileCoolDown = shootCoolDown;
151 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
152 new Vector2(projectileSpeed, 0), Coordinates.X + 1, Coordinates.Y));
153 }
154 if (state == State.left)
155 {
156 projectileCoolDown = shootCoolDown;
157 theDisplay.AddProjectiles(new Projectile(theMap, projectileModel,
158 new Vector2(-projectileSpeed, 0), Coordinates.X - 1, Coordinates.Y));
159 }
160 }
161 }
162 }
163
164
165 public void powerUp(int amount)
166 {
167 health += amount;
168 }
169
170 public void Spawn(Vector2 spawn)
171 {
172 //gridX = (int)spawn.X;
173 //gridY = (int)spawn.Y;
174 visible = true;
175 }
176
177
178 }
179 }
This page took 0.040627 seconds and 5 git commands to generate.