76 lines
1.5 KiB
C++
76 lines
1.5 KiB
C++
#include "GameInstance.h"
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <chrono>
|
|
#include <thread>
|
|
|
|
#include "Core/CoreInstance.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)
|
|
{
|
|
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()
|
|
{
|
|
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::stop()
|
|
{
|
|
is_running = false;
|
|
}
|
|
|
|
void GameInstance::quit()
|
|
{
|
|
stop();
|
|
core.quit();
|
|
}
|