CrystalPoint.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "CrystalPoint.h"
  2. #include <GL/freeglut.h>
  3. #include <cmath>
  4. #include <cstring>
  5. #include "WorldHandler.h"
  6. #include "Player.h"
  7. int CrystalPoint::width = 0;
  8. int CrystalPoint::height = 0;
  9. SoundSystem CrystalPoint::sound_system;
  10. void CrystalPoint::init()
  11. {
  12. player = Player::getInstance();
  13. worldhandler = WorldHandler::getInstance();
  14. //cursor = Cursor::getInstance();
  15. lastFrameTime = 0;
  16. glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
  17. mousePosition = Vec2f(width / 2, height / 2);
  18. }
  19. void CrystalPoint::draw()
  20. {
  21. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  22. //Draw world
  23. glEnable(GL_LIGHTING);
  24. glEnable(GL_DEPTH_TEST);
  25. glMatrixMode(GL_PROJECTION);
  26. glLoadIdentity();
  27. gluPerspective(70, width / (float)height, 0.1f, 15000);
  28. glMatrixMode(GL_MODELVIEW);
  29. glLoadIdentity();
  30. worldhandler->draw();
  31. player->draw();
  32. //cursor->draw();
  33. glutSwapBuffers();
  34. }
  35. void CrystalPoint::update()
  36. {
  37. float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  38. float deltaTime = frameTime - lastFrameTime;
  39. lastFrameTime = frameTime;
  40. if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
  41. worldhandler->PreviousWorld();
  42. if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT])
  43. worldhandler->NextWorld();
  44. if (keyboardState.keys[27])
  45. exit(0);
  46. Player* player = Player::getInstance();
  47. player->rotation.y += mouseOffset.x / 10.0f;
  48. player->rotation.x += mouseOffset.y / 10.0f;
  49. if (player->rotation.x > 90)
  50. player->rotation.x = 90;
  51. if (player->rotation.x < -90)
  52. player->rotation.x = -90;
  53. float speed = 10;
  54. Vec3f oldPosition = player->position;
  55. if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
  56. if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
  57. if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
  58. if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
  59. if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
  60. if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
  61. Controller *leftcontroller = controller.getLeftController();
  62. if (leftcontroller != nullptr) {
  63. Vec2f *leftControllerJoystick = &leftcontroller->joystick;
  64. if (leftcontroller->joystickButton) {
  65. controller.rumble(leftcontroller->controllerId, 100, 100);
  66. }
  67. if (leftControllerJoystick->y > 0.3) {
  68. player->setPosition(270, leftControllerJoystick->y*deltaTime, false);
  69. }
  70. else if (leftControllerJoystick->y < -0.3) {
  71. player->setPosition(90, leftControllerJoystick->y*-1 * deltaTime, false);
  72. }
  73. if (leftControllerJoystick->x > 0.3) {
  74. player->setPosition(180, leftControllerJoystick->x*deltaTime, false);
  75. }
  76. else if (leftControllerJoystick->x < -0.3) {
  77. player->setPosition(0, leftControllerJoystick->x*-1 * deltaTime, false);
  78. }
  79. player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z));
  80. }
  81. if (!worldhandler->isPlayerPositionValid())
  82. player->position = oldPosition;
  83. player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
  84. worldhandler->update(deltaTime);
  85. mousePosition = mousePosition + mouseOffset;
  86. //cursor->update(mousePosition);
  87. mouseOffset = Vec2f(0, 0);
  88. prevKeyboardState = keyboardState;
  89. glutPostRedisplay();
  90. sound_system.SetListener(player->position, Vec3f(), Vec3f());
  91. }
  92. KeyboardState::KeyboardState()
  93. {
  94. memset(keys, 0, sizeof(keys));
  95. memset(special, 0, sizeof(special));
  96. }