refactor: change pointer parameters to references for Level and Content in various classes

This commit is contained in:
MihailRis
2024-12-25 18:53:53 +03:00
parent ef2c0b2370
commit c1b311f3c4
41 changed files with 258 additions and 251 deletions
+7 -7
View File
@@ -18,19 +18,19 @@
Level::Level(
std::unique_ptr<World> worldPtr,
const Content* content,
const Content& content,
EngineSettings& settings
)
: settings(settings),
world(std::move(worldPtr)),
content(content),
chunks(std::make_unique<GlobalChunks>(this)),
chunks(std::make_unique<GlobalChunks>(*this)),
physics(std::make_unique<PhysicsSolver>(glm::vec3(0, -22.6f, 0))),
events(std::make_unique<LevelEvents>()),
entities(std::make_unique<Entities>(this)),
players(std::make_unique<Players>(this)) {
entities(std::make_unique<Entities>(*this)),
players(std::make_unique<Players>(*this)) {
const auto& worldInfo = world->getInfo();
auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
auto& cameraIndices = content.getIndices(ResourceType::CAMERA);
for (size_t i = 0; i < cameraIndices.size(); i++) {
auto camera = std::make_shared<Camera>();
auto map = cameraIndices.getSavedData(i);
@@ -76,7 +76,7 @@ const World* Level::getWorld() const {
}
void Level::onSave() {
auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
auto& cameraIndices = content.getIndices(ResourceType::CAMERA);
for (size_t i = 0; i < cameraIndices.size(); i++) {
auto& camera = *cameras.at(i);
auto map = dv::object();
@@ -91,7 +91,7 @@ void Level::onSave() {
}
std::shared_ptr<Camera> Level::getCamera(const std::string& name) {
size_t index = content->getIndices(ResourceType::CAMERA).indexOf(name);
size_t index = content.getIndices(ResourceType::CAMERA).indexOf(name);
if (index == ResourceIndices::MISSING) {
return nullptr;
}
+2 -2
View File
@@ -23,7 +23,7 @@ class Level {
const EngineSettings& settings;
std::unique_ptr<World> world;
public:
const Content* const content;
const Content& content;
std::unique_ptr<GlobalChunks> chunks;
std::unique_ptr<Inventories> inventories;
@@ -35,7 +35,7 @@ public:
Level(
std::unique_ptr<World> world,
const Content* content,
const Content& content,
EngineSettings& settings
);
~Level();
+6 -7
View File
@@ -30,7 +30,7 @@ world_load_error::world_load_error(const std::string& message)
World::World(
WorldInfo info,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const Content& content,
const std::vector<ContentPack>& packs
) : info(std::move(info)),
content(content),
@@ -46,12 +46,12 @@ void World::updateTimers(float delta) {
info.totalTime += delta;
}
void World::writeResources(const Content* content) {
void World::writeResources(const Content& content) {
auto root = dv::object();
for (size_t typeIndex = 0; typeIndex < RESOURCE_TYPES_COUNT; typeIndex++) {
auto typeName = to_string(static_cast<ResourceType>(typeIndex));
auto& list = root.list(typeName);
auto& indices = content->resourceIndices[typeIndex];
auto& indices = content.resourceIndices[typeIndex];
for (size_t i = 0; i < indices.size(); i++) {
auto& map = list.object();
map["name"] = indices.getName(i);
@@ -65,10 +65,9 @@ void World::writeResources(const Content* content) {
}
void World::write(Level* level) {
const Content* content = level->content;
level->chunks->saveAll();
info.nextEntityId = level->entities->peekNextID();
wfile->write(this, content);
wfile->write(this, &content);
auto playerFile = level->players->serialize();
files::write_json(wfile->getPlayerFile(), playerFile);
@@ -82,7 +81,7 @@ std::unique_ptr<Level> World::create(
const fs::path& directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const Content& content,
const std::vector<ContentPack>& packs
) {
WorldInfo info {};
@@ -103,7 +102,7 @@ std::unique_ptr<Level> World::create(
std::unique_ptr<Level> World::load(
const std::shared_ptr<WorldFiles>& worldFilesPtr,
EngineSettings& settings,
const Content* content,
const Content& content,
const std::vector<ContentPack>& packs
) {
auto worldFiles = worldFilesPtr.get();
+5 -10
View File
@@ -56,17 +56,17 @@ struct WorldInfo : public Serializable {
class World {
WorldInfo info {};
const Content* const content;
const Content& content;
std::vector<ContentPack> packs;
void writeResources(const Content* content);
void writeResources(const Content& content);
public:
std::shared_ptr<WorldFiles> wfile;
World(
WorldInfo info,
const std::shared_ptr<WorldFiles>& worldFiles,
const Content* content,
const Content& content,
const std::vector<ContentPack>& packs
);
@@ -103,7 +103,7 @@ public:
const fs::path& directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const Content& content,
const std::vector<ContentPack>& packs
);
@@ -118,7 +118,7 @@ public:
static std::unique_ptr<Level> load(
const std::shared_ptr<WorldFiles>& worldFiles,
EngineSettings& settings,
const Content* content,
const Content& content,
const std::vector<ContentPack>& packs
);
@@ -156,9 +156,4 @@ public:
int64_t getNextInventoryId() {
return info.nextInventoryId++;
}
/// @brief Get current world Content instance
const Content* getContent() const {
return content;
}
};
+1 -1
View File
@@ -59,7 +59,7 @@ std::unique_ptr<VoxelFragment> VoxelFragment::create(
std::vector<std::string> blockNames {CORE_AIR};
std::unordered_map<blockid_t, blockid_t> blocksRegistered {{0, 0}};
auto contentIndices = level.content->getIndices();
auto contentIndices = level.content.getIndices();
for (size_t i = 0 ; i < voxels.size(); i++) {
blockid_t id = volVoxels[i].id;
blockid_t index;
+6 -6
View File
@@ -24,7 +24,7 @@ static inline constexpr uint MAX_PARAMETERS = 4;
static inline constexpr uint BASIC_PROTOTYPE_LAYERS = 5;
WorldGenerator::WorldGenerator(
const GeneratorDef& def, const Content* content, uint64_t seed
const GeneratorDef& def, const Content& content, uint64_t seed
)
: def(def),
content(content),
@@ -66,10 +66,10 @@ WorldGenerator::WorldGenerator(
});
for (int i = 0; i < def.structures.size(); i++) {
// pre-calculate rotated structure variants
def.structures[i]->fragments[0]->prepare(*content);
def.structures[i]->fragments[0]->prepare(content);
for (int j = 1; j < 4; j++) {
def.structures[i]->fragments[j] =
def.structures[i]->fragments[j-1]->rotated(*content);
def.structures[i]->fragments[j-1]->rotated(content);
}
}
}
@@ -367,7 +367,7 @@ void WorldGenerator::generatePlants(
int chunkZ,
const Biome** biomes
) {
const auto& indices = content->getIndices()->blocks;
const auto& indices = content.getIndices()->blocks;
util::PseudoRandom plantsRand;
plantsRand.setSeed(chunkX, chunkZ);
@@ -431,7 +431,7 @@ void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) {
std::memset(voxels, 0, sizeof(voxel) * CHUNK_VOL);
const auto& indices = content->getIndices()->blocks;
const auto& indices = content.getIndices()->blocks;
const auto& biomes = prototype.biomes.get();
for (uint z = 0; z < CHUNK_D; z++) {
for (uint x = 0; x < CHUNK_W; x++) {
@@ -531,7 +531,7 @@ void WorldGenerator::generateLine(
voxel* voxels,
int chunkX, int chunkZ
) {
const auto& indices = content->getIndices()->blocks;
const auto& indices = content.getIndices()->blocks;
int cgx = chunkX * CHUNK_W;
int cgz = chunkZ * CHUNK_D;
+2 -2
View File
@@ -50,7 +50,7 @@ class WorldGenerator {
/// @param def generator definition
const GeneratorDef& def;
/// @param content world content
const Content* content;
const Content& content;
/// @param seed world seed
uint64_t seed;
/// @brief Chunk prototypes main storage
@@ -120,7 +120,7 @@ class WorldGenerator {
public:
WorldGenerator(
const GeneratorDef& def,
const Content* content,
const Content& content,
uint64_t seed
);
~WorldGenerator();