World.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "common/world/World.h"
  2. #include "common/world/HighMap.h"
  3. #include "utils/Random.h"
  4. World::World(const BlockRegistry& blockRegistry)
  5. : blockRegistry(blockRegistry), blocks(7, 7), dirty(true) {
  6. }
  7. void World::setBlock(int x, int y, int z, const Block& block) {
  8. blocks.set(x, y, z, block.getId());
  9. }
  10. const Block& World::getBlock(int x, int y, int z) const {
  11. return blockRegistry.getBlock(blocks.get(x, y, z));
  12. }
  13. int World::getSize() const {
  14. return blocks.getSize();
  15. }
  16. int World::getHeight() const {
  17. return blocks.getHeight();
  18. }
  19. void World::addPlayer(Player* p) {
  20. players.add(p);
  21. }
  22. void World::preTick() {
  23. for(Player* p : players) {
  24. p->lastPosition = p->position;
  25. p->lastLengthAngle = p->lengthAngle;
  26. p->lastWidthAngle = p->widthAngle;
  27. p->acceleration = Vector3(0.0f, -0.5f, 0.0f);
  28. }
  29. }
  30. void World::tick() {
  31. for(Player* p : players) {
  32. p->acceleration +=
  33. Vector3(-p->velocity[0] * 0.7f, 0.0f, -p->velocity[2] * 0.7f);
  34. p->velocity += p->acceleration;
  35. p->position += p->velocity;
  36. int x = p->position[0];
  37. int y = p->position[1];
  38. int z = p->position[2];
  39. while(getBlock(x, y, z).getId() != 0 ||
  40. getBlock(x - 1, y, z).getId() != 0 ||
  41. getBlock(x + 1, y, z).getId() != 0 ||
  42. getBlock(x, y, z - 1).getId() != 0 ||
  43. getBlock(x, y, z + 1).getId() != 0) {
  44. y++;
  45. }
  46. p->position[1] = y + 1.0f;
  47. }
  48. }