Game.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 "utils/List.h"
  9. #include "utils/Logger.h"
  10. static bool running = true;
  11. static RawReader<256, 10> reader{0, "> "};
  12. Clock Game::ticksPerSecond;
  13. World Game::world;
  14. void Game::stop() {
  15. running = false;
  16. }
  17. bool Game::isRunning() {
  18. return running;
  19. }
  20. void Game::init() {
  21. Block::init();
  22. }
  23. static void handleCommands() {
  24. if(!reader.canRead()) {
  25. return;
  26. }
  27. const char* s = reader.readLine();
  28. if(s == nullptr) {
  29. return;
  30. }
  31. Commands::Raw raw(s);
  32. Commands::execute(raw);
  33. }
  34. void Game::tick() {
  35. ticksPerSecond.update();
  36. world.tick();
  37. handleCommands();
  38. }
  39. void Game::addPlayer(ServerPlayer& p) {
  40. LOG_INFO("player add");
  41. for(int x = -1; x <= 1; x++) {
  42. for(int z = -1; z <= 1; z++) {
  43. WorldPackets::sendChunk(p, world, x, z);
  44. }
  45. }
  46. p.setPosition(Vector3(0.0f, 32.0f, 0.0f));
  47. world.addEntity(&p);
  48. }
  49. void Game::removePlayer(ServerPlayer& p) {
  50. LOG_INFO("player remove");
  51. world.removeEntity(&p);
  52. }