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