You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

134 lines
3.2 KiB

  1. #include "config.h"
  2. #include "world.h"
  3. #include <chrono>
  4. #include <iostream>
  5. #include <string>
  6. #include <thread>
  7. #include <vector>
  8. #include <termios.h> //termios, TCSANOW, ECHO, ICANON
  9. #include <unistd.h> //STDIN_FILENO
  10. constexpr int ticktime = 1000.0 / TICKRATE;
  11. static struct termios oldt, newt;
  12. enum gamestatus { RUNNING, PAUSED, STOPPED } game;
  13. void initTerminal() {
  14. /*tcgetattr gets the parameters of the current terminal
  15. STDIN_FILENO will tell tcgetattr that it should write the settings
  16. of stdin to oldt*/
  17. tcgetattr(STDIN_FILENO, &oldt);
  18. /*now the settings will be copied*/
  19. newt = oldt;
  20. /*ICANON normally takes care that one line at a time will be processed
  21. that means it will return if it sees a "\n" or an EOF or an EOL*/
  22. newt.c_lflag &= ~(ICANON);
  23. /*ECHO - don't print user input */
  24. newt.c_lflag &= ~(ECHO);
  25. /*Those new settings will be set to STDIN
  26. TCSANOW tells tcsetattr to change attributes immediately. */
  27. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  28. }
  29. void restoreTerminal() {
  30. /*restore the old settings*/
  31. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  32. }
  33. void input(Object &o) {
  34. while (game != STOPPED) {
  35. char c = getchar();
  36. if (game == PAUSED) {
  37. if (c == 'p')
  38. game = RUNNING;
  39. continue;
  40. }
  41. bool ok = 1;
  42. switch (c) {
  43. case 'w':
  44. o.changePosition(0, -1);
  45. break;
  46. case 's':
  47. o.changePosition(0, 1);
  48. break;
  49. case 'd':
  50. o.changePosition(1, 0);
  51. break;
  52. case 'a':
  53. o.changePosition(-1, 0);
  54. break;
  55. case 'p':
  56. game = PAUSED;
  57. break;
  58. case 'e':
  59. ok = 0;
  60. break;
  61. default:
  62. break;
  63. }
  64. if (!ok)
  65. break;
  66. }
  67. }
  68. void botAI(Object &o) {
  69. while (game != STOPPED) {
  70. if (game == PAUSED)
  71. continue;
  72. o.changePosition(rand() % 3 - 1, rand() % 3 - 1);
  73. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  74. }
  75. }
  76. void gameloop(World &world) {
  77. while (game != STOPPED) {
  78. if (game == RUNNING)
  79. world.update();
  80. world.print(std::cout);
  81. switch (game) {
  82. case RUNNING:
  83. std::cout << "GAME RUNNING!\n";
  84. break;
  85. case PAUSED:
  86. std::cout << "GAME PAUSED\n";
  87. break;
  88. default:
  89. std::cout << "GAME UNDEFINED\n";
  90. break;
  91. }
  92. std::this_thread::sleep_for(std::chrono::milliseconds(ticktime));
  93. std::cout << "\x1B[2J\x1B[H";
  94. }
  95. }
  96. int main() {
  97. initTerminal();
  98. srand(time(NULL));
  99. game = RUNNING;
  100. World world;
  101. Object &objtest =
  102. world.createObject(Object(Object::Position(3, 3), 3, '*'));
  103. Object &objtestAI =
  104. world.createObject(Object(Object::Position(20, 20), 4, '='));
  105. std::thread m(gameloop, std::ref(world));
  106. std::thread bot(botAI, std::ref(objtestAI));
  107. std::thread in(input, std::ref(objtest));
  108. in.join();
  109. game = STOPPED;
  110. bot.join();
  111. m.join();
  112. restoreTerminal();
  113. }