-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTank.cs
644 lines (523 loc) · 22.3 KB
/
Tank.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
#endregion
namespace Tanks
{
class Tank
{
#region Variables
private Model tankModel;
private Matrix world = Matrix.Identity;
private BasicEffect effect;
private GraphicsDevice graphicsDevice;
ModelBone leftBackWheelBone;
ModelBone rightBackWheelBone;
ModelBone leftFrontWheelBone;
ModelBone rightFrontWheelBone;
ModelBone leftSteerBone;
ModelBone rightSteerBone;
ModelBone turretBone;
ModelBone cannonBone;
ModelBone hatchBone;
Matrix leftBackWheelTransform;
Matrix rightBackWheelTransform;
Matrix leftFrontWheelTransform;
Matrix rightFrontWheelTransform;
Matrix leftSteerTransform;
Matrix rightSteerTransform;
Matrix turretTransform;
Matrix cannonTransform;
Matrix hatchTransform;
Matrix[] boneTransforms;
float wheelRotationValue;
float steerRotationValue;
float turretRotationValue;
float cannonRotationValue;
float hatchRotationValue;
private BoundingSphere boundingSphere;
private Vector3 position;
private Vector3 previousPosition;
private Vector3 direction;
private Vector3 newDirection;
private Vector3 tankOrientation;
private Vector3 cannonOrientation;
private Vector3 normalNova;
private float yaw = 0.0f;
private bool lastLeft, lastRight, moveLeft, moveRight;
private int width, height;
private bool collided;
private bool nextCollided;
private bool moving = false;
private BoundingSphere nextBoundingSphere;
private bool player2Control = true;
private bool canChangePlayer2Control = true;
private bool turningAround = false;
/*Music / sound related
private SoundEffect hitSound;
*/
#endregion
#region Properties
public bool Collided
{
get { return collided; }
set { collided = value; }
}
public bool Moving
{
get { return moving; }
set { moving = value; }
}
public Vector3 NewDirection
{
get { return newDirection; }
set { newDirection = value; }
}
public Vector3 NormalNova
{
get { return normalNova; }
set { normalNova = value; }
}
public float Yaw
{
get { return yaw; }
set { yaw = value; }
}
public BoundingSphere BoundingSphere
{
get { return boundingSphere; }
set { boundingSphere = value; }
}
public Vector3 Direction
{
get { return direction; }
set { direction = value; }
}
public Vector3 CannonOrientation
{
get { return cannonOrientation; }
set { cannonOrientation = value; }
}
public Vector3 TankOrientation
{
get { return tankOrientation; }
set { tankOrientation = value; }
}
public Vector3 Position
{
get { return position; }
set { position = value; }
}
public float WheelRotationValue
{
get { return wheelRotationValue; }
set { wheelRotationValue = value; }
}
public float SteerRotationValue
{
get { return steerRotationValue; }
set { steerRotationValue = value; }
}
public float TurretRotationValue
{
get { return turretRotationValue; }
set { turretRotationValue = value; }
}
public float CannonRotationValue
{
get { return cannonRotationValue; }
set { cannonRotationValue = value; }
}
public float HatchRotationValue
{
get { return hatchRotationValue; }
set { hatchRotationValue = value; }
}
#endregion
public Tank(GraphicsDevice graphicsDevice, ContentManager contentManager, Map mapa, Vector3 startingPosition)
{
this.graphicsDevice = graphicsDevice;
this.effect = mapa.Effect;
position = startingPosition;
width = mapa.Width;
height = mapa.Height;
boundingSphere = new BoundingSphere();
boundingSphere.Center = position;
boundingSphere.Radius = 1.5f;
tankModel = contentManager.Load<Model>(@"Tank\tank");
/* Music / sound related
hitSound = contentManager.Load<SoundEffect>("hitSound");
*/
// Look up shortcut references to the bones we are going to animate.
leftBackWheelBone = tankModel.Bones["l_back_wheel_geo"];
rightBackWheelBone = tankModel.Bones["r_back_wheel_geo"];
leftFrontWheelBone = tankModel.Bones["l_front_wheel_geo"];
rightFrontWheelBone = tankModel.Bones["r_front_wheel_geo"];
leftSteerBone = tankModel.Bones["l_steer_geo"];
rightSteerBone = tankModel.Bones["r_steer_geo"];
turretBone = tankModel.Bones["turret_geo"];
cannonBone = tankModel.Bones["canon_geo"];
hatchBone = tankModel.Bones["hatch_geo"];
// Store the original transform matrix for each animating bone.
leftBackWheelTransform = leftBackWheelBone.Transform;
rightBackWheelTransform = rightBackWheelBone.Transform;
leftFrontWheelTransform = leftFrontWheelBone.Transform;
rightFrontWheelTransform = rightFrontWheelBone.Transform;
leftSteerTransform = leftSteerBone.Transform;
rightSteerTransform = rightSteerBone.Transform;
turretTransform = turretBone.Transform;
cannonTransform = cannonBone.Transform;
hatchTransform = hatchBone.Transform;
// Allocate the transform matrix array.
boneTransforms = new Matrix[tankModel.Bones.Count];
}
public void Update()
{
moving = false;
direction = Vector3.Transform(new Vector3(0, 0, 1), Matrix.CreateRotationY(yaw));
if (Keyboard.GetState().IsKeyDown(Keys.W))
{
Vector3 nextPosition = position + direction * 0.2f;
nextBoundingSphere = boundingSphere;
nextBoundingSphere.Center = nextPosition;
if (!((nextPosition.X < 2f || nextPosition.X > width - 2f || nextPosition.Z < 2f || nextPosition.Z > height - 2f) ||
(nextPosition.X < 2f && nextPosition.Z < 2f) || (nextPosition.X > width - 2f && nextPosition.Z < 2f) ||
(nextPosition.X > width - 2f && nextPosition.Z > height - 2f) || (nextPosition.X < 2f && nextPosition.Z > height - 2f))
&& !nextCollided)
{
position += direction * 0.1f;
}
wheelRotationValue += 0.1f;
moving = true;
}
else if (Keyboard.GetState().IsKeyDown(Keys.S))
{
Vector3 nextPosition = position - direction * 0.2f;
nextBoundingSphere = boundingSphere;
nextBoundingSphere.Center = nextPosition;
if (!((nextPosition.X < 2f || nextPosition.X > width - 2f || nextPosition.Z < 2f || nextPosition.Z > height - 2f) ||
(nextPosition.X < 2f && nextPosition.Z < 2f) || (nextPosition.X > width - 2f && nextPosition.Z < 2f) ||
(nextPosition.X > width - 2f && nextPosition.Z > height - 2f) || (nextPosition.X < 2f && nextPosition.Z > height - 2f))
&& !nextCollided)
{
position -= direction * 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.A) || Keyboard.GetState().IsKeyDown(Keys.D))
wheelRotationValue -= 0.2f;
else
wheelRotationValue -= 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.A) && lastLeft && Keyboard.GetState().IsKeyDown(Keys.D) && !lastRight)
{
moveLeft = false;
moveRight = true;
}
else if (Keyboard.GetState().IsKeyDown(Keys.A) && !lastLeft && Keyboard.GetState().IsKeyDown(Keys.D) && lastRight)
{
moveLeft = true;
moveRight = false;
}
else if (Keyboard.GetState().IsKeyDown(Keys.A) && !lastLeft && !Keyboard.GetState().IsKeyDown(Keys.D))
{
moveLeft = true;
moveRight = false;
}
else if (!Keyboard.GetState().IsKeyDown(Keys.A) && Keyboard.GetState().IsKeyDown(Keys.D) && !lastRight)
{
moveLeft = false;
moveRight = true;
}
if(Keyboard.GetState().IsKeyDown(Keys.A) && Keyboard.GetState().IsKeyUp(Keys.D))
{
moveLeft = true;
moveRight = false;
}
else if (Keyboard.GetState().IsKeyUp(Keys.A) && Keyboard.GetState().IsKeyDown(Keys.D))
{
moveLeft = false;
moveRight = true;
}
else if (Keyboard.GetState().IsKeyUp(Keys.A) && Keyboard.GetState().IsKeyUp(Keys.D))
{
moveLeft = false;
moveRight = false;
}
if(Keyboard.GetState().IsKeyDown(Keys.I))
{
if(cannonRotationValue > -1)
cannonRotationValue -= 0.04f;
}
else if (Keyboard.GetState().IsKeyDown(Keys.K))
{
if (cannonRotationValue < 0)
cannonRotationValue += 0.04f;
}
if (Keyboard.GetState().IsKeyDown(Keys.J))
{
turretRotationValue += 0.04f;
}
else if (Keyboard.GetState().IsKeyDown(Keys.L))
{
turretRotationValue -= 0.04f;
}
previousPosition = position;
if(moveLeft)
{
yaw += 0.05f;
wheelRotationValue += 0.1f;
if (steerRotationValue < 1f)
steerRotationValue += 0.05f;
moving = true;
}
if(moveRight)
{
yaw -= 0.05f;
wheelRotationValue += 0.1f;
if (steerRotationValue > -1f)
steerRotationValue -= 0.05f;
moving = true;
}
if(!moveRight && !moveLeft)
{
if (steerRotationValue > 0)
steerRotationValue -= 0.05f;
if (steerRotationValue < 0)
steerRotationValue += 0.05f;
}
lastLeft = Keyboard.GetState().IsKeyDown(Keys.A);
lastRight = Keyboard.GetState().IsKeyDown(Keys.D);
boundingSphere.Center = position;
}
public void UpdatePlayer2(Tank tankP1)
{
moving = false;
direction = Vector3.Transform(new Vector3(0, 0, 1), Matrix.CreateRotationY(yaw));
if (Keyboard.GetState().IsKeyDown(Keys.Enter) && canChangePlayer2Control)
{
canChangePlayer2Control = false;
if (player2Control)
player2Control = false;
else
player2Control = true;
}
else if (Keyboard.GetState().IsKeyUp(Keys.Enter))
canChangePlayer2Control = true;
if (player2Control)
{
if (Keyboard.GetState().IsKeyDown(Keys.NumPad8))
{
Vector3 nextPosition = position + direction * 0.2f;
nextBoundingSphere = boundingSphere;
nextBoundingSphere.Center = nextPosition;
if (!((nextPosition.X < 2f || nextPosition.X > width - 2f || nextPosition.Z < 2f || nextPosition.Z > height - 2f) ||
(nextPosition.X < 2f && nextPosition.Z < 2f) || (nextPosition.X > width - 2f && nextPosition.Z < 2f) ||
(nextPosition.X > width - 2f && nextPosition.Z > height - 2f) || (nextPosition.X < 2f && nextPosition.Z > height - 2f))
&& !nextCollided)
{
position += direction * 0.1f;
}
wheelRotationValue += 0.1f;
moving = true;
}
else if (Keyboard.GetState().IsKeyDown(Keys.NumPad2))
{
Vector3 nextPosition = position - direction * 0.2f;
nextBoundingSphere = boundingSphere;
nextBoundingSphere.Center = nextPosition;
if (!((nextPosition.X < 2f || nextPosition.X > width - 2f || nextPosition.Z < 2f || nextPosition.Z > height - 2f) ||
(nextPosition.X < 2f && nextPosition.Z < 2f) || (nextPosition.X > width - 2f && nextPosition.Z < 2f) ||
(nextPosition.X > width - 2f && nextPosition.Z > height - 2f) || (nextPosition.X < 2f && nextPosition.Z > height - 2f))
&& !nextCollided)
{
position -= direction * 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.NumPad4) || Keyboard.GetState().IsKeyDown(Keys.NumPad6))
wheelRotationValue -= 0.2f;
else
wheelRotationValue -= 0.1f;
}
if (Keyboard.GetState().IsKeyDown(Keys.NumPad4) && lastLeft && Keyboard.GetState().IsKeyDown(Keys.NumPad6) && !lastRight)
{
moveLeft = false;
moveRight = true;
}
else if (Keyboard.GetState().IsKeyDown(Keys.NumPad4) && !lastLeft && Keyboard.GetState().IsKeyDown(Keys.NumPad6) && lastRight)
{
moveLeft = true;
moveRight = false;
}
else if (Keyboard.GetState().IsKeyDown(Keys.NumPad4) && !lastLeft && !Keyboard.GetState().IsKeyDown(Keys.NumPad6))
{
moveLeft = true;
moveRight = false;
}
else if (!Keyboard.GetState().IsKeyDown(Keys.NumPad4) && Keyboard.GetState().IsKeyDown(Keys.NumPad6) && !lastRight)
{
moveLeft = false;
moveRight = true;
}
if (Keyboard.GetState().IsKeyDown(Keys.NumPad4) && Keyboard.GetState().IsKeyUp(Keys.NumPad6))
{
moveLeft = true;
moveRight = false;
}
else if (Keyboard.GetState().IsKeyUp(Keys.NumPad4) && Keyboard.GetState().IsKeyDown(Keys.NumPad6))
{
moveLeft = false;
moveRight = true;
}
else if (Keyboard.GetState().IsKeyUp(Keys.NumPad4) && Keyboard.GetState().IsKeyUp(Keys.NumPad6))
{
moveLeft = false;
moveRight = false;
}
if (Keyboard.GetState().IsKeyDown(Keys.NumPad7))
{
if (cannonRotationValue > -1)
cannonRotationValue -= 0.04f;
}
else if (Keyboard.GetState().IsKeyDown(Keys.NumPad9))
{
if (cannonRotationValue < 0)
cannonRotationValue += 0.04f;
}
if (Keyboard.GetState().IsKeyDown(Keys.NumPad1))
{
turretRotationValue += 0.04f;
}
else if (Keyboard.GetState().IsKeyDown(Keys.NumPad3))
{
turretRotationValue -= 0.04f;
}
previousPosition = position;
if (moveLeft)
{
yaw += 0.05f;
wheelRotationValue += 0.1f;
if (steerRotationValue < 1f)
steerRotationValue += 0.05f;
moving = true;
}
if (moveRight)
{
yaw -= 0.05f;
wheelRotationValue += 0.1f;
if (steerRotationValue > -1f)
steerRotationValue -= 0.05f;
moving = true;
}
if (!moveRight && !moveLeft)
{
if (steerRotationValue > 0)
steerRotationValue -= 0.05f;
if (steerRotationValue < 0)
steerRotationValue += 0.05f;
}
lastLeft = Keyboard.GetState().IsKeyDown(Keys.NumPad4);
lastRight = Keyboard.GetState().IsKeyDown(Keys.NumPad6);
}
else
{
if (Vector3.Distance(position, tankP1.position) < 20f)
{
moving = true;
Vector3 newDirection = position - tankP1.position;
newDirection.Normalize();
direction = Vector3.Transform(new Vector3(0, 0, 1), Matrix.CreateRotationY(yaw));
direction.Normalize();
double newAngle = Math.Atan2(newDirection.Z, newDirection.X) - Math.Atan2(direction.Z, direction.X);
if(!turningAround)
yaw = MathHelper.Lerp(yaw, yaw - (float)newAngle, 0.05f);
Vector3 nextPosition = position + direction * 0.2f;
nextBoundingSphere = boundingSphere;
nextBoundingSphere.Center = nextPosition;
if (!((nextPosition.X < 2f || nextPosition.X > width - 2f || nextPosition.Z < 2f || nextPosition.Z > height - 2f) ||
(nextPosition.X < 2f && nextPosition.Z < 2f) || (nextPosition.X > width - 2f && nextPosition.Z < 2f) ||
(nextPosition.X > width - 2f && nextPosition.Z > height - 2f) || (nextPosition.X < 2f && nextPosition.Z > height - 2f))
&& !nextCollided && !turningAround)
{
wheelRotationValue += 0.1f;
position += direction * 0.1f;
}
else
{
turningAround = true;
float turnYaw = yaw - 180;
yaw = MathHelper.Lerp(yaw, turnYaw, 0.05f);
if (yaw >= turnYaw)
turningAround = false;
}
}
else
moving = false;
}
boundingSphere.Center = position;
}
public void CheckCollision(BoundingSphere bs)
{
if (boundingSphere.Intersects(bs))
{
collided = true;
/* Music / sound related
hitSound.Play();
*/
}
else
collided = false;
}
public void CheckNextCollision(BoundingSphere bs)
{
if (nextBoundingSphere.Intersects(bs))
nextCollided = true;
else
nextCollided = false;
}
public void Draw()
{
// Calculate matrices based on the current animation position.
Matrix wheelRotation = Matrix.CreateRotationX(wheelRotationValue);
Matrix steerRotation = Matrix.CreateRotationY(steerRotationValue);
Matrix turretRotation = Matrix.CreateRotationY(turretRotationValue);
Matrix cannonRotation = Matrix.CreateRotationX(cannonRotationValue);
Matrix hatchRotation = Matrix.CreateRotationX(hatchRotationValue);
// Apply matrices to the relevant bones.
leftBackWheelBone.Transform = wheelRotation * leftBackWheelTransform;
rightBackWheelBone.Transform = wheelRotation * rightBackWheelTransform;
leftFrontWheelBone.Transform = wheelRotation * leftFrontWheelTransform;
rightFrontWheelBone.Transform = wheelRotation * rightFrontWheelTransform;
leftSteerBone.Transform = steerRotation * leftSteerTransform;
rightSteerBone.Transform = steerRotation * rightSteerTransform;
turretBone.Transform = turretRotation * turretTransform;
cannonBone.Transform = cannonRotation * cannonTransform;
hatchBone.Transform = hatchRotation * hatchTransform;
// Set the world matrix as the root transform of the model.
tankModel.Root.Transform = world;
normalNova = Vector3.Transform(tankOrientation, Matrix.CreateRotationY(-yaw));
Matrix worldMatrix = Matrix.CreateScale(0.005f) * Matrix.CreateFromYawPitchRoll(yaw, normalNova.Z, -normalNova.X) *
Matrix.CreateTranslation(position);
cannonOrientation = Vector3.Transform(Vector3.Forward, Matrix.CreateFromYawPitchRoll(turretRotationValue, cannonRotationValue, 0));
newDirection = Vector3.Transform(new Vector3(0, 0, 1), Matrix.CreateFromYawPitchRoll(yaw, normalNova.Z, -normalNova.X));
tankModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
// Draw the model.
foreach (ModelMesh mesh in tankModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.View = this.effect.View;
effect.Projection = this.effect.Projection;
effect.World = boneTransforms[mesh.ParentBone.Index] * worldMatrix;
effect.EnableDefaultLighting();
effect.LightingEnabled = true;
}
mesh.Draw();
}
}
}
}