Move classes Mesh and StaticMesh to UwURenderEngine project. Because they depended from UwURenderEngine
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
#include "Mesh.hpp"
|
||||
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
|
||||
Mesh::Mesh()
|
||||
{
|
||||
SetType(Classes::Mesh);
|
||||
}
|
||||
|
||||
std::shared_ptr<SaveMap> Mesh::save()
|
||||
{
|
||||
return std::make_shared<SaveMap>("Mesh")
|
||||
->SaveString("model_name_", model_name_)
|
||||
->connect_to(Actor::save());
|
||||
}
|
||||
|
||||
void Mesh::load(std::shared_ptr<SaveMap> save)
|
||||
{
|
||||
Actor::load(save->getParent());
|
||||
model_name_ = save->GetString("model_name_");
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "../Actor.hpp"
|
||||
|
||||
GENERATE_META(Mesh)
|
||||
class Mesh : public Actor
|
||||
{
|
||||
std::string model_name_;
|
||||
protected:
|
||||
void SetModelName(const std::string& new_model_name) { model_name_ = new_model_name; }
|
||||
|
||||
public:
|
||||
Mesh();
|
||||
|
||||
#pragma region ISave
|
||||
virtual std::shared_ptr<SaveMap> save() override;
|
||||
virtual void load(std::shared_ptr<SaveMap> save) override;
|
||||
#pragma endregion
|
||||
|
||||
virtual void load_model(const std::string& model_name) = 0;
|
||||
const std::string& GetModelName() const { return model_name_; }
|
||||
};
|
||||
@@ -1,34 +0,0 @@
|
||||
#include "StaticMesh.hpp"
|
||||
|
||||
#include "Core/CoreInstance.hpp"
|
||||
#include "Game/GameInstance.hpp"
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
#include "Game/World/World.hpp"
|
||||
|
||||
StaticMesh::StaticMesh()
|
||||
{
|
||||
SetType(Classes::StaticMesh);
|
||||
}
|
||||
|
||||
std::shared_ptr<SaveMap> StaticMesh::save()
|
||||
{
|
||||
return std::make_shared<SaveMap>("StaticMesh")->connect_to(Mesh::save());
|
||||
}
|
||||
|
||||
void StaticMesh::load(std::shared_ptr<SaveMap> save)
|
||||
{
|
||||
Mesh::load(save->getParent());
|
||||
}
|
||||
|
||||
void StaticMesh::load_model(const std::string& model_name)
|
||||
{
|
||||
model_name_ = model_name;
|
||||
SetModelName(model_name);
|
||||
model_ = GetWorld()->GetGameInstance().GetCurrentModelManager()->LoadModel(model_name);
|
||||
}
|
||||
|
||||
void StaticMesh::OnDestroy()
|
||||
{
|
||||
Mesh::OnDestroy();
|
||||
GetWorld()->GetGameInstance().GetCurrentModelManager()->FreeModel(model_name_);
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "Mesh.hpp"
|
||||
#include "Game/ModelManager.hpp"
|
||||
|
||||
GENERATE_META(StaticMesh)
|
||||
|
||||
class StaticMesh : public Mesh
|
||||
{
|
||||
std::string model_name_;
|
||||
ModelManager::StaticModel* model_ = nullptr;
|
||||
public:
|
||||
StaticMesh();
|
||||
|
||||
#pragma region ISave
|
||||
virtual std::shared_ptr<SaveMap> save() override;
|
||||
virtual void load(std::shared_ptr<SaveMap> save) override;
|
||||
#pragma endregion
|
||||
|
||||
void load_model(const std::string& model_name) override;
|
||||
void OnDestroy() override;
|
||||
};
|
||||
@@ -1,12 +1,10 @@
|
||||
#include "ObjectFactory.hpp"
|
||||
#include "Actors/Mesh/StaticMesh.hpp"
|
||||
|
||||
#include "Game/Actors/Actor.hpp"
|
||||
#include "Math/Vector.hpp"
|
||||
|
||||
std::vector<ObjectFactory> base_object_factories = {
|
||||
GENERATE_FACTORY_OBJECT(Actor)
|
||||
GENERATE_FACTORY_OBJECT(StaticMesh)
|
||||
GENERATE_FACTORY_OBJECT(Vector2D)
|
||||
GENERATE_FACTORY_OBJECT(Vector3D)
|
||||
};
|
||||
@@ -2,36 +2,6 @@
|
||||
|
||||
#include "Log/Log.hpp"
|
||||
|
||||
ModelManager::StaticModel::StaticModel(const std::vector<Voxel>& voxels, std::string name):
|
||||
name_(std::move(name)),
|
||||
voxels_(voxels)
|
||||
{
|
||||
glm::ivec3 sum_moments{0, 0, 0};
|
||||
int sum_masses = 0;
|
||||
|
||||
for (auto& i : voxels_)
|
||||
{
|
||||
sum_moments += glm::ivec3{i.loc.x * i.mass, i.loc.y * i.mass, i.loc.z * i.mass};
|
||||
sum_masses += i.mass;
|
||||
}
|
||||
|
||||
if(sum_masses != 0)
|
||||
sum_moments /= sum_masses;
|
||||
else if (!voxels_.empty())
|
||||
{
|
||||
sum_moments.x /= static_cast<int>(voxels_.size());
|
||||
sum_moments.y /= static_cast<int>(voxels_.size());
|
||||
sum_moments.z /= static_cast<int>(voxels_.size());
|
||||
}
|
||||
|
||||
mass_center_ = sum_moments;
|
||||
}
|
||||
|
||||
ModelManager::StaticModel::~StaticModel()
|
||||
{
|
||||
OnDestroy.Call(name_);
|
||||
}
|
||||
|
||||
ModelManager::owner_counter::owner_counter(const owner_counter& other) : counter(other.counter.load()), model(other.model)
|
||||
{
|
||||
}
|
||||
@@ -49,18 +19,8 @@ unsigned int ModelManager::FNV1aHash(const char* buf)
|
||||
return h_val;
|
||||
}
|
||||
|
||||
void ModelManager::OnDestroySometimeModelCaller(const std::string& name)
|
||||
{
|
||||
Loging::Log("Free model: " + name);
|
||||
OnDestroySometimeModel.Call(name);
|
||||
}
|
||||
|
||||
ModelManager::~ModelManager()
|
||||
{
|
||||
for (const auto& [it, counter] : models)
|
||||
{
|
||||
counter.model->OnDestroy.unbind(this, &ModelManager::OnDestroySometimeModelCaller);
|
||||
}
|
||||
models.clear();
|
||||
}
|
||||
|
||||
@@ -78,14 +38,11 @@ ModelManager::StaticModel* ModelManager::LoadModel(const std::string& name)
|
||||
|
||||
Loging::Log("Load new model: " + name);
|
||||
|
||||
std::vector<Voxel> model_data;
|
||||
// Load model_data
|
||||
owner_counter new_counter;
|
||||
new_counter.counter.store(1);
|
||||
new_counter.model = new StaticModel(model_data, name);
|
||||
new_counter.model->OnDestroy.bind(this, &ModelManager::OnDestroySometimeModelCaller);
|
||||
new_counter.model = new StaticModel();
|
||||
models.try_emplace(hash, new_counter);
|
||||
OnLoadSometimeModel.Call(new_counter.model);
|
||||
return new_counter.model;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,33 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <Delegate/Delegate.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <atomic>
|
||||
|
||||
class ModelManager
|
||||
{
|
||||
public:
|
||||
struct Voxel
|
||||
{
|
||||
glm::vec3 loc, color;
|
||||
int mass;
|
||||
};
|
||||
class StaticModel
|
||||
{
|
||||
std::string name_;
|
||||
std::vector<Voxel> voxels_;
|
||||
glm::vec3 mass_center_;
|
||||
public:
|
||||
Delegate<const std::string&> OnDestroy;
|
||||
|
||||
StaticModel(const std::vector<Voxel>& voxels, std::string name);
|
||||
~StaticModel();
|
||||
|
||||
const std::vector<Voxel>& GetVoxels() const { return voxels_; }
|
||||
glm::vec3 GetMassCenter() const { return mass_center_; }
|
||||
const std::string& GetName() const { return name_; }
|
||||
//DATA
|
||||
};
|
||||
|
||||
struct owner_counter
|
||||
@@ -45,12 +27,7 @@ private:
|
||||
|
||||
static unsigned int FNV1aHash (const char *buf);
|
||||
|
||||
void OnDestroySometimeModelCaller(const std::string& name);
|
||||
|
||||
public:
|
||||
Delegate<std::string> OnDestroySometimeModel;
|
||||
Delegate<StaticModel*> OnLoadSometimeModel;
|
||||
|
||||
~ModelManager();
|
||||
|
||||
StaticModel* LoadModel(const std::string& name);
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#include "RTTI.h"
|
||||
#include "Game/Actors/Actor.hpp"
|
||||
#include "Game/Actors/Mesh/Mesh.hpp"
|
||||
#include "Types/object_ptr.hpp"
|
||||
|
||||
class GameInstance;
|
||||
|
||||
Reference in New Issue
Block a user