-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cs
261 lines (200 loc) · 6.74 KB
/
Player.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
namespace _3DAsteroids
{
class Player
{
public Vector3 position;
public Quaternion rotation;
public Model model;
public float upDownRot;
public float leftRightRot;
public Vector3 forwardThrust;
public Bullet[] bulletSupply; //reuse bullets!
int currentBullet; //the bullet that will be fired next from the pool
float timer; //time to shoot again
private int outLimit;
private SoundEffect missileSound;
public float fuel;
public bool multiBullet;
public float multiTimer;
public bool doubleSpeed;
public float doubleTimer;
//public BoundingBox bounds;
public Player()
{
position = Vector3.Zero;
rotation = Quaternion.Identity;
upDownRot = 0;
leftRightRot = 0;
forwardThrust = Vector3.Zero;
bulletSupply = new Bullet[20];
for (int i = 0; i < bulletSupply.Length; i++)
{
bulletSupply[i] = new Bullet();
}
currentBullet = 0;
timer = 0;
fuel = 100;
multiBullet = false;
doubleSpeed = false;
}
public void updateTimer(float timeElapsed)
{
timer += timeElapsed;
fuel -= timeElapsed /2;
if (multiBullet) multiTimer += timeElapsed;
if (doubleSpeed) doubleTimer += timeElapsed;
if (multiTimer > 10)
{
multiBullet = false;
multiTimer = 0;
}
if (doubleTimer > 10)
{
doubleSpeed = false;
doubleTimer = 0;
}
}
public bool canShoot()
{
return timer >= Constants.SHOOTTIMER;
}
public void fireBullet(Vector3 pos, Quaternion rot)
{
timer = 0;
missileSound.Play();
if (multiBullet)
{
Vector3 up = Vector3.Transform(new Vector3(0, -1, 0), rotation);
// Quaternion bullet1 = Quaternion.CreateFromYawPitchRoll(forward.X, forward.Y, forward.Z);
Quaternion bullet1 = rot + Quaternion.CreateFromAxisAngle(up, 2);
//bulletSupply[currentBullet].setBullet(pos, rot + Quaternion.CreateFromAxisAngle(up, 2));
// currentBullet++;
//currentBullet %= (bulletSupply.Length);
bulletSupply[currentBullet].setBullet(pos, rot, 1);
currentBullet++;
currentBullet %= (bulletSupply.Length);
bulletSupply[currentBullet].setBullet(pos, rot, 2);
currentBullet++;
currentBullet %= (bulletSupply.Length);
}
bulletSupply[currentBullet].setBullet(pos, rot, 0);
currentBullet++;
currentBullet %= (bulletSupply.Length);
}
public void setSound(SoundEffect sound)
{
missileSound = sound;
}
public void setBounds(int limit)
{
outLimit = limit;
for (int i = 0; i < bulletSupply.Length; i++)
{
bulletSupply[i].setBounds(outLimit);
}
}
public void updateBounds()
{
/*BoundingBox all = new BoundingBox();
int count = 0;
foreach (ModelMesh m in model.Meshes)
{
System.Diagnostics.Debug.Write("" + count + "\n");
BoundingBox.CreateMerged(all, BoundingBox.CreateFromSphere(m.BoundingSphere));
count++;
}
bounds = all;*/
//bounds = BoundingBox.CreateFromSphere(model.Meshes[0].BoundingSphere);
}
public void playerFlight()
{
leftRightRot = 0;
upDownRot = 0;
float rotationSpeed = 0.025F;
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
leftRightRot += rotationSpeed;
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
leftRightRot -= rotationSpeed;
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
upDownRot += rotationSpeed;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
upDownRot -= rotationSpeed;
}
if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
{
Vector3 increase = Vector3.Transform(new Vector3(0, 0, -0.1F), rotation);
forwardThrust += increase;
if (forwardThrust.LengthSquared() >= Constants.MAXSPEED * Constants.MAXSPEED)
{
forwardThrust.Normalize();
forwardThrust *= Constants.MAXSPEED;
}
//position = Vector3.Transform(position, Matrix.CreateTranslation(0, 0, 0.1F));
}
if (Keyboard.GetState().IsKeyDown(Keys.Space))
{
//Create Bullet
if (canShoot())
{
fireBullet(position, rotation);
}
}
//update position based off of all of the rotating done above
rotation *= Quaternion.CreateFromYawPitchRoll(leftRightRot, upDownRot, 0);
if (checkOutOfBounds())
{
//bounce if going out of bounds
forwardThrust *= -1;
}
//PROGRESS NEVER STOPS, ALWAYS MOVE FORWARD
if (doubleSpeed) position += forwardThrust*2;
else position += forwardThrust;
//cool the engines bro
forwardThrust *= .99F;
}
bool checkOutOfBounds()
{
if (position.X > outLimit)
{
return true;
}
if (position.X < -outLimit)
{
return true;
}
if (position.Y > outLimit)
{
return true;
}
if (position.Y < -outLimit)
{
return true;
}
if (position.Z > outLimit)
{
return true;
}
if (position.Z < -outLimit)
{
return true;
}
return false;
}
public void addFuel(float fuel)
{
this.fuel += fuel;
if (this.fuel > 100) this.fuel = 100;
}
}
}