More world generators support

This commit is contained in:
Onran
2024-02-21 11:54:10 +09:00
committed by GitHub
parent 399bd9c362
commit 9cc2f93cfd
9 changed files with 209 additions and 30 deletions
+52 -23
View File
@@ -6,6 +6,7 @@
#include "Level.h"
#include "../files/WorldFiles.h"
#include "../world/WorldTypes.h"
#include "../content/Content.h"
#include "../content/ContentLUT.h"
#include "../voxels/Chunk.h"
@@ -34,6 +35,23 @@ World::World(
wfile = new WorldFiles(directory, settings.debug);
}
World::World(
std::string name,
std::string type,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack> packs)
: name(name),
type(type),
seed(seed),
settings(settings),
content(content),
packs(packs) {
wfile = new WorldFiles(directory, settings.debug);
}
World::~World(){
delete wfile;
}
@@ -69,12 +87,13 @@ const float DEF_PLAYER_SPEED = 4.0f;
const int DEF_PLAYER_INVENTORY_SIZE = 40;
Level* World::create(std::string name,
std::string type,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
const std::vector<ContentPack>& packs) {
auto world = new World(name, directory, seed, settings, content, packs);
auto world = new World(name, type, directory, seed, settings, content, packs);
auto inv = std::make_shared<Inventory>(world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE);
auto player = new Player(
glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv
@@ -141,47 +160,57 @@ uint64_t World::getSeed() const {
return seed;
}
std::string World::getType() const {
return type;
}
const std::vector<ContentPack>& World::getPacks() const {
return packs;
}
void World::deserialize(dynamic::Map* root) {
name = root->getStr("name", name);
type = root->getStr("type", type);
seed = root->getInt("seed", seed);
auto verobj = root->map("version");
if (verobj) {
int major=0, minor=-1;
verobj->num("major", major);
verobj->num("minor", minor);
std::cout << "world version: " << major << "." << minor << std::endl;
}
if(type == "") {
type = WorldTypes::getDefaultWorldType();
}
auto timeobj = root->map("time");
if (timeobj) {
timeobj->num("day-time", daytime);
timeobj->num("day-time-speed", daytimeSpeed);
auto verobj = root->map("version");
if (verobj) {
int major=0, minor=-1;
verobj->num("major", major);
verobj->num("minor", minor);
std::cout << "world version: " << major << "." << minor << std::endl;
}
auto timeobj = root->map("time");
if (timeobj) {
timeobj->num("day-time", daytime);
timeobj->num("day-time-speed", daytimeSpeed);
timeobj->num("total-time", totalTime);
}
}
nextInventoryId = root->getNum("next-inventory-id", 2);
}
std::unique_ptr<dynamic::Map> World::serialize() const {
auto root = std::make_unique<dynamic::Map>();
auto root = std::make_unique<dynamic::Map>();
auto& versionobj = root->putMap("version");
versionobj.put("major", ENGINE_VERSION_MAJOR);
versionobj.put("minor", ENGINE_VERSION_MINOR);
auto& versionobj = root->putMap("version");
versionobj.put("major", ENGINE_VERSION_MAJOR);
versionobj.put("minor", ENGINE_VERSION_MINOR);
root->put("name", name);
root->put("seed", seed);
root->put("name", name);
root->put("type", type);
root->put("seed", seed);
auto& timeobj = root->putMap("time");
timeobj.put("day-time", daytime);
timeobj.put("day-time-speed", daytimeSpeed);
timeobj.put("day-time", daytime);
timeobj.put("day-time-speed", daytimeSpeed);
timeobj.put("total-time", totalTime);
root->put("next-inventory-id", nextInventoryId);
return root;
}
}
+15 -1
View File
@@ -28,6 +28,7 @@ public:
class World : Serializable {
std::string name;
std::string type;
uint64_t seed;
EngineSettings& settings;
const Content* const content;
@@ -52,6 +53,15 @@ public:
EngineSettings& settings,
const Content* content,
std::vector<ContentPack> packs);
World(std::string name,
std::string type,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
const Content* content,
std::vector<ContentPack> packs);
~World();
/**
@@ -77,6 +87,7 @@ public:
* Create new world
* @param name internal world name
* @param directory root world directory
* @param type of the world
* @param seed world generation seed
* @param settings current engine settings
* @param content current engine Content instance
@@ -85,6 +96,7 @@ public:
* @return Level instance containing World instance
*/
static Level* create(std::string name,
std::string type,
fs::path directory,
uint64_t seed,
EngineSettings& settings,
@@ -124,6 +136,8 @@ public:
/** Get world generation seed */
uint64_t getSeed() const;
/** Get world type */
std::string getType() const;
/**
* Get vector of all content-packs installed in world
*/
@@ -148,4 +162,4 @@ public:
void deserialize(dynamic::Map *src) override;
};
#endif /* WORLD_WORLD_H_ */
#endif /* WORLD_WORLD_H_ */
+37
View File
@@ -0,0 +1,37 @@
#include "WorldTypes.h"
#include "../voxels/WorldGenerator.h"
#include "../voxels/FlatWorldGenerator.h"
#include "../content/Content.h"
#include <vector>
#include <string>
#include <iostream>
std::vector<std::string> worldTypes;
void fillTypes() {
worldTypes.push_back("Default");
worldTypes.push_back("Flat");
}
std::vector<std::string> WorldTypes::getWorldTypes() {
if(worldTypes.size() == 0) {
fillTypes();
}
return worldTypes;
}
std::string WorldTypes::getDefaultWorldType() {
return getWorldTypes()[0];
}
WorldGenerator* WorldTypes::createWorldGenerator(std::string worldType, const Content* content) {
if(worldType == "Default") {
return new WorldGenerator(content);
} else if(worldType == "Flat") {
return (WorldGenerator*) new FlatWorldGenerator(content);
}
std::cerr << "unknown world type: " << worldType << std::endl;
return nullptr;
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef WORLD_WORLDTYPES_H_
#define WORLD_WORLDTYPES_H_
#include "../voxels/WorldGenerator.h"
#include "../content/Content.h"
#include <vector>
#include <string>
class WorldTypes {
public:
static std::vector<std::string> getWorldTypes();
static std::string getDefaultWorldType();
static WorldGenerator* createWorldGenerator(std::string worldType, const Content* content);
};
#endif /* WORLD_WORLDTYPES_H_ */