Я пересоздал репозиторий из-за большого количества мусора в прошлом
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#include "GameInstance.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "Core/CoreInstance.h"
|
||||
#include "Core/RenderEngine.h"
|
||||
#include "Game/WorldFactory.h"
|
||||
#include "SaveMap/SaveMap.h"
|
||||
#include "Game/World/World.h"
|
||||
|
||||
extern std::vector<WorldFactory> world_factories;
|
||||
|
||||
GameInstance::GameInstance(CoreInstance& core) : core(core)
|
||||
{
|
||||
render_engine_ = std::make_unique<RenderEngine>(core.getGameName(), 0, 0, 0, 0, 0, 0);
|
||||
render_engine_->onCloseWindow.bind(this, &GameInstance::quit);
|
||||
const std::string& base_world = core.getBaseWorldName();
|
||||
|
||||
// Init directories
|
||||
std::filesystem::create_directories(std::filesystem::path("./Resources"));
|
||||
|
||||
std::ifstream file("./Worlds/" + base_world + ".world");
|
||||
if (file.fail())
|
||||
throw std::runtime_error("Can't open world file");
|
||||
|
||||
std::string data;
|
||||
std::getline(file, data);
|
||||
|
||||
for (auto& factories : world_factories)
|
||||
{
|
||||
if (factories.world_name == base_world)
|
||||
{
|
||||
world = factories.factory(*this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (world == nullptr)
|
||||
throw std::runtime_error("Can't find world");
|
||||
|
||||
world->load(std::make_shared<SaveMap>(data));
|
||||
}
|
||||
|
||||
GameInstance::~GameInstance()
|
||||
{
|
||||
delete world;
|
||||
}
|
||||
|
||||
void GameInstance::start()
|
||||
{
|
||||
render_engine_->start();
|
||||
world->BeginPlay();
|
||||
|
||||
auto first = std::chrono::steady_clock::now();
|
||||
|
||||
|
||||
while (is_running)
|
||||
{
|
||||
auto second = std::chrono::steady_clock::now();
|
||||
std::chrono::milliseconds delta_time = std::chrono::duration_cast<std::chrono::milliseconds>(second - first);
|
||||
world->Tick(delta_time.count() / 1000.0);
|
||||
first = second;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
}
|
||||
|
||||
void GameInstance::quit()
|
||||
{
|
||||
is_running = false;
|
||||
}
|
||||
Reference in New Issue
Block a user