Game.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "server/Game.h"
  2. #include "commands/Commands.h"
  3. #include "memory/UniquePointer.h"
  4. #include "raw-terminal/RawReader.h"
  5. #include "server/packets/WorldPackets.h"
  6. #include "server/world/WorldGenerator.h"
  7. #include "utils/List.h"
  8. #include "utils/Logger.h"
  9. static bool running = true;
  10. static RawReader<256, 10> reader{0, "> "};
  11. static List<UniquePointer<World>> worlds;
  12. Clock Game::ticksPerSecond;
  13. BlockRegistry Game::blocks;
  14. void Game::stop() {
  15. running = false;
  16. }
  17. bool Game::isRunning() {
  18. return running;
  19. }
  20. void Game::testWorld() {
  21. worlds.add(new World(blocks));
  22. WorldGenerator wg(blocks, *(worlds[0]));
  23. wg.generate();
  24. }
  25. World& Game::getTestWorld() {
  26. return *(worlds[0]);
  27. }
  28. static void handleCommands() {
  29. if(!reader.canRead()) {
  30. return;
  31. }
  32. const char* s = reader.readLine();
  33. if(s == nullptr) {
  34. return;
  35. }
  36. Commands::Raw raw(s);
  37. Commands::execute(raw);
  38. }
  39. void Game::tick() {
  40. ticksPerSecond.update();
  41. handleCommands();
  42. }
  43. void Game::addPlayer(ServerPlayer& p) {
  44. LOG_INFO("player add");
  45. for(int x = -1; x <= 1; x++) {
  46. for(int z = -1; z <= 1; z++) {
  47. WorldPackets::sendChunk(p, getTestWorld(), x, z);
  48. }
  49. }
  50. }
  51. void Game::removePlayer(ServerPlayer& p) {
  52. LOG_INFO("player remove");
  53. (void)p;
  54. }