|
@ -17,23 +17,24 @@ |
|
|
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
|
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
#include "model-system.hpp"
|
|
|
|
|
|
|
|
|
#include "render-system.hpp"
|
|
|
#include "entity/components/transform-component.hpp"
|
|
|
#include "entity/components/transform-component.hpp"
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
using namespace ecs; |
|
|
using namespace ecs; |
|
|
|
|
|
|
|
|
model_system::model_system(entt::registry& registry, ::scene& scene): |
|
|
|
|
|
entity_system(registry), |
|
|
|
|
|
scene(scene) |
|
|
|
|
|
|
|
|
render_system::render_system(entt::registry& registry): |
|
|
|
|
|
entity_system(registry) |
|
|
{ |
|
|
{ |
|
|
registry.on_construct<model_component>().connect<&model_system::on_model_construct>(this); |
|
|
|
|
|
registry.on_replace<model_component>().connect<&model_system::on_model_replace>(this); |
|
|
|
|
|
registry.on_destroy<model_component>().connect<&model_system::on_model_destroy>(this); |
|
|
|
|
|
|
|
|
registry.on_construct<model_component>().connect<&render_system::on_model_construct>(this); |
|
|
|
|
|
registry.on_replace<model_component>().connect<&render_system::on_model_replace>(this); |
|
|
|
|
|
registry.on_destroy<model_component>().connect<&render_system::on_model_destroy>(this); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void model_system::update(double t, double dt) |
|
|
|
|
|
|
|
|
void render_system::update(double t, double dt) |
|
|
{ |
|
|
{ |
|
|
registry.view<transform_component, model_component>().each( |
|
|
|
|
|
|
|
|
registry.view<transform_component, model_component>().each |
|
|
|
|
|
( |
|
|
[this](auto entity, auto& transform, auto& model) |
|
|
[this](auto entity, auto& transform, auto& model) |
|
|
{ |
|
|
{ |
|
|
model_instance* instance = model_instances[entity]; |
|
|
model_instance* instance = model_instances[entity]; |
|
@ -46,10 +47,21 @@ void model_system::update(double t, double dt) |
|
|
instance->update_tweens(); |
|
|
instance->update_tweens(); |
|
|
transform.warp = false; |
|
|
transform.warp = false; |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void render_system::add_layer(::scene* layer) |
|
|
|
|
|
{ |
|
|
|
|
|
layers.push_back(layer); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void model_system::update_model_and_materials(entt::entity entity, model_component& model) |
|
|
|
|
|
|
|
|
void render_system::remove_layers() |
|
|
|
|
|
{ |
|
|
|
|
|
layers.clear(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void render_system::update_model_and_materials(entt::entity entity, model_component& model) |
|
|
{ |
|
|
{ |
|
|
if (auto model_it = model_instances.find(entity); model_it != model_instances.end()) |
|
|
if (auto model_it = model_instances.find(entity); model_it != model_instances.end()) |
|
|
{ |
|
|
{ |
|
@ -60,28 +72,42 @@ void model_system::update_model_and_materials(entt::entity entity, model_compone |
|
|
{ |
|
|
{ |
|
|
model_it->second->set_material(material_it->first, material_it->second); |
|
|
model_it->second->set_material(material_it->first, material_it->second); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Add model instance to its specified layers
|
|
|
|
|
|
for (std::size_t i = 0; i < std::min<std::size_t>(layers.size(), (sizeof(model.layers) << 3)); ++i) |
|
|
|
|
|
{ |
|
|
|
|
|
layers[i]->remove_object(model_it->second); |
|
|
|
|
|
|
|
|
|
|
|
if ((model.layers >> i) & 1) |
|
|
|
|
|
{ |
|
|
|
|
|
layers[i]->add_object(model_it->second); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void model_system::on_model_construct(entt::registry& registry, entt::entity entity, model_component& model) |
|
|
|
|
|
|
|
|
void render_system::on_model_construct(entt::registry& registry, entt::entity entity, model_component& model) |
|
|
{ |
|
|
{ |
|
|
::model_instance* model_instance = new ::model_instance(); |
|
|
|
|
|
scene.add_object(model_instance); |
|
|
|
|
|
|
|
|
::model_instance* model_instance = new ::model_instance(); |
|
|
model_instances[entity] = model_instance; |
|
|
model_instances[entity] = model_instance; |
|
|
|
|
|
|
|
|
update_model_and_materials(entity, model); |
|
|
update_model_and_materials(entity, model); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void model_system::on_model_replace(entt::registry& registry, entt::entity entity, model_component& model) |
|
|
|
|
|
|
|
|
void render_system::on_model_replace(entt::registry& registry, entt::entity entity, model_component& model) |
|
|
{ |
|
|
{ |
|
|
update_model_and_materials(entity, model); |
|
|
update_model_and_materials(entity, model); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void model_system::on_model_destroy(entt::registry& registry, entt::entity entity) |
|
|
|
|
|
|
|
|
void render_system::on_model_destroy(entt::registry& registry, entt::entity entity) |
|
|
{ |
|
|
{ |
|
|
if (auto it = model_instances.find(entity); it != model_instances.end()) |
|
|
if (auto it = model_instances.find(entity); it != model_instances.end()) |
|
|
{ |
|
|
{ |
|
|
scene.remove_object(it->second); |
|
|
|
|
|
|
|
|
// Remove model instance from all layers
|
|
|
|
|
|
for (std::size_t i = 0; i < layers.size(); ++i) |
|
|
|
|
|
{ |
|
|
|
|
|
layers[i]->remove_object(it->second); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
delete it->second; |
|
|
delete it->second; |
|
|
model_instances.erase(it); |
|
|
model_instances.erase(it); |
|
|
} |
|
|
} |