CrystalPoint.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 (state)
  49. {
  50. if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
  51. worldhandler->PreviousWorld();
  52. if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT])
  53. worldhandler->NextWorld();
  54. if (keyboardState.keys[27])
  55. state = false;
  56. Player* player = Player::getInstance();
  57. //testing code
  58. if (keyboardState.keys['u'])
  59. player->HpUp(1);
  60. if (keyboardState.keys['i'])
  61. player->HpDown(1);
  62. if (keyboardState.keys['o'])
  63. player->XpUp(1);
  64. player->rotation.y += mouseOffset.x / 10.0f;
  65. player->rotation.x += mouseOffset.y / 10.0f;
  66. if (player->rotation.x > 90)
  67. player->rotation.x = 90;
  68. if (player->rotation.x < -90)
  69. player->rotation.x = -90;
  70. float speed = 10;
  71. Vec3f oldPosition = player->position;
  72. if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
  73. if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
  74. if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
  75. if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
  76. if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
  77. if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
  78. if (!worldhandler->isPlayerPositionValid())
  79. player->position = oldPosition;
  80. player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
  81. worldhandler->update(deltaTime);
  82. }
  83. else
  84. {
  85. menu->update();
  86. cursor->update(cursor->mousePosition + mouseOffset);
  87. }
  88. mouseOffset = Vec2f(0, 0);
  89. prevKeyboardState = keyboardState;
  90. glutPostRedisplay();
  91. sound_system.SetListener(player->position, Vec3f(), Vec3f());
  92. }
  93. void CrystalPoint::buildMenu()
  94. {
  95. Button* start = new Button("Resume", Vec2f(1920 / 2 - 50, 1080 / 2 - 30), 100, 50);
  96. auto toWorld = [](Button* b)
  97. {
  98. state = true;
  99. };
  100. start->addAction(toWorld);
  101. menu->AddMenuElement(start);
  102. Button* test = new Button("Exit", Vec2f(1920 / 2 - 50, 1080 / 2 + 30), 100, 50);
  103. test->addAction([](Button* b)
  104. {
  105. exit(0);
  106. });
  107. menu->AddMenuElement(test);
  108. Text* t = new Text("Pause", Vec2f(1920 / 2 - Util::glutTextWidth("Pause") / 2, 1080 / 2 - 75));
  109. t->setColor(Vec3f(255, 255, 0));
  110. menu->AddMenuElement(t);
  111. }
  112. KeyboardState::KeyboardState()
  113. {
  114. memset(keys, 0, sizeof(keys));
  115. memset(special, 0, sizeof(special));
  116. }