CrystalPoint.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "CrystalPoint.h"
  2. #include <GL/freeglut.h>
  3. #include <cmath>
  4. #include <cstring>
  5. #include "WorldHandler.h"
  6. #include "Player.h"
  7. #include "Cursor.h"
  8. #include "Menu.h"
  9. #include "Text.h"
  10. #include "Vector.h"
  11. #include "Button.h"
  12. int CrystalPoint::width = 0;
  13. int CrystalPoint::height = 0;
  14. SoundSystem CrystalPoint::sound_system;
  15. bool state = false;
  16. void CrystalPoint::init()
  17. {
  18. player = Player::getInstance();
  19. worldhandler = WorldHandler::getInstance();
  20. cursor = Cursor::getInstance();
  21. menu = new Menu();
  22. buildMenu();
  23. lastFrameTime = 0;
  24. state = true;
  25. glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
  26. }
  27. void CrystalPoint::draw()
  28. {
  29. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  30. //Draw world
  31. glEnable(GL_LIGHTING);
  32. glEnable(GL_DEPTH_TEST);
  33. glMatrixMode(GL_PROJECTION);
  34. glLoadIdentity();
  35. gluPerspective(70, width / (float)height, 0.1f, 15000);
  36. glMatrixMode(GL_MODELVIEW);
  37. glLoadIdentity();
  38. worldhandler->draw();
  39. if(!state)
  40. menu->draw();
  41. //cursor->draw();
  42. glutSwapBuffers();
  43. }
  44. void CrystalPoint::update()
  45. {
  46. float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  47. float deltaTime = frameTime - lastFrameTime;
  48. lastFrameTime = frameTime;
  49. if (state)
  50. {
  51. if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
  52. worldhandler->PreviousWorld();
  53. if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT])
  54. worldhandler->NextWorld();
  55. if (keyboardState.keys[27])
  56. state = false;
  57. Player* player = Player::getInstance();
  58. player->rotation.y += mouseOffset.x / 10.0f;
  59. player->rotation.x += mouseOffset.y / 10.0f;
  60. float speed = 10;
  61. Vec3f oldPosition = player->position;
  62. if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
  63. if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
  64. if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
  65. if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
  66. if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
  67. if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
  68. Controller *leftcontroller = controller.getLeftController();
  69. if (leftcontroller != nullptr) {
  70. Vec2f *leftControllerJoystick = &leftcontroller->joystick;
  71. if (leftcontroller->joystickButton) {
  72. controller.rumble(leftcontroller->controllerId, 100, 100);
  73. }
  74. if (leftControllerJoystick->y > 0.3) {
  75. player->setPosition(270, leftControllerJoystick->y * deltaTime, false);
  76. }
  77. else if (leftControllerJoystick->y < -0.3) {
  78. player->setPosition(90, leftControllerJoystick->y * -1 * deltaTime, false);
  79. }
  80. if (leftControllerJoystick->x > 0.3) {
  81. player->setPosition(180, leftControllerJoystick->x * deltaTime, false);
  82. }
  83. else if (leftControllerJoystick->x < -0.3) {
  84. player->setPosition(0, leftControllerJoystick->x * -1 * deltaTime, false);
  85. }
  86. player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z));
  87. }
  88. Controller *rightcontroller = controller.getRightController();
  89. if(rightcontroller != nullptr){
  90. Vec2f *rightControllerJoystick = &rightcontroller->joystick;
  91. if (rightControllerJoystick->y > 0.3 || rightControllerJoystick->y < -0.3) {
  92. player->rotation.x += rightcontroller->joystick.y/2;
  93. }
  94. if (rightControllerJoystick->x > 0.3 || rightControllerJoystick->x < -0.3) {
  95. player->rotation.y += rightcontroller->joystick.x/2;
  96. }
  97. player->rightWeapon->rotateWeapon(Vec3f(rightcontroller->ypr.y + 140, 0, -rightcontroller->ypr.z));
  98. }
  99. if (player->rotation.x > 90)
  100. player->rotation.x = 90;
  101. if (player->rotation.x < -90)
  102. player->rotation.x = -90;
  103. if (!worldhandler->isPlayerPositionValid())
  104. player->position = oldPosition;
  105. player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
  106. worldhandler->update(deltaTime);
  107. }
  108. else
  109. {
  110. menu->update();
  111. cursor->update(cursor->mousePosition + mouseOffset);
  112. }
  113. mouseOffset = Vec2f(0, 0);
  114. prevKeyboardState = keyboardState;
  115. glutPostRedisplay();
  116. sound_system.SetListener(player->position, Vec3f(), Vec3f());
  117. }
  118. void CrystalPoint::buildMenu()
  119. {
  120. Button* start = new Button("Resume", Vec2f(1920 / 2 - 50, 1080 / 2 - 30), 100, 50);
  121. auto toWorld = [](Button* b)
  122. {
  123. state = true;
  124. };
  125. start->addAction(toWorld);
  126. menu->AddMenuElement(start);
  127. Button* test = new Button("Exit", Vec2f(1920 / 2 - 50, 1080 / 2 + 30), 100, 50);
  128. test->addAction([](Button* b)
  129. {
  130. exit(0);
  131. });
  132. menu->AddMenuElement(test);
  133. Text* t = new Text("Pause", Vec2f(1920 / 2 - Util::glutTextWidth("Pause") / 2, 1080 / 2 - 75));
  134. t->setColor(Vec3f(255, 255, 0));
  135. menu->AddMenuElement(t);
  136. }
  137. KeyboardState::KeyboardState()
  138. {
  139. memset(keys, 0, sizeof(keys));
  140. memset(special, 0, sizeof(special));
  141. }