Game.cpp 1.3 KB

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