ADD: Update ModelStages based in ModelParams
This commit is contained in:
parent
2d453d05c1
commit
f301cede21
@ -16,13 +16,13 @@
|
||||
|
||||
class ModelComponent: public Component {
|
||||
public:
|
||||
explicit ModelComponent(const ModelStages& modelStages): currentStage(0) {
|
||||
for (const auto& stage : modelStages.getModelStages()) {
|
||||
ModelComponent(const std::shared_ptr<ModelStages> &modelStages, std::string modelname): modelName(std::move(modelname)), currentStage(0) {
|
||||
for (const auto& stage : modelStages->getModelStages()) {
|
||||
stages.insert(stage.getStageName(), stage.getModelOfModelStage());
|
||||
}
|
||||
};
|
||||
|
||||
explicit ModelComponent(std::shared_ptr<TexturedModel> model): currentStage(0) {
|
||||
ModelComponent(std::shared_ptr<TexturedModel> model, std::string modelname): modelName(std::move(modelname)), currentStage(0) {
|
||||
stages["default"] = std::move(model);
|
||||
}
|
||||
|
||||
@ -38,7 +38,11 @@ public:
|
||||
[[nodiscard]] std::shared_ptr<TexturedModel> getActiveModel() const{
|
||||
return stages.atIndex(currentStage);
|
||||
}
|
||||
|
||||
[[nodiscard]] const std::string& getModelName() const {return modelName;}
|
||||
|
||||
private:
|
||||
std::string modelName;
|
||||
IndexedMap<std::string, std::shared_ptr<TexturedModel>> stages;
|
||||
size_t currentStage;
|
||||
};
|
||||
|
||||
@ -4,12 +4,20 @@
|
||||
|
||||
#include "RenderSystem.h"
|
||||
|
||||
#include "ModelStateComponent.h"
|
||||
#include "../../renderer/loader/AssetManager.h"
|
||||
|
||||
void RenderSystem::render(EntityManager &entityManager, MasterRenderer &renderer) {
|
||||
for (auto id : entityManager.getAllEntities()) {
|
||||
|
||||
auto transform = entityManager.getComponent<TransformComponent>(id);
|
||||
auto model = entityManager.getComponent<ModelComponent>(id);
|
||||
auto tileRenderingComponent = entityManager.getComponent<TileRenderComponent>(id);
|
||||
auto modelStateComponent = entityManager.getComponent<ModelStateComponent>(id);
|
||||
|
||||
if (modelStateComponent) {
|
||||
updateModelStage(model.get(), modelStateComponent.get());
|
||||
}
|
||||
|
||||
if (!transform || !model) continue;
|
||||
|
||||
@ -22,3 +30,22 @@ void RenderSystem::render(EntityManager &entityManager, MasterRenderer &renderer
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void RenderSystem::updateModelStage(ModelComponent *model, ModelStateComponent *state) {
|
||||
std::string bestStage = "default";
|
||||
|
||||
auto modelStages = AssetManager::getModelStages(model->getModelName());
|
||||
|
||||
|
||||
for (const auto& stage : modelStages->getModelStages()) {
|
||||
auto it = state->params.find(stage.getCondition().key);
|
||||
if (it == state->params.end()) continue;
|
||||
|
||||
float value = it->second;
|
||||
if (value >= stage.getCondition().min && value < stage.getCondition().max) {
|
||||
bestStage = stage.getStageName();
|
||||
}
|
||||
}
|
||||
|
||||
model->updateStage(bestStage);
|
||||
}
|
||||
|
||||
@ -5,12 +5,15 @@
|
||||
#ifndef RENDERSYSTEM_H
|
||||
#define RENDERSYSTEM_H
|
||||
#include "EntityManager.h"
|
||||
#include "ModelStateComponent.h"
|
||||
#include "../../renderer/MasterRenderer.h"
|
||||
|
||||
|
||||
class RenderSystem {
|
||||
public:
|
||||
void render(EntityManager &entityManager, MasterRenderer &renderer);
|
||||
static void render(EntityManager &entityManager, MasterRenderer &renderer);
|
||||
|
||||
static void updateModelStage(ModelComponent* model, ModelStateComponent* state);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@ -5,19 +5,22 @@
|
||||
#ifndef ENTITY_H
|
||||
#define ENTITY_H
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "../../renderer/model/TexturedModel.h"
|
||||
#include "glm/vec3.hpp"
|
||||
|
||||
class Entity {
|
||||
private:
|
||||
|
||||
std::shared_ptr<TexturedModel> model;
|
||||
glm::vec3 position;
|
||||
float rotX, rotY, rotZ;
|
||||
float scale;
|
||||
|
||||
public:
|
||||
Entity(std::shared_ptr<TexturedModel> model, const glm::vec3 position, const float rotX, const float rotY, const float rotZ, const float scale) : model(std::move(model)), position(position), rotX(rotX), rotY(rotY), rotZ(rotZ), scale(scale) {};
|
||||
std::string modelName;
|
||||
Entity(std::shared_ptr<TexturedModel> model, const glm::vec3 position, const float rotX, const float rotY, const float rotZ, const float scale, std::string modelname="") : modelName(std::move(modelname)), model(std::move(model)), position(position), rotX(rotX), rotY(rotY), rotZ(rotZ), scale(scale) {};
|
||||
|
||||
void increasePosition(const float dx, const float dy, const float dz) {position += glm::vec3(dx, dy, dz);} //( dx, dy, dz
|
||||
void increaseRotation(const float dx, const float dy, const float dz) {rotX += dx; rotY += dy; rotZ += dz;}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
|
||||
#include "GameLayer.h"
|
||||
|
||||
#include "../engine/core/ECS/ModelStateComponent.h"
|
||||
#include "../engine/platform/glfw/InputManager.h"
|
||||
#include "../engine/renderer/Renderer.h"
|
||||
#include "../engine/renderer/loader/OBJLoader.h"
|
||||
@ -43,14 +44,7 @@ void GameLayer::onAttach()
|
||||
MapGenerator::generateHexMap(*map, 10,10, *entityManager);
|
||||
//printf("Generated Terrain with %lu Tiles!\n", map->tiles.size());
|
||||
|
||||
for (const auto& entity : mapEntities) {
|
||||
EntityID entityID = entityManager->createEntity();
|
||||
glm::vec3 pos = entity->getPosition();
|
||||
glm::vec3 rot = glm::vec3(entity->getRotX(), entity->getRotY(), entity->getRotZ());
|
||||
float scale = entity->getScale();
|
||||
entityManager->addComponent(entityID, std::make_shared<TransformComponent>(pos, rot, scale));
|
||||
entityManager->addComponent(entityID, std::make_shared<ModelComponent>(entity->getModel()));
|
||||
}
|
||||
|
||||
|
||||
const std::string stoneMasonPath = "assets/buildings/stone_mason/";
|
||||
|
||||
@ -71,11 +65,14 @@ void GameLayer::onAttach()
|
||||
auto modelStages = AssetManager::getModelStages("stoneMason");
|
||||
|
||||
auto transformComponent = std::make_shared<TransformComponent>(glm::vec3(0,0,0), glm::vec3(0), 1.f);
|
||||
auto modelComponent = std::make_shared<ModelComponent>(*modelStages);
|
||||
auto modelComponent = std::make_shared<ModelComponent>(modelStages, "stoneMason");
|
||||
auto modelStateComponent = std::make_shared<ModelStateComponent>();
|
||||
modelStateComponent->params["fillRatio"] = 0.f;
|
||||
modelComponent->updateStage("stone_mason_empty");
|
||||
EntityID entityID = entityManager->createEntity();
|
||||
entityManager->addComponent(entityID, transformComponent);
|
||||
entityManager->addComponent(entityID, modelComponent);
|
||||
entityManager->addComponent(entityID, modelStateComponent);
|
||||
testEntity = entityID;
|
||||
}
|
||||
|
||||
@ -105,8 +102,8 @@ void GameLayer::onUpdate()
|
||||
}
|
||||
|
||||
if (InputManager::isKeyPressed(GLFW_KEY_U)) {
|
||||
auto model_component = entityManager->getComponent<ModelComponent>(testEntity);
|
||||
model_component->updateStage("stone_mason_full");
|
||||
auto modelStateComponent = entityManager->getComponent<ModelStateComponent>(testEntity);
|
||||
modelStateComponent->params["fillRatio"] += 0.1f;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -37,19 +37,22 @@ void MapGenerator::generateHexMap(Map &map, int width, int height, EntityManager
|
||||
RessourceType resourceType = RessourceType::NONE;
|
||||
std::shared_ptr<TexturedModel> hexModel = AssetManager::getModel("hexModelNone");
|
||||
std::vector<EntityID> tileEntities;
|
||||
std::string modelName = "hexModelNone";
|
||||
if (randomValue < 0.5f) {
|
||||
resourceType = RessourceType::WOOD;
|
||||
ForestTileGenerator forestTileGenerator;
|
||||
tileEntities = forestTileGenerator.generateHexTile(entityManager, random, worldPos);
|
||||
hexModel = AssetManager::getModel("hexModelWood");
|
||||
modelName = "hexModelWood";
|
||||
} else if (randomValue < 0.75f) {
|
||||
resourceType = RessourceType::STONE;
|
||||
hexModel = AssetManager::getModel("hexModelStone");
|
||||
modelName = "hexModelStone";
|
||||
}
|
||||
|
||||
EntityID entityID = entityManager.createEntity();
|
||||
const auto transformComponent = std::make_shared<TransformComponent>(worldPos, glm::vec3(0), 1.0f);
|
||||
const auto modelComponent = std::make_shared<ModelComponent>(hexModel);
|
||||
const auto modelComponent = std::make_shared<ModelComponent>(hexModel, modelName);
|
||||
const auto tileHighlightComponent = std::make_shared<TileRenderComponent>(false);
|
||||
const auto tileGameplayComponent = std::make_shared<TileGameplayComponent>(q, r, radius, resourceType);
|
||||
tileGameplayComponent->entitiesOnTile = tileEntities;
|
||||
|
||||
@ -13,7 +13,7 @@ EntityID BuildingFactory::create(EntityManager &em, const BuildingDefinition &de
|
||||
|
||||
EntityID e = em.createEntity();
|
||||
|
||||
em.addComponent(e, std::make_shared<ModelComponent>(AssetManager::getModel(def.model)));
|
||||
em.addComponent(e, std::make_shared<ModelComponent>(AssetManager::getModel(def.model), def.model));
|
||||
|
||||
if (def.type == BuildingType::FOREST_HUT) {
|
||||
em.addComponent(e, std::make_shared<TransformComponent>(
|
||||
|
||||
@ -47,7 +47,7 @@ std::vector<EntityID> ForestTileGenerator::generateHexTile(EntityManager& em, Ra
|
||||
int treeModelIndex = random.randomInt(0, static_cast<int>(treeModels.size()-1));
|
||||
|
||||
const auto transformComponent = std::make_shared<TransformComponent>(treePos, glm::vec3(0), 1.f);
|
||||
const auto modelComponent = std::make_shared<ModelComponent>(treeModels[treeModelIndex]);
|
||||
const auto modelComponent = std::make_shared<ModelComponent>(treeModels[treeModelIndex], "hexModelWood");
|
||||
|
||||
const EntityID entityID = em.createEntity();
|
||||
em.addComponent(entityID, transformComponent);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user