CrystalPoint.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. glutSwapBuffers();
  42. }
  43. void CrystalPoint::update()
  44. {
  45. float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  46. float deltaTime = frameTime - lastFrameTime;
  47. lastFrameTime = frameTime;
  48. if (keyboardState.keys[27] && !prevKeyboardState.keys[27])
  49. state = !state;
  50. Controller *rightcontroller = controller.getRightController();
  51. Controller *leftcontroller = controller.getLeftController();
  52. if (state)
  53. {
  54. Player* player = Player::getInstance();
  55. if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
  56. worldhandler->PreviousWorld();
  57. if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT])
  58. worldhandler->NextWorld();
  59. if (keyboardState.special[GLUT_KEY_UP] && !prevKeyboardState.special[GLUT_KEY_UP])
  60. player->NextRightWeapon();
  61. if (keyboardState.special[GLUT_KEY_DOWN] && !prevKeyboardState.special[GLUT_KEY_DOWN])
  62. player->PreviousLeftWeapon();
  63. if (keyboardState.keys[27])
  64. state = false;
  65. //testing code
  66. if (keyboardState.keys['u'])
  67. player->HpUp(1);
  68. if (keyboardState.keys['i'])
  69. player->HpDown(1);
  70. if (keyboardState.keys['o'])
  71. player->XpUp(1);
  72. player->rotation.y += mouseOffset.x / 10.0f;
  73. player->rotation.x += mouseOffset.y / 10.0f;
  74. float speed = 10;
  75. Vec3f oldPosition = player->position;
  76. if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
  77. if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
  78. if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
  79. if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
  80. if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
  81. if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
  82. if (leftcontroller != nullptr) {
  83. Vec2f *leftControllerJoystick = &leftcontroller->joystick;
  84. if (leftControllerJoystick->y > 0.3 || leftControllerJoystick->y < -0.3) {
  85. player->rotation.x += leftControllerJoystick->y/4;
  86. }
  87. if (leftControllerJoystick->x > 0.3 || leftControllerJoystick->x < -0.3) {
  88. player->rotation.y += leftControllerJoystick->x/4;
  89. }
  90. player->leftWeapon->rotateWeapon(Vec3f(leftcontroller->ypr.y + 140, 0, -leftcontroller->ypr.z));
  91. if(leftcontroller->button && leftcontroller->joystickButton){
  92. state = !state;
  93. }else if(!leftcontroller->lastButton && leftcontroller->button){
  94. leftcontroller->lastButton = leftcontroller->button;
  95. controller.rumble(leftcontroller->controllerId, 100, 200);
  96. player->NextLeftWeapon();
  97. }else if(!leftcontroller->lastJoystickButton && leftcontroller->joystickButton){
  98. leftcontroller->lastJoystickButton = leftcontroller->joystickButton;
  99. player->hit = true;
  100. }
  101. }
  102. if(rightcontroller != nullptr){
  103. Vec2f *rightControllerJoystick = &rightcontroller->joystick;
  104. if (rightControllerJoystick->y > 0.3) {
  105. player->setPosition(270, rightControllerJoystick->y * deltaTime * 2.0f, false);
  106. }
  107. else if (rightControllerJoystick->y < -0.3) {
  108. player->setPosition(90, rightControllerJoystick->y * -1 * deltaTime * 2.0f, false);
  109. }
  110. if (rightControllerJoystick->x > 0.3) {
  111. player->setPosition(180, rightControllerJoystick->x * deltaTime * 2.0f, false);
  112. }
  113. else if (rightControllerJoystick->x < -0.3) {
  114. player->setPosition(0, rightControllerJoystick->x * -1 * deltaTime * 2.0f, false);
  115. }
  116. if(!rightcontroller->lastButton && rightcontroller->button){
  117. rightcontroller->lastButton = rightcontroller->button;
  118. controller.rumble(rightcontroller->controllerId, 100, 200);
  119. player->NextRightWeapon();
  120. }
  121. player->rightWeapon->rotateWeapon(Vec3f(rightcontroller->ypr.y + 140, 0, -rightcontroller->ypr.z));
  122. }
  123. if (player->rotation.x > 90)
  124. player->rotation.x = 90;
  125. if (player->rotation.x < -90)
  126. player->rotation.x = -90;
  127. player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
  128. if (!worldhandler->isPlayerPositionValid())
  129. player->position = oldPosition;
  130. worldhandler->update(deltaTime);
  131. }
  132. else
  133. {
  134. menu->update();
  135. if (leftcontroller != nullptr) {
  136. Vec2f *leftControllerJoystick = &leftcontroller->joystick;
  137. if (leftControllerJoystick->y > 0.3 || leftControllerJoystick->y < -0.3) {
  138. cursor->update(Vec2f(cursor->mousePosition.x,cursor->mousePosition.y+leftControllerJoystick->y ));
  139. }
  140. if (leftControllerJoystick->x > 0.3 || leftControllerJoystick->x < -0.3) {
  141. cursor->update(Vec2f(cursor->mousePosition.x+leftControllerJoystick->x ,cursor->mousePosition.y));
  142. }
  143. if(leftcontroller->button){
  144. cursor->state = 137;
  145. }else if(cursor->state == 137){
  146. cursor->state = GLUT_UP;
  147. }
  148. }
  149. cursor->update(cursor->mousePosition + mouseOffset);
  150. }
  151. mouseOffset = Vec2f(0, 0);
  152. prevKeyboardState = keyboardState;
  153. glutPostRedisplay();
  154. sound_system.SetListener(player->position, Vec3f(), Vec3f());
  155. }
  156. void CrystalPoint::buildMenu()
  157. {
  158. Button* start = new Button("Resume", Vec2f(1920 / 2 - 50, 1080 / 2 - 30), 100, 50);
  159. auto toWorld = [](Button* b)
  160. {
  161. state = true;
  162. };
  163. start->addAction(toWorld);
  164. menu->AddMenuElement(start);
  165. Button* test = new Button("Exit", Vec2f(1920 / 2 - 50, 1080 / 2 + 30), 100, 50);
  166. test->addAction([](Button* b)
  167. {
  168. exit(0);
  169. });
  170. menu->AddMenuElement(test);
  171. Text* t = new Text("Pause", Vec2f(1920 / 2 - Util::glutTextWidth("Pause") / 2, 1080 / 2 - 75));
  172. t->setColor(Vec3f(255, 255, 0));
  173. menu->AddMenuElement(t);
  174. }
  175. KeyboardState::KeyboardState()
  176. {
  177. memset(keys, 0, sizeof(keys));
  178. memset(special, 0, sizeof(special));
  179. }