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