Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #include "r015290hTank.h"
  2. #include "../../TankManager.h"
  3. #include "../../Commons.h"
  4. #include "../../C2DMatrix.h"
  5. #include "../../ObstacleManager.h"
  6. #include "r015290hSteeringBehaviour.h"
  7. #include "r015290hPathFinder_DRS.h"
  8. #include "r015290hMaths.h"
  9.  
  10. r015290hTank::r015290hTank(SDL_Renderer* renderer, TankSetupDetails details) : BaseTank(renderer, details)
  11. {
  12. mTankTurnDirection = DIRECTION_UNKNOWN;
  13. mTankMoveDirection = DIRECTION_NONE;
  14. mManTurnDirection = DIRECTION_UNKNOWN;
  15.  
  16. steeringBehaviour = new r015290hSterringBehaviour(this);
  17.  
  18. pathFinder = new r015290hPathFinder_DRS(renderer, mCollisionMap);
  19. pathFound = false;
  20. waypointRadius = 50.0f;
  21. currentWaypointIndex = 0;
  22.  
  23. targetPos = Vector2D(0, 0);
  24. mCurrentSpeed = 40.0f;
  25.  
  26. mManRotation = 0;
  27. }
  28.  
  29. r015290hTank::~r015290hTank()
  30. {
  31. }
  32.  
  33. void r015290hTank::ChangeState(BASE_TANK_STATE newState)
  34. {
  35. BaseTank::ChangeState(newState);
  36. }
  37.  
  38. void r015290hTank::Update(float deltaTime, SDL_Event e)
  39. {
  40. if (e.type == SDL_MOUSEBUTTONDOWN)
  41. {
  42. canMove = true;
  43. SDL_GetMouseState(&x, &y);
  44. pathFound = pathFinder->FindPath(GetCentralPosition(), Vector2D(x, y));
  45. currentWaypointIndex = pathFinder->mPathData.size() - 1;
  46. }
  47.  
  48. if (pathFound)
  49. {
  50. targetPos = pathFinder->mPathData[currentWaypointIndex];
  51. if (CheckNearbyWaypoints())
  52. {
  53. if (currentWaypointIndex == 0)
  54. {
  55. pathFound = false;
  56. canMove = false;
  57. }
  58. else
  59. {
  60. currentWaypointIndex--;
  61. }
  62. }
  63. }
  64.  
  65. RotateTurret(deltaTime, targetPos);
  66.  
  67. //Call parent update.
  68. BaseTank::Update(deltaTime, e);
  69. }
  70.  
  71. void r015290hTank::Render()
  72. {
  73. BaseTank::Render();
  74.  
  75. DrawDebugCircle(Steering()->ObstaclePos, 4.0f, 0, 255, 0);
  76.  
  77. DrawDebugCircle(Steering()->ahead, 4.0f, 255, 255, 255);
  78. DrawDebugCircle(Steering()->aheadLeftCorner, 4.0f, 255, 255, 255);
  79. DrawDebugCircle(Steering()->aheadRightCorner, 4.0f, 255, 255, 255);
  80. DrawDebugCircle(Steering()->aheadLeftCorner2, 4.0f, 255, 255, 255);
  81. DrawDebugCircle(Steering()->aheadRightCorner2, 4.0f, 255, 255, 255);
  82.  
  83. //DrawDebugCircle(GetCentralPosition(), waypointRadius, 255, 0, 0);
  84.  
  85. /*
  86. for (size_t i = 0; i < pathFinder->mPathData.size(); i++)
  87. {
  88. DrawDebugCircle(pathFinder->mPathData[i], 5.0f, 255, 255, 255);
  89. if (i > 0)
  90. {
  91. DrawDebugLine(pathFinder->mPathData[i - 1], pathFinder->mPathData[i], 0, 255, 0);
  92. }
  93. }
  94. */
  95. }
  96.  
  97. void r015290hTank::MoveInHeadingDirection(float deltaTime)
  98. {
  99. RotateTankInMoveDirection(deltaTime);
  100.  
  101. //Get the force that propels in current heading.
  102. Vector2D force = Vector2D();
  103. if (canMove)
  104. {
  105. //mRotationAngle = r015290hMaths::Lerp(mRotationAngle, RadsToDegs(atan2(GetVelocity().y, GetVelocity().x)) + 90, 0.2);
  106. Vector2D obstacleAvoidence = Steering()->ObstacleAvoidance();
  107. force += Steering()->Seek(targetPos) + obstacleAvoidence;
  108. }
  109. else
  110. {
  111. force -= mVelocity;
  112. }
  113.  
  114. //Acceleration = Force/Mass
  115. Vector2D acceleration = force/GetMass();
  116.  
  117. //Update velocity.
  118. mVelocity += acceleration * deltaTime;
  119.  
  120. //Don't allow the tank does not go faster than max speed.
  121. mVelocity.Truncate(GetMaxSpeed()); //TODO: Add Penalty for going faster than MAX Speed.
  122.  
  123. //Finally, update the position.
  124. Vector2D newPosition = GetPosition();
  125. newPosition.x += mVelocity.x * deltaTime;
  126. newPosition.y += (mVelocity.y/**-1.0f*/) * deltaTime; //Y flipped as adding to Y moves down screen.
  127. SetPosition(newPosition);
  128. }
  129.  
  130. void r015290hTank::RotateTurret(float deltaTime, Vector2D target)
  131. {
  132. Vector2D toTarget = target - GetCentralPosition();
  133. toTarget.Normalize();
  134.  
  135. double dot = toTarget.Dot(mManFireDirection);
  136. if (dot < 0.95f)
  137. {
  138. RotateManByRadian(mMaxTurnRate, -1, deltaTime);
  139. }
  140. }
  141.  
  142. void r015290hTank::RotateTankInMoveDirection(float deltaTime)
  143. {
  144. double difference = GetVelocity().Dot(GetHeading());
  145.  
  146. std::cout << difference << std::endl;
  147.  
  148. RotateHeadingByRadian(mMaxTurnRate, -1, deltaTime);
  149. }
  150.  
  151. bool r015290hTank::CheckNearbyWaypoints()
  152. {
  153. Vector2D vecToWaypoint = targetPos - GetCentralPosition();
  154.  
  155. if (vecToWaypoint.Length() <= waypointRadius)
  156. {
  157. return true;
  158. }
  159.  
  160. return false;
  161. }
  162.  
  163. std::vector<GameObject*> r015290hTank::GetNearbyObstacles(double collisionRadius)
  164. {
  165. std::vector<GameObject*> nearbyObstacles = std::vector<GameObject*>();
  166. std::vector<GameObject*> obstacles = ObstacleManager::Instance()->GetObstacles();
  167.  
  168. for (int i = 0; i < obstacles.size(); i++)
  169. {
  170. Vector2D vecToObstacle = GetCentralPosition() - obstacles[i]->GetCentralPosition();
  171. if (vecToObstacle.Length() <= collisionRadius)
  172. {
  173. nearbyObstacles.push_back(obstacles[i]);
  174. }
  175. }
  176.  
  177. return nearbyObstacles;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement