HUD.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "HUD.h"
  9. #include "Texture.h"
  10. #include "Shader.h"
  11. #include "Game.h"
  12. #include "Renderer.h"
  13. #include "PhysWorld.h"
  14. #include "FollowActor.h"
  15. #include <algorithm>
  16. #include "TargetComponent.h"
  17. HUD::HUD(Game* game)
  18. :UIScreen(game)
  19. ,mRadarRange(2000.0f)
  20. ,mRadarRadius(92.0f)
  21. ,mTargetEnemy(false)
  22. {
  23. Renderer* r = mGame->GetRenderer();
  24. mHealthBar = r->GetTexture("Assets/HealthBar.png");
  25. mRadar = r->GetTexture("Assets/Radar.png");
  26. mCrosshair = r->GetTexture("Assets/Crosshair.png");
  27. mCrosshairEnemy = r->GetTexture("Assets/CrosshairRed.png");
  28. mBlipTex = r->GetTexture("Assets/Blip.png");
  29. mRadarArrow = r->GetTexture("Assets/RadarArrow.png");
  30. }
  31. HUD::~HUD()
  32. {
  33. }
  34. void HUD::Update(float deltaTime)
  35. {
  36. UIScreen::Update(deltaTime);
  37. UpdateCrosshair(deltaTime);
  38. UpdateRadar(deltaTime);
  39. }
  40. void HUD::Draw(Shader* shader)
  41. {
  42. // Crosshair
  43. //Texture* cross = mTargetEnemy ? mCrosshairEnemy : mCrosshair;
  44. //DrawTexture(shader, cross, Vector2::Zero, 2.0f);
  45. // Radar
  46. const Vector2 cRadarPos(-390.0f, 275.0f);
  47. DrawTexture(shader, mRadar, cRadarPos, 1.0f);
  48. // Blips
  49. for (Vector2& blip : mBlips)
  50. {
  51. DrawTexture(shader, mBlipTex, cRadarPos + blip, 1.0f);
  52. }
  53. // Radar arrow
  54. DrawTexture(shader, mRadarArrow, cRadarPos);
  55. //// Health bar
  56. //DrawTexture(shader, mHealthBar, Vector2(-350.0f, -350.0f));
  57. }
  58. void HUD::AddTargetComponent(TargetComponent* tc)
  59. {
  60. mTargetComps.emplace_back(tc);
  61. }
  62. void HUD::RemoveTargetComponent(TargetComponent* tc)
  63. {
  64. auto iter = std::find(mTargetComps.begin(), mTargetComps.end(),
  65. tc);
  66. mTargetComps.erase(iter);
  67. }
  68. void HUD::UpdateCrosshair(float deltaTime)
  69. {
  70. // Reset to regular cursor
  71. mTargetEnemy = false;
  72. // Make a line segment
  73. const float cAimDist = 5000.0f;
  74. Vector3 start, dir;
  75. mGame->GetRenderer()->GetScreenDirection(start, dir);
  76. LineSegment l(start, start + dir * cAimDist);
  77. // Segment cast
  78. PhysWorld::CollisionInfo info;
  79. if (mGame->GetPhysWorld()->SegmentCast(l, info))
  80. {
  81. // Is this a target?
  82. for (auto tc : mTargetComps)
  83. {
  84. if (tc->GetOwner() == info.mActor)
  85. {
  86. mTargetEnemy = true;
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. void HUD::UpdateRadar(float deltaTime)
  93. {
  94. // Clear blip positions from last frame
  95. mBlips.clear();
  96. // Convert player position to radar coordinates (x forward, z up)
  97. Vector3 playerPos = mGame->GetPlayer()->GetPosition();
  98. Vector2 playerPos2D(playerPos.y, playerPos.x);
  99. // Ditto for player forward
  100. Vector3 playerForward = mGame->GetPlayer()->GetForward();
  101. Vector2 playerForward2D(playerForward.x, playerForward.y);
  102. // Use atan2 to get rotation of radar
  103. float angle = Math::Atan2(playerForward2D.y, playerForward2D.x);
  104. // Make a 2D rotation matrix
  105. Matrix3 rotMat = Matrix3::CreateRotation(angle);
  106. // Get positions of blips
  107. for (auto tc : mTargetComps)
  108. {
  109. Vector3 targetPos = tc->GetOwner()->GetPosition();
  110. Vector2 actorPos2D(targetPos.y, targetPos.x);
  111. // Calculate vector between player and target
  112. Vector2 playerToTarget = actorPos2D - playerPos2D;
  113. // See if within range
  114. if (playerToTarget.LengthSq() <= (mRadarRange * mRadarRange))
  115. {
  116. // Convert playerToTarget into an offset from
  117. // the center of the on-screen radar
  118. Vector2 blipPos = playerToTarget;
  119. blipPos *= mRadarRadius/mRadarRange;
  120. // Rotate blipPos
  121. blipPos = Vector2::Transform(blipPos, rotMat);
  122. mBlips.emplace_back(blipPos);
  123. }
  124. }
  125. }