Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.79 KB | None | 0 0
  1. #include "PlayerInfo.h"
  2. #include <iostream>
  3.  
  4. #include "MouseController.h"
  5. #include "KeyboardController.h"
  6. #include "Mtx44.h"
  7. #include "../Projectile/Projectile.h"
  8. #include "../WeaponInfo/Pistol.h"
  9. #include "../WeaponInfo/AssualtRifle.h"
  10. #include "../Minimap/Minimap.h"
  11.  
  12. // Allocating and initializing CPlayerInfo's static data member.
  13. // The pointer is allocated but not the object's constructor.
  14. CPlayerInfo *CPlayerInfo::s_instance = 0;
  15.  
  16. CPlayerInfo::CPlayerInfo(void)
  17. : m_dSpeed(40.0)
  18. , m_dAcceleration(10.0)
  19. , m_bJumpUpwards(false)
  20. , m_dJumpSpeed(30.0)
  21. , m_dJumpAcceleration(-10.0)
  22. , m_bFallDownwards(false)
  23. , m_dFallSpeed(0.0)
  24. , m_dFallAcceleration(-10.0)
  25. , m_dElapsedTime(0.0)
  26. , attachedCamera(NULL)
  27. , m_pTerrain(NULL)
  28. , primaryWeapon(NULL)
  29. , secondaryWeapon(NULL)
  30. , theCurrentPosture(STAND)
  31. , weaponManager(NULL)
  32. , m_iCurrentWeapon(0)
  33. , m_fCameraSwayAngle(0.0f)
  34. , m_fCameraSwayDeltaAngle(0.1f)
  35. , m_fCameraSwayAngle_LeftLimit(-0.3f)
  36. , m_fCameraSwayAngle_RightLimit(0.3f)
  37. , m_bCameraSwayDirection(false)
  38. {
  39. }
  40.  
  41. CPlayerInfo::~CPlayerInfo(void)
  42. {
  43. if (weaponManager)
  44. {
  45. for (int i = 0; i < m_iNumOfWeapon; i++)
  46. {
  47. delete weaponManager[i];
  48. }
  49. delete [] weaponManager;
  50. weaponManager = NULL;
  51. }
  52. if (secondaryWeapon)
  53. {
  54. delete secondaryWeapon;
  55. secondaryWeapon = NULL;
  56. }
  57. if (primaryWeapon)
  58. {
  59. delete primaryWeapon;
  60. primaryWeapon = NULL;
  61. }
  62. m_pTerrain = NULL;
  63. }
  64.  
  65. // Initialise this class instance
  66. void CPlayerInfo::Init(void)
  67. {
  68. // Set the default values
  69. defaultPosition.Set(0,0,10);
  70. defaultTarget.Set(0,0,0);
  71. defaultUp.Set(0,1,0);
  72.  
  73. // Set the current values
  74. position.Set(0, 0, 10);
  75. target.Set(0, 0, 0);
  76. up.Set(0, 1, 0);
  77.  
  78. // Set Boundary
  79. maxBoundary.Set(1,1,1);
  80. minBoundary.Set(-1, -1, -1);
  81.  
  82. // Set the pistol as the primary weapon
  83. primaryWeapon = new CPistol();
  84. primaryWeapon->Init();
  85.  
  86. weaponManager = new CWeaponInfo*[m_iNumOfWeapon];
  87. weaponManager[0] = new CPistol();
  88. weaponManager[0]->Init();
  89.  
  90. weaponManager[1] = new CAssualtRifle();
  91. weaponManager[1]->Init();
  92.  
  93. /*for (int i = 0; i < m_iNumOfWeapon; i++)
  94. {
  95. weaponManager[i] = new CPistol();
  96. weaponManager[i]->Init();
  97. }*/
  98.  
  99. m_fCameraSwayAngle = 0.0f;
  100. m_fCameraSwayDeltaAngle = 1.0f;
  101. m_fCameraSwayAngle_LeftLimit = -5.0f;
  102. m_fCameraSwayAngle_RightLimit = 5.0f;
  103. m_bCameraSwayDirection = false;
  104. }
  105.  
  106. // Returns true if the player is on ground
  107. bool CPlayerInfo::isOnGround(void)
  108. {
  109. if (m_bJumpUpwards == false && m_bFallDownwards == false && position.y == m_pTerrain->GetTerrainHeight(position))
  110. return true;
  111.  
  112. return false;
  113. }
  114.  
  115. // Returns true if the player is jumping upwards
  116. bool CPlayerInfo::isJumpUpwards(void)
  117. {
  118. if (m_bJumpUpwards == true && m_bFallDownwards == false)
  119. return true;
  120.  
  121. return false;
  122. }
  123.  
  124. // Returns true if the player is on freefall
  125. bool CPlayerInfo::isFreeFall(void)
  126. {
  127. if (m_bJumpUpwards == false && m_bFallDownwards == true)
  128. return true;
  129.  
  130. return false;
  131. }
  132.  
  133. // Set the player's status to free fall mode
  134. void CPlayerInfo::SetOnFreeFall(bool isOnFreeFall)
  135. {
  136. if (isOnFreeFall == true)
  137. {
  138. m_bJumpUpwards = false;
  139. m_bFallDownwards = true;
  140. m_dFallSpeed = 0.0;
  141. }
  142. }
  143.  
  144. // Set the player to jumping upwards
  145. void CPlayerInfo::SetToJumpUpwards(bool isOnJumpUpwards)
  146. {
  147. if (isOnJumpUpwards == true)
  148. {
  149. m_bJumpUpwards = true;
  150. m_bFallDownwards = false;
  151. m_dJumpSpeed = 10.0;
  152. }
  153. }
  154.  
  155. // Set position
  156. void CPlayerInfo::SetPos(const Vector3& pos)
  157. {
  158. position = pos;
  159. }
  160.  
  161. // Set target
  162. void CPlayerInfo::SetTarget(const Vector3& target)
  163. {
  164. this->target = target;
  165. }
  166.  
  167. // Set position
  168. void CPlayerInfo::SetUp(const Vector3& up)
  169. {
  170. this->up = up;
  171. }
  172.  
  173. // Set m_dJumpAcceleration of the player
  174. void CPlayerInfo::SetJumpAcceleration(const double m_dJumpAcceleration)
  175. {
  176. this->m_dJumpAcceleration = m_dJumpAcceleration;
  177. }
  178.  
  179. // Set Fall Acceleration of the player
  180. void CPlayerInfo::SetFallAcceleration(const double m_dFallAcceleration)
  181. {
  182. this->m_dFallAcceleration = m_dFallAcceleration;
  183. }
  184.  
  185. // Set the boundary for the player info
  186. void CPlayerInfo::SetBoundary(Vector3 max, Vector3 min)
  187. {
  188. maxBoundary = max;
  189. minBoundary = min;
  190. }
  191.  
  192. // Set the terrain for the player info
  193. void CPlayerInfo::SetTerrain(GroundEntity* m_pTerrain)
  194. {
  195. if (m_pTerrain != NULL)
  196. {
  197. this->m_pTerrain = m_pTerrain;
  198.  
  199. SetBoundary(this->m_pTerrain->GetMaxBoundary(), this->m_pTerrain->GetMinBoundary());
  200. }
  201. }
  202.  
  203. // Stop the player's movement
  204. void CPlayerInfo::StopVerticalMovement(void)
  205. {
  206. m_bJumpUpwards = false;
  207. m_bFallDownwards = false;
  208. }
  209.  
  210. // Reset this player instance to default
  211. void CPlayerInfo::Reset(void)
  212. {
  213. // Set the current values to default values
  214. position = defaultPosition;
  215. target = defaultTarget;
  216. up = defaultUp;
  217.  
  218. // Stop vertical movement too
  219. StopVerticalMovement();
  220. }
  221.  
  222. // Get position x of the player
  223. Vector3 CPlayerInfo::GetPos(void) const
  224. {
  225. return position;
  226. }
  227.  
  228. // Get target
  229. Vector3 CPlayerInfo::GetTarget(void) const
  230. {
  231. return target;
  232. }
  233. // Get Up
  234. Vector3 CPlayerInfo::GetUp(void) const
  235. {
  236. return up;
  237. }
  238.  
  239. // Get m_dJumpAcceleration of the player
  240. double CPlayerInfo::GetJumpAcceleration(void) const
  241. {
  242. return m_dJumpAcceleration;
  243. }
  244.  
  245. // Update Jump Upwards
  246. void CPlayerInfo::UpdateJumpUpwards(double dt)
  247. {
  248. if (m_bJumpUpwards == false)
  249. return;
  250.  
  251. // Update the jump's elapsed time
  252. m_dElapsedTime += dt;
  253.  
  254. // Update position and target y values
  255. // Use SUVAT equation to update the change in position and target
  256. // s = u * t + 0.5 * a * t ^ 2
  257. position.y += (float)(m_dJumpSpeed * m_dElapsedTime +
  258. 0.5 * m_dJumpAcceleration * m_dElapsedTime * m_dElapsedTime);
  259. target.y += (float)(m_dJumpSpeed * m_dElapsedTime +
  260. 0.5 * m_dJumpAcceleration * m_dElapsedTime * m_dElapsedTime);
  261. // Use this equation to calculate final velocity, v
  262. // SUVAT: v = u + a * t; v is m_dJumpSpeed AFTER updating using SUVAT where u is
  263. // the initial speed and is equal to m_dJumpSpeed
  264. m_dJumpSpeed = m_dJumpSpeed + m_dJumpAcceleration * m_dElapsedTime;
  265. // Check if the jump speed is less than zero, then it should be falling
  266. if (m_dJumpSpeed < 0.0)
  267. {
  268. m_dJumpSpeed = 0.0;
  269. m_bJumpUpwards = false;
  270. m_dFallSpeed = 0.0;
  271. m_bFallDownwards = true;
  272. m_dElapsedTime = 0.0;
  273. }
  274. }
  275.  
  276. // Update FreeFall
  277. void CPlayerInfo::UpdateFreeFall(double dt)
  278. {
  279. if (m_bFallDownwards == false)
  280. return;
  281.  
  282. // Update the jump's elapsed time
  283. m_dElapsedTime += dt;
  284.  
  285. // Update position and target y values.
  286. // Use SUVAT equation to update the change in position and target
  287. // s = u * t + 0.5 * a * t ^ 2
  288. position.y += (float)(m_dFallSpeed * m_dElapsedTime +
  289. 0.5 * m_dJumpAcceleration * m_dElapsedTime * m_dElapsedTime);
  290. target.y += (float)(m_dFallSpeed * m_dElapsedTime +
  291. 0.5 * m_dJumpAcceleration * m_dElapsedTime * m_dElapsedTime);
  292. // Use this equation to calculate final velocity, v
  293. // SUVAT: v = u + a * t;
  294. // v is m_dJumpSpeed AFTER updating using SUVAT where u is the initial speed and is equal to m_dJumpSpeed
  295. m_dFallSpeed = m_dFallSpeed + m_dFallAcceleration * m_dElapsedTime;
  296. // Check if the jump speed is below terrain, then it should be reset to terrain height
  297. if (position.y < m_pTerrain->GetTerrainHeight(position))
  298. {
  299. Vector3 viewDirection = target - position;
  300. position.y = m_pTerrain->GetTerrainHeight(position);
  301. target = position + viewDirection;
  302. m_dFallSpeed = 0.0;
  303. m_bFallDownwards = false;
  304. m_dElapsedTime = 0.0;
  305. }
  306. }
  307.  
  308. /********************************************************************************
  309. Hero Update
  310. ********************************************************************************/
  311. void CPlayerInfo::Update(double dt)
  312. {
  313. //double mouse_diff_x, mouse_diff_y;
  314. //MouseController::GetInstance()->GetMouseDelta(mouse_diff_x, mouse_diff_y);
  315.  
  316. //double camera_yaw = mouse_diff_x * 0.0174555555555556; // 3.142 / 180.0
  317. //double camera_pitch = mouse_diff_y * 0.0174555555555556; // 3.142 / 180.0
  318.  
  319. // Check if there is a need to change posture
  320. /*if (KeyboardController::GetInstance()->IsKeyReleased('Z'))
  321. {
  322. theCurrentPosture = (CURRENT_POSTURE)(theCurrentPosture + 1);
  323. if (theCurrentPosture == NUM_POSTURE)
  324. theCurrentPosture = STAND;
  325.  
  326. Vector3 viewDirection = target - position;
  327. switch (theCurrentPosture)
  328. {
  329. case STAND:
  330. position.y = m_pTerrain->GetTerrainHeight(Vector3(position.x, 0.0f, position.z));
  331. target = position + viewDirection;
  332. break;
  333. case CROUCH:
  334. position.y = m_pTerrain->GetTerrainHeight(Vector3(position.x, 0.0f, position.z));
  335. position.y -= 5.0f;
  336. target = position + viewDirection;
  337. break;
  338. case PRONE:
  339. position.y = m_pTerrain->GetTerrainHeight(Vector3(position.x, 0.0f, position.z));
  340. position.y -= 8.0f;
  341. target = position + viewDirection;
  342. break;
  343. default:
  344. break;
  345. }
  346. }*/
  347.  
  348. //// Update the position if the WASD buttons were activated
  349. //if (KeyboardController::GetInstance()->IsKeyDown('W') ||
  350. // KeyboardController::GetInstance()->IsKeyDown('A') ||
  351. // KeyboardController::GetInstance()->IsKeyDown('S') ||
  352. // KeyboardController::GetInstance()->IsKeyDown('D'))
  353. //{
  354. // Vector3 viewVector = target - position;
  355. // Vector3 rightUV;
  356. // if (KeyboardController::GetInstance()->IsKeyDown('W'))
  357. // {
  358. // if ((KeyboardController::GetInstance()->IsKeyDown('W')) && (KeyboardController::GetInstance()->IsKeyDown(VK_SHIFT)))
  359. // position += viewVector.Normalized() * (float)m_dSpeed * 2.0f * (float)dt;
  360. // else
  361. // position += viewVector.Normalized() * (float)m_dSpeed * (float)dt;
  362. // }
  363. // else if (KeyboardController::GetInstance()->IsKeyDown('S'))
  364. // {
  365. // position -= viewVector.Normalized() * (float)m_dSpeed * (float)dt;
  366. // }
  367. // if (KeyboardController::GetInstance()->IsKeyDown('A'))
  368. // {
  369. // rightUV = (viewVector.Normalized()).Cross(up);
  370. // rightUV.y = 0;
  371. // rightUV.Normalize();
  372. // position -= rightUV * (float)m_dSpeed * (float)dt;
  373. // }
  374. // else if (KeyboardController::GetInstance()->IsKeyDown('D'))
  375. // {
  376. // rightUV = (viewVector.Normalized()).Cross(up);
  377. // rightUV.y = 0;
  378. // rightUV.Normalize();
  379. // position += rightUV * (float)m_dSpeed * (float)dt;
  380. // }
  381. // Constrain the position
  382. // Constrain();
  383. // // Update the target
  384. // target = position + viewVector;
  385. //}
  386.  
  387. //// Rotate the view direction
  388. //if (KeyboardController::GetInstance()->IsKeyDown(VK_LEFT) ||
  389. // KeyboardController::GetInstance()->IsKeyDown(VK_RIGHT) ||
  390. // KeyboardController::GetInstance()->IsKeyDown(VK_UP) ||
  391. // KeyboardController::GetInstance()->IsKeyDown(VK_DOWN))
  392. //{
  393. // Vector3 viewUV = (target - position).Normalized();
  394. // Vector3 rightUV;
  395. // if (KeyboardController::GetInstance()->IsKeyDown(VK_LEFT))
  396. // {
  397. // float yaw = (float)m_dSpeed * (float)dt;
  398. // Mtx44 rotation;
  399. // rotation.SetToRotation(yaw, 0, 1, 0);
  400. // viewUV = rotation * viewUV;
  401. // target = position + viewUV;
  402. // rightUV = viewUV.Cross(up);
  403. // rightUV.y = 0;
  404. // rightUV.Normalize();
  405. // up = rightUV.Cross(viewUV).Normalized();
  406. // }
  407. // else if (KeyboardController::GetInstance()->IsKeyDown(VK_RIGHT))
  408. // {
  409. // float yaw = (float)(-m_dSpeed * (float)dt);
  410. // Mtx44 rotation;
  411. // rotation.SetToRotation(yaw, 0, 1, 0);
  412. // viewUV = rotation * viewUV;
  413. // target = position + viewUV;
  414. // rightUV = viewUV.Cross(up);
  415. // rightUV.y = 0;
  416. // rightUV.Normalize();
  417. // up = rightUV.Cross(viewUV).Normalized();
  418. // }
  419. // if (KeyboardController::GetInstance()->IsKeyDown(VK_UP))
  420. // {
  421. // float pitch = (float)(m_dSpeed * (float)dt);
  422. // rightUV = viewUV.Cross(up);
  423. // rightUV.y = 0;
  424. // rightUV.Normalize();
  425. // up = rightUV.Cross(viewUV).Normalized();
  426. // Mtx44 rotation;
  427. // rotation.SetToRotation(pitch, rightUV.x, rightUV.y, rightUV.z);
  428. // viewUV = rotation * viewUV;
  429. // target = position + viewUV;
  430. // }
  431. // else if (KeyboardController::GetInstance()->IsKeyDown(VK_DOWN))
  432. // {
  433. // float pitch = (float)(-m_dSpeed * (float)dt);
  434. // rightUV = viewUV.Cross(up);
  435. // rightUV.y = 0;
  436. // rightUV.Normalize();
  437. // up = rightUV.Cross(viewUV).Normalized();
  438. // Mtx44 rotation;
  439. // rotation.SetToRotation(pitch, rightUV.x, rightUV.y, rightUV.z);
  440. // viewUV = rotation * viewUV;
  441. // target = position + viewUV;
  442. // }
  443. //}
  444.  
  445. ////Update the camera direction based on mouse move
  446. //{
  447. // Vector3 viewUV = (target - position).Normalized();
  448. // Vector3 rightUV;
  449.  
  450. // if (camera_yaw != 0.0)
  451. // {
  452. // float yaw = (float)(-m_dSpeed * camera_yaw * (float)dt);
  453. // Mtx44 rotation;
  454. // rotation.SetToRotation(yaw, 0, 1, 0);
  455. // viewUV = rotation * viewUV;
  456. // target = position + viewUV;
  457. // rightUV = viewUV.Cross(up);
  458. // rightUV.y = 0;
  459. // rightUV.Normalize();
  460. // up = rightUV.Cross(viewUV).Normalized();
  461. //
  462. // // Update the minimap rotation angle
  463. // CMinimap::GetInstance()->SetAngle(atan2(viewUV.z, viewUV.x) * 57.2883513685549146);
  464. // }
  465. // {
  466. // float pitch = (float)(-m_dSpeed * camera_pitch * (float)dt);
  467. // rightUV = viewUV.Cross(up);
  468. // rightUV.y = 0;
  469. // rightUV.Normalize();
  470. // up = rightUV.Cross(viewUV).Normalized();
  471. // Mtx44 rotation;
  472. // rotation.SetToRotation(pitch, rightUV.x, rightUV.y, rightUV.z);
  473. // viewUV = rotation * viewUV;
  474. // target = position + viewUV;
  475. // }
  476. //}
  477.  
  478. //// If the user presses SPACEBAR, then make him jump
  479. //if (KeyboardController::GetInstance()->IsKeyDown(VK_SPACE) &&
  480. // position.y == m_pTerrain->GetTerrainHeight(position))
  481. //{
  482. // SetToJumpUpwards(true);
  483. //}
  484.  
  485. //// Update the weapons
  486. //if (KeyboardController::GetInstance()->IsKeyReleased('R'))
  487. //{
  488. // //if (primaryWeapon)
  489. // //{
  490. // // primaryWeapon->Reload();
  491. // // //primaryWeapon->PrintSelf();
  492. // //}
  493. // if (weaponManager[m_iCurrentWeapon])
  494. // weaponManager[m_iCurrentWeapon]->Reload();
  495. //}
  496. if (primaryWeapon)
  497. primaryWeapon->Update(dt);
  498. if (secondaryWeapon)
  499. secondaryWeapon->Update(dt);
  500. if (weaponManager[m_iCurrentWeapon])
  501. weaponManager[m_iCurrentWeapon]->Update(dt);
  502.  
  503. //if (MouseController::GetInstance()->GetMouseScrollStatus(MouseController::SCROLL_TYPE_YOFFSET) != m_iCurrentWeapon)
  504. //{
  505. // if ((MouseController::GetInstance()->GetMouseScrollStatus(MouseController::SCROLL_TYPE_YOFFSET) >= 0) &&
  506. // (MouseController::GetInstance()->GetMouseScrollStatus(MouseController::SCROLL_TYPE_YOFFSET) < m_iNumOfWeapon))
  507. // {
  508. // m_iCurrentWeapon = MouseController::GetInstance()->GetMouseScrollStatus(MouseController::SCROLL_TYPE_YOFFSET);
  509. // }
  510. //}
  511.  
  512. //// if Mouse Buttons were activated, then act on them
  513. //if (MouseController::GetInstance()->IsButtonPressed(MouseController::LMB))
  514. //{
  515. // //if (primaryWeapon)
  516. // // primaryWeapon->Discharge(position, target, this);
  517.  
  518. // if (weaponManager[m_iCurrentWeapon])
  519. // weaponManager[m_iCurrentWeapon]->Discharge(position, target, this);
  520. //}
  521. //else if (MouseController::GetInstance()->IsButtonPressed(MouseController::RMB))
  522. //{
  523.  
  524. //}
  525.  
  526. //// If the user presses R key, then reset the view to default values
  527. //if (KeyboardController::GetInstance()->IsKeyDown('P'))
  528. //{
  529. // Reset();
  530. //}
  531. //else
  532. {
  533. // Constrain the position
  534. //Constrain();
  535. UpdateJumpUpwards(dt);
  536. UpdateFreeFall(dt);
  537. }
  538.  
  539. // Do camera sway
  540. if (m_fCameraSwayAngle != 0.0f)
  541. {
  542. Mtx44 rotation;
  543. if (m_bCameraSwayDirection == false)
  544. rotation.SetToRotation(-m_fCameraSwayDeltaAngle, 0.0f, 0.0f, 1.0f);
  545. else if (m_bCameraSwayDirection == true)
  546. rotation.SetToRotation(m_fCameraSwayDeltaAngle, 0.0f, 0.0f, 1.0f);
  547. up = rotation * up;
  548. }
  549.  
  550. // Update minimap rotation angle
  551. Vector3 viewUV = (target - position).Normalized();
  552. CMinimap::GetInstance()->SetAngle(atan2(viewUV.z, viewUV.x) * 57.2883513685549146);
  553.  
  554. // If a camera is attached to this playerInfo class, then update it
  555. if (attachedCamera)
  556. {
  557. attachedCamera->SetCameraPos(position);
  558. attachedCamera->SetCameraTarget(target);
  559. attachedCamera->SetCameraUp(up);
  560. }
  561. }
  562.  
  563. // Detect and process front / back movement on the controller
  564. bool CPlayerInfo::Move_FrontBack(const float deltaTime, const bool direction, const float speedMultiplier)
  565. {
  566. // Add camera sway
  567. if (m_bCameraSwayDirection == false)
  568. {
  569. m_fCameraSwayAngle -= m_fCameraSwayDeltaAngle;
  570. if (m_fCameraSwayAngle < m_fCameraSwayAngle_LeftLimit * speedMultiplier)
  571. m_bCameraSwayDirection = !m_bCameraSwayDirection;
  572. }
  573. else
  574. {
  575. m_fCameraSwayAngle += m_fCameraSwayDeltaAngle;
  576. if (m_fCameraSwayAngle > m_fCameraSwayAngle_RightLimit * speedMultiplier)
  577. m_bCameraSwayDirection = !m_bCameraSwayDirection;
  578. }
  579.  
  580. Vector3 viewVector = (target - position).Normalized();
  581.  
  582. if (direction)
  583. {
  584. //position += viewVector * (float)m_dSpeed * speedMultiplier * (float)deltaTime;
  585. if ((theCurrentPosture == STAND) && (KeyboardController::GetInstance()->IsKeyDown(VK_SHIFT)))
  586. position += viewVector * (float)m_dSpeed * 2.0f * (float)deltaTime;
  587. else if (theCurrentPosture == CROUCH)
  588. position += viewVector * (float)m_dSpeed * 0.75f * (float)deltaTime;
  589. else if (theCurrentPosture == PRONE)
  590. position += viewVector * (float)m_dSpeed * 0.25f * (float)deltaTime;
  591. else
  592. position += viewVector * (float)m_dSpeed * (float)deltaTime;
  593.  
  594. // Constrain the position
  595. Constrain();
  596. // Update the target
  597. target = position + viewVector;
  598.  
  599. return true;
  600. }
  601. else
  602. {
  603. position -= viewVector * (float)m_dSpeed * (float)deltaTime;
  604. // Constrain the position
  605. Constrain();
  606. // Update the target
  607. target = position + viewVector;
  608. return true;
  609. }
  610.  
  611. return false;
  612. }
  613. // Detect and process left / right movement on the controller
  614. bool CPlayerInfo::Move_LeftRight(const float deltaTime, const bool direction, const float speedMultiplier)
  615. {
  616. Vector3 viewVector = target - position;
  617. Vector3 rightUV;
  618. if (direction)
  619. {
  620. rightUV = (viewVector.Normalized()).Cross(up);
  621. rightUV.y = 0;
  622. rightUV.Normalize();
  623. position -= rightUV * (float)m_dSpeed * deltaTime;
  624. // Constrain the position
  625. Constrain();
  626. // Update the target
  627. target = position + viewVector;
  628. return true;
  629. }
  630. else
  631. {
  632. rightUV = (viewVector.Normalized()).Cross(up);
  633. rightUV.y = 0;
  634. rightUV.Normalize();
  635. position += rightUV * (float)m_dSpeed * deltaTime;
  636. // Constrain the position
  637. Constrain();
  638. // Update the target
  639. target = position + viewVector;
  640. return true;
  641. }
  642. return false;
  643. }
  644.  
  645. // Detect and process look up / down on the controller
  646. bool CPlayerInfo::Look_UpDown(const float deltaTime, const bool direction, const float speedMultiplier)
  647. {
  648. if (speedMultiplier == 0.0f)
  649. return false;
  650.  
  651. Vector3 viewUV = (target - position).Normalized();
  652. Vector3 rightUV;
  653.  
  654. float pitch = (float)(-m_dSpeed * speedMultiplier * (float)deltaTime);
  655. rightUV = viewUV.Cross(up);
  656. rightUV.y = 0;
  657. rightUV.Normalize();
  658. up = rightUV.Cross(viewUV).Normalized();
  659. Mtx44 rotation;
  660. rotation.SetToRotation(pitch, rightUV.x, rightUV.y, rightUV.z);
  661. viewUV = rotation * viewUV;
  662. target = position + viewUV;
  663.  
  664. return true;
  665. }
  666. // Detect and process look left / right on the controller
  667. bool CPlayerInfo::Look_LeftRight(const float deltaTime, const bool direction, const float speedMultiplier)
  668. {
  669. if (speedMultiplier == 0.0f)
  670. return false;
  671.  
  672. Vector3 viewUV = (target - position).Normalized();
  673. Vector3 rightUV;
  674.  
  675. float yaw = (float)-m_dSpeed * speedMultiplier * (float)deltaTime;
  676. Mtx44 rotation;
  677. rotation.SetToRotation(yaw, 0, 1, 0);
  678. viewUV = rotation * viewUV;
  679. target = position + viewUV;
  680. rightUV = viewUV.Cross(up);
  681. rightUV.y = 0;
  682. rightUV.Normalize();
  683. up = rightUV.Cross(viewUV).Normalized();
  684.  
  685. return true;
  686. }
  687.  
  688. // Stop sway
  689. bool CPlayerInfo::StopSway(const float deltaTime)
  690. {
  691. m_bCameraSwayDirection = false;
  692. m_fCameraSwayAngle = 0.0f;
  693. up = Vector3(0.0f, 1.0f, 0.0f);
  694. return true;
  695. }
  696.  
  697. // Reload current weapon
  698. bool CPlayerInfo::ReloadWeapon(void)
  699. {
  700. if (weaponManager[m_iCurrentWeapon])
  701. {
  702. weaponManager[m_iCurrentWeapon]->Reload();
  703. return true;
  704. }
  705. return false;
  706. }
  707.  
  708. // Change current weapon
  709. bool CPlayerInfo::ChangeWeapon(void)
  710. {
  711. if (MouseController::GetInstance()->GetMouseScrollStatus(MouseController::SCROLL_TYPE_YOFFSET) != m_iCurrentWeapon)
  712. {
  713. if ((MouseController::GetInstance()->GetMouseScrollStatus(MouseController::SCROLL_TYPE_YOFFSET) >= 0) &&
  714. (MouseController::GetInstance()->GetMouseScrollStatus(MouseController::SCROLL_TYPE_YOFFSET) < m_iNumOfWeapon))
  715. {
  716. m_iCurrentWeapon = MouseController::GetInstance()->GetMouseScrollStatus(MouseController::SCROLL_TYPE_YOFFSET);
  717. }
  718. }
  719. return true;
  720. }
  721.  
  722. // Get Current Weapon
  723. int CPlayerInfo::GetWeapon(void) const
  724. {
  725. return m_iCurrentWeapon;
  726. }
  727.  
  728. // Discharge Primary Weapon
  729. bool CPlayerInfo::DischargePrimaryWeapon(const float deltaTime)
  730. {
  731. //if (primaryWeapon)
  732. // primaryWeapon->Discharge(position, target, this);
  733.  
  734. if (weaponManager[m_iCurrentWeapon])
  735. {
  736. weaponManager[m_iCurrentWeapon]->Discharge(position, target, this);
  737. return true;
  738. }
  739.  
  740. return false;
  741. }
  742.  
  743. // Discharge Secondary Weapon
  744. bool CPlayerInfo::DischargeSecondaryWeapon(const float deltaTime)
  745. {
  746. if (secondaryWeapon)
  747. {
  748. secondaryWeapon->Discharge(position, target, this);
  749. return true;
  750. }
  751.  
  752. return false;
  753. }
  754. #include "../CameraEffects/CameraEffects.h"
  755.  
  756. // Constrain the position within the borders
  757. void CPlayerInfo::Constrain(void)
  758. {
  759. // Constrain player within the boundary
  760. if (position.x > maxBoundary.x - 1.0f)
  761. {
  762. position.x = maxBoundary.x - 1.0f;
  763. CCameraEffects::GetInstance()->SetStatus_BloodScreen(true);
  764. }
  765. if (position.y > maxBoundary.y - 1.0f)
  766. {
  767. position.y = maxBoundary.y - 1.0f;
  768. m_dJumpSpeed = 0.0;
  769. m_bJumpUpwards = false;
  770. m_dFallSpeed = 0.0;
  771. m_bFallDownwards = true;
  772. m_dElapsedTime = 0.0;
  773. }
  774. if (position.z > maxBoundary.z - 1.0f)
  775. {
  776. position.z = maxBoundary.z - 1.0f;
  777. CCameraEffects::GetInstance()->SetStatus_BloodScreen(true);
  778. }
  779. if (position.x < minBoundary.x + 1.0f)
  780. {
  781. position.x = minBoundary.x + 1.0f;
  782. CCameraEffects::GetInstance()->SetStatus_BloodScreen(true);
  783. }
  784. if (position.y < minBoundary.y + 1.0f)
  785. position.y = minBoundary.y + 1.0f;
  786. if (position.z < minBoundary.z + 1.0f)
  787. {
  788. position.z = minBoundary.z + 1.0f;
  789. CCameraEffects::GetInstance()->SetStatus_BloodScreen(true);
  790. }
  791.  
  792. // if the player is not jumping nor falling, then adjust his y position
  793. if ((m_bJumpUpwards == false) && (m_bFallDownwards == false))
  794. {
  795. // if the y position is not equal to terrain height at that position,
  796. // then update y position to the terrain height
  797. if (position.y != m_pTerrain->GetTerrainHeight(position))
  798. position.y = m_pTerrain->GetTerrainHeight(position);
  799.  
  800. Vector3 viewDirection = target - position;
  801. switch (theCurrentPosture)
  802. {
  803. case STAND:
  804. position.y = m_pTerrain->GetTerrainHeight(Vector3(position.x, 0.0f, position.z));
  805. target = position + viewDirection;
  806. break;
  807. case CROUCH:
  808. position.y = m_pTerrain->GetTerrainHeight(Vector3(position.x, 0.0f, position.z)) - 5.0f;
  809. target.y = position.y + viewDirection.y;
  810. break;
  811. case PRONE:
  812. position.y = m_pTerrain->GetTerrainHeight(Vector3(position.x, 0.0f, position.z)) - 8.0f;
  813. target.y = position.y + viewDirection.y;
  814. break;
  815. default:
  816. break;
  817. }
  818. }
  819. }
  820.  
  821. void CPlayerInfo::AttachCamera(FPSCamera* _cameraPtr)
  822. {
  823. attachedCamera = _cameraPtr;
  824. }
  825.  
  826. void CPlayerInfo::DetachCamera()
  827. {
  828. attachedCamera = nullptr;
  829. }
  830.  
  831. void CPlayerInfo::ChangePosture()
  832. {
  833. theCurrentPosture = (CURRENT_POSTURE)(theCurrentPosture + 1);
  834. if (theCurrentPosture == NUM_POSTURE)
  835. theCurrentPosture = STAND;
  836.  
  837. Vector3 viewDirection = target - position;
  838. switch (theCurrentPosture)
  839. {
  840. case STAND:
  841. position.y = m_pTerrain->GetTerrainHeight(Vector3(position.x, 0.0f, position.z));
  842. target = position + viewDirection;
  843. break;
  844. case CROUCH:
  845. position.y = m_pTerrain->GetTerrainHeight(Vector3(position.x, 0.0f, position.z)) - 5.0f;
  846. target = position + viewDirection;
  847. break;
  848. case PRONE:
  849. position.y = m_pTerrain->GetTerrainHeight(Vector3(position.x, 0.0f, position.z)) - 8.0f;
  850. target = position + viewDirection;
  851. break;
  852. default:
  853. break;
  854. }
  855. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement