Minor - ECS implementation pt.2 - V12.3.0
This commit is contained in:
parent
bf1b5d78e5
commit
8f0e583c62
@ -175,7 +175,7 @@ public:
|
||||
|
||||
private:
|
||||
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_fps();
|
||||
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
|
||||
template<typename T>
|
||||
std::vector<std::shared_ptr<Entity>> GetEntitiesWithComponent() {
|
||||
|
@ -1791,33 +1791,31 @@ void application_class::culling_thread_function()
|
||||
}
|
||||
|
||||
// Traitement des files d'objets normaux (sans la skybox)
|
||||
std::vector<std::reference_wrapper<std::vector<object*>>> queues = {
|
||||
std::ref(object_), std::ref(cubes_), std::ref(terrain_chunk_)
|
||||
};
|
||||
auto all_entity = entity_manager_->GetAllEntities();
|
||||
|
||||
for (auto& queueRef : queues)
|
||||
for (auto& entity : all_entity)
|
||||
{
|
||||
std::vector<object*>& queue = queueRef.get();
|
||||
|
||||
std::lock_guard<std::mutex> lock(objects_mutex_);
|
||||
|
||||
for (auto* object : queue)
|
||||
auto renderComponent = entity->GetComponent<ecs::RenderComponent>();
|
||||
if (renderComponent && renderComponent->GetModel())
|
||||
{
|
||||
if (!object) continue;
|
||||
|
||||
// Extraction des données de position
|
||||
XMVECTOR position = object->GetPosition();
|
||||
float x = XMVectorGetX(position);
|
||||
float y = XMVectorGetY(position);
|
||||
float z = XMVectorGetZ(position);
|
||||
// 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);
|
||||
|
||||
// Calcul du rayon approximatif
|
||||
XMVECTOR scale = object->GetScale();
|
||||
XMVECTOR scale = transformComponent->GetScale();
|
||||
float radius = max(max(XMVectorGetX(scale), XMVectorGetY(scale)), XMVectorGetZ(scale));
|
||||
|
||||
// Vérification du frustum
|
||||
// verification du frustum
|
||||
bool visible = frustum_culling_.CheckCube(x, y, z, radius, get_frustum_tolerance());
|
||||
object->SetVisible(visible);
|
||||
renderComponent->SetVisible(visible);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -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) {
|
||||
const float maxSpeed = 50.0f; // Limite de vitesse maximale
|
||||
bool application_class::render_physics(float delta_time) {
|
||||
|
||||
for (auto& object : object_) {
|
||||
if (object == nullptr) {
|
||||
Logger::Get().Log("object is null", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
return false;
|
||||
// update the physical entity if they have the physics component
|
||||
auto entities_with_physics = entity_manager_->GetEntitiesWithComponent<ecs::PhysicsComponent>();
|
||||
for (auto& entity : entities_with_physics) {
|
||||
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;
|
||||
@ -2057,7 +1988,7 @@ void application_class::physics_thread_function()
|
||||
lastTime = now;
|
||||
|
||||
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)
|
||||
{
|
||||
Logger::Get().Log("Could not render the physics scene", __FILE__, __LINE__, Logger::LogLevel::Error);
|
||||
|
Loading…
x
Reference in New Issue
Block a user