PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Samples/CarGame2D/Car.cs

#
C# | 98 lines | 85 code | 10 blank | 3 comment | 6 complexity | 9514290873e2562d7f41b7cebe9014a6 MD5 | raw file
Possible License(s): Apache-2.0
  1. using DeltaEngine.Commands;
  2. using DeltaEngine.Core;
  3. using DeltaEngine.Datatypes;
  4. using DeltaEngine.Entities;
  5. using DeltaEngine.Extensions;
  6. using DeltaEngine.Rendering2D;
  7. namespace CarGame2D
  8. {
  9. /// <summary>
  10. /// Handle our car and the simple physics we need for driving around the track.
  11. /// </summary>
  12. public sealed class Car : Sprite, Updateable
  13. {
  14. public Car(Vector2D startPosition, Track setTrack)
  15. : base("CarImage", new Rectangle(startPosition, new Size(0.075f, 0.05f)))
  16. {
  17. track = setTrack;
  18. velocity = new Vector2D(1f, 0f);
  19. collisionRadius = DrawArea.Width / 2;
  20. NextTrackIndex = 0;
  21. new Command(Command.MoveLeft,
  22. () => velocity = velocity.Rotate(-speed * RotationStep * Time.Delta));
  23. new Command(Command.MoveRight,
  24. () => velocity = velocity.Rotate(speed * RotationStep * Time.Delta));
  25. new Command(Command.MoveUp, () => speed += Time.Delta * 2);
  26. }
  27. private readonly Track track;
  28. private Vector2D velocity;
  29. private readonly float collisionRadius;
  30. public int NextTrackIndex { get; private set; }
  31. private float speed;
  32. private const float RotationStep = 100.0f;
  33. public void Update()
  34. {
  35. ReduceSpeedAutomaticallyAndLimitIt();
  36. CalculateNextTrackIndex();
  37. velocity = CalculateVelocity();
  38. Center += (velocity * speed) * Time.Delta * 0.2f;
  39. float oldRotation = Rotation;
  40. Rotation = velocity.GetRotation();
  41. if ((oldRotation - Rotation).Abs() > 50)
  42. SetWithoutInterpolation(Rotation);
  43. }
  44. private void ReduceSpeedAutomaticallyAndLimitIt()
  45. {
  46. speed -= Time.Delta;
  47. speed = speed.Clamp(0f, MaxSpeed);
  48. }
  49. private const float MaxSpeed = 1.75f;
  50. private void CalculateNextTrackIndex()
  51. {
  52. float distanceToNext = (Center - track.Positions[NextTrackIndex]).LengthSquared;
  53. if (distanceToNext <= lastDistanceToNext)
  54. return;
  55. NextTrackIndex++;
  56. NextTrackIndex %= track.Positions.Length;
  57. lastDistanceToNext = (Center - track.Positions[NextTrackIndex]).LengthSquared;
  58. }
  59. private float lastDistanceToNext;
  60. private Vector2D CalculateVelocity()
  61. {
  62. Vector2D carBackward = Center - (velocity * collisionRadius);
  63. Vector2D carForward = Center + (velocity * collisionRadius);
  64. for (int index = 0; index < track.Positions.Length - 1; index++)
  65. {
  66. Vector2D inner1 = track.TrackInnerBounds[index];
  67. Vector2D inner2 = track.TrackInnerBounds[index + 1];
  68. if (MathExtensions.IsLineIntersectingWith(carBackward, carForward, inner1, inner2))
  69. return GetVelocityAfterTrackLineCollision(inner1, inner2);
  70. Vector2D outer1 = track.TrackOuterBounds[index];
  71. Vector2D outer2 = track.TrackOuterBounds[index + 1];
  72. if (MathExtensions.IsLineIntersectingWith(carBackward, carForward, outer1, outer2))
  73. return GetVelocityAfterTrackLineCollision(outer1, outer2);
  74. }
  75. return velocity;
  76. }
  77. private Vector2D GetVelocityAfterTrackLineCollision(Vector2D inner1, Vector2D inner2)
  78. {
  79. SlowDownIfAboveOneThirdOfMaxSpeed();
  80. return Vector2D.Normalize(inner2 - inner1);
  81. }
  82. private void SlowDownIfAboveOneThirdOfMaxSpeed()
  83. {
  84. if (speed > MaxSpeed / 3)
  85. speed /= 2.0f;
  86. }
  87. }
  88. }