Minor - ECS implementation pt.2 - V12.3.0
This commit is contained in:
parent
bf1b5d78e5
commit
8f0e583c62
@ -175,7 +175,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool render(float, float, float, float, float);
|
bool render(float, float, float, float, float);
|
||||||
bool render_physics(bool key_left, bool key_right, bool key_up, bool key_down, float delta_time);
|
bool render_physics(float delta_time);
|
||||||
bool update_mouse_strings(int, int, bool);
|
bool update_mouse_strings(int, int, bool);
|
||||||
bool update_fps();
|
bool update_fps();
|
||||||
bool update_render_count_string(int);
|
bool update_render_count_string(int);
|
||||||
|
@ -54,6 +54,18 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Obtenir toutes les entités
|
||||||
|
std::vector<std::shared_ptr<Entity>> GetAllEntities() {
|
||||||
|
std::vector<std::shared_ptr<Entity>> result;
|
||||||
|
result.reserve(m_Entities.size());
|
||||||
|
|
||||||
|
for (const auto& [id, entity] : m_Entities) {
|
||||||
|
result.push_back(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
// Obtenir toutes les entités qui ont un composant spécifique
|
// Obtenir toutes les entités qui ont un composant spécifique
|
||||||
template<typename T>
|
template<typename T>
|
||||||
std::vector<std::shared_ptr<Entity>> GetEntitiesWithComponent() {
|
std::vector<std::shared_ptr<Entity>> GetEntitiesWithComponent() {
|
||||||
|
@ -1791,36 +1791,34 @@ void application_class::culling_thread_function()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Traitement des files d'objets normaux (sans la skybox)
|
// Traitement des files d'objets normaux (sans la skybox)
|
||||||
std::vector<std::reference_wrapper<std::vector<object*>>> queues = {
|
auto all_entity = entity_manager_->GetAllEntities();
|
||||||
std::ref(object_), std::ref(cubes_), std::ref(terrain_chunk_)
|
|
||||||
};
|
|
||||||
|
|
||||||
for (auto& queueRef : queues)
|
for (auto& entity : all_entity)
|
||||||
{
|
{
|
||||||
std::vector<object*>& queue = queueRef.get();
|
std::lock_guard<std::mutex> lock(objects_mutex_);
|
||||||
|
auto renderComponent = entity->GetComponent<ecs::RenderComponent>();
|
||||||
|
if (renderComponent && renderComponent->GetModel())
|
||||||
|
{
|
||||||
|
// Extraction des données de position via le composant TransformComponent
|
||||||
|
auto transformComponent = entity->GetComponent<ecs::TransformComponent>();
|
||||||
|
if (transformComponent)
|
||||||
|
{
|
||||||
|
XMVECTOR transformPosition = transformComponent->GetPosition();
|
||||||
|
float x = XMVectorGetX(transformPosition);
|
||||||
|
float y = XMVectorGetY(transformPosition);
|
||||||
|
float z = XMVectorGetZ(transformPosition);
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(objects_mutex_);
|
// Calcul du rayon approximatif
|
||||||
|
XMVECTOR scale = transformComponent->GetScale();
|
||||||
|
float radius = max(max(XMVectorGetX(scale), XMVectorGetY(scale)), XMVectorGetZ(scale));
|
||||||
|
|
||||||
for (auto* object : queue)
|
// verification du frustum
|
||||||
{
|
bool visible = frustum_culling_.CheckCube(x, y, z, radius, get_frustum_tolerance());
|
||||||
if (!object) continue;
|
renderComponent->SetVisible(visible);
|
||||||
|
}
|
||||||
|
|
||||||
// Extraction des données de position
|
}
|
||||||
XMVECTOR position = object->GetPosition();
|
}
|
||||||
float x = XMVectorGetX(position);
|
|
||||||
float y = XMVectorGetY(position);
|
|
||||||
float z = XMVectorGetZ(position);
|
|
||||||
|
|
||||||
// Calcul du rayon approximatif
|
|
||||||
XMVECTOR scale = object->GetScale();
|
|
||||||
float radius = max(max(XMVectorGetX(scale), XMVectorGetY(scale)), XMVectorGetZ(scale));
|
|
||||||
|
|
||||||
// Vérification du frustum
|
|
||||||
bool visible = frustum_culling_.CheckCube(x, y, z, radius, get_frustum_tolerance());
|
|
||||||
object->SetVisible(visible);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Pause pour éviter de surcharger le CPU
|
// Pause pour éviter de surcharger le CPU
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(16)); // ~60 Hz
|
std::this_thread::sleep_for(std::chrono::milliseconds(16)); // ~60 Hz
|
||||||
@ -1962,83 +1960,16 @@ void application_class::update_skybox_position()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool application_class::render_physics(bool key_left, bool key_right, bool key_up, bool key_down, float delta_time) {
|
bool application_class::render_physics(float delta_time) {
|
||||||
const float maxSpeed = 50.0f; // Limite de vitesse maximale
|
|
||||||
|
|
||||||
for (auto& object : object_) {
|
// update the physical entity if they have the physics component
|
||||||
if (object == nullptr) {
|
auto entities_with_physics = entity_manager_->GetEntitiesWithComponent<ecs::PhysicsComponent>();
|
||||||
Logger::Get().Log("object is null", __FILE__, __LINE__, Logger::LogLevel::Error);
|
for (auto& entity : entities_with_physics) {
|
||||||
return false;
|
auto physicsComponent = entity->GetComponent<ecs::PhysicsComponent>();
|
||||||
|
if (physicsComponent) {
|
||||||
|
// Update the physics component with the input keys and delta time
|
||||||
|
physicsComponent->Update(delta_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!object->IsPhysicsEnabled()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset acceleration for the new frame
|
|
||||||
object->SetAcceleration(XMVectorZero());
|
|
||||||
object->SetGrounded(false);
|
|
||||||
|
|
||||||
for (auto& chunk : terrain_chunk_) {
|
|
||||||
if (!physics_->IsColliding(object, chunk)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
object->SetVelocity(XMVectorSetY(object->GetVelocity(), 0.0f));
|
|
||||||
object->SetAcceleration(XMVectorSetY(object->GetAcceleration(), 0.0f));
|
|
||||||
object->SetGrounded(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& object2 : object_) {
|
|
||||||
if (object->GetId() == object2->GetId()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!physics_->IsColliding(object, object2)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
object->SetVelocity(XMVectorZero());
|
|
||||||
object->SetAcceleration(XMVectorZero());
|
|
||||||
}
|
|
||||||
|
|
||||||
float forceX = 0, forceY = 0, forceZ = 0, forceW = 0;
|
|
||||||
|
|
||||||
if (key_left) {
|
|
||||||
forceX = -40.0f;
|
|
||||||
}
|
|
||||||
if (key_right) {
|
|
||||||
forceX = 40.0f;
|
|
||||||
}
|
|
||||||
if (key_up) {
|
|
||||||
forceY = 40.0f;
|
|
||||||
}
|
|
||||||
if (key_down && !object->IsGrounded()) {
|
|
||||||
forceY = -40.0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
XMVECTOR force = XMVectorSet(forceX, forceY, forceZ, forceW);
|
|
||||||
physics_->AddForce(object, force);
|
|
||||||
|
|
||||||
object->AddVelocity(delta_time);
|
|
||||||
|
|
||||||
XMVECTOR velocity = object->GetVelocity();
|
|
||||||
float speed = XMVectorGetX(XMVector3Length(velocity));
|
|
||||||
if (speed > maxSpeed) {
|
|
||||||
velocity = XMVectorScale(velocity, maxSpeed / speed);
|
|
||||||
object->SetVelocity(velocity);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (object->m_gravityEnabled) {
|
|
||||||
physics_->ApplyGravity(object, delta_time);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (XMVectorGetY(object->GetPosition()) < -30.0f) {
|
|
||||||
XMVECTOR currentPosition = object->GetPosition();
|
|
||||||
object->SetPosition(XMVectorSetY(currentPosition, 50.0f));
|
|
||||||
}
|
|
||||||
|
|
||||||
object->m_previousPosition = object->GetPosition();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -2057,7 +1988,7 @@ void application_class::physics_thread_function()
|
|||||||
lastTime = now;
|
lastTime = now;
|
||||||
|
|
||||||
float deltaTime = 1.0f / static_cast<float>(physics_tick_rate_);
|
float deltaTime = 1.0f / static_cast<float>(physics_tick_rate_);
|
||||||
bool result = render_physics(inputs_.key_left, inputs_.key_right, inputs_.key_up, inputs_.key_down, deltaTime);
|
bool result = render_physics(deltaTime);
|
||||||
if (!result)
|
if (!result)
|
||||||
{
|
{
|
||||||
Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error);
|
Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user