Game.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. static Entity* test = nullptr;
  17. void Game::stop() {
  18. running = false;
  19. }
  20. bool Game::isRunning() {
  21. return running;
  22. }
  23. void Game::init() {
  24. WorldGenerator::generate(world);
  25. }
  26. static void handleCommands() {
  27. if(!reader.canRead()) {
  28. return;
  29. }
  30. const char* s = reader.readLine();
  31. if(s == nullptr) {
  32. return;
  33. }
  34. Commands::Raw raw(s);
  35. Commands::execute(raw);
  36. }
  37. void Game::tick() {
  38. ticksPerSecond.update();
  39. world.tick();
  40. if(test != nullptr) {
  41. EntityUpdatePacket e(*test);
  42. OutPacket out = OutPacket::sequenced(EntityUpdatePacket::getSize());
  43. e.write(out);
  44. GameServer::sendToAll(out);
  45. }
  46. handleCommands();
  47. }
  48. void Game::addPlayer(ServerPlayer& p) {
  49. LOG_INFO("player add");
  50. for(int x = -1; x <= 1; x++) {
  51. for(int z = -1; z <= 1; z++) {
  52. WorldPackets::sendChunk(p, world, x, z);
  53. }
  54. }
  55. p.setPosition(Vector3(0.0f, 2.0f, 0.0f));
  56. test = &p;
  57. world.addEntity(&p);
  58. }
  59. void Game::removePlayer(ServerPlayer& p) {
  60. LOG_INFO("player remove");
  61. test = nullptr;
  62. world.removeEntity(&p);
  63. }