2021-12-05 17:54:36 +00:00
|
|
|
#include "dpWorld.h"
|
|
|
|
#include "dpEntity.h"
|
|
|
|
#include "dpGrid.h"
|
|
|
|
#include "DetourCommon.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "Game.h"
|
|
|
|
#include "dLogger.h"
|
|
|
|
#include "dConfig.h"
|
|
|
|
|
|
|
|
void dpWorld::Initialize(unsigned int zoneID) {
|
|
|
|
phys_sp_tilecount = std::atoi(Game::config->GetValue("phys_sp_tilecount").c_str());
|
|
|
|
phys_sp_tilesize = std::atoi(Game::config->GetValue("phys_sp_tilesize").c_str());
|
|
|
|
|
2022-07-25 02:26:51 +00:00
|
|
|
//If spatial partitioning is enabled, then we need to create the m_Grid.
|
2021-12-05 17:54:36 +00:00
|
|
|
//if m_Grid exists, then the old method will be used.
|
|
|
|
//SP will NOT be used unless it is added to ShouldUseSP();
|
|
|
|
if (std::atoi(Game::config->GetValue("phys_spatial_partitioning").c_str()) == 1
|
|
|
|
&& ShouldUseSP(zoneID)) {
|
|
|
|
m_Grid = new dpGrid(phys_sp_tilecount, phys_sp_tilesize);
|
|
|
|
}
|
|
|
|
|
2022-08-02 05:30:19 +00:00
|
|
|
m_NavMesh = new dNavMesh(zoneID);
|
2021-12-05 17:54:36 +00:00
|
|
|
|
2022-08-02 05:30:19 +00:00
|
|
|
Game::logger->Log("dpWorld", "Physics world initialized!");
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dpWorld::~dpWorld() {
|
|
|
|
if (m_Grid) {
|
|
|
|
delete m_Grid;
|
|
|
|
m_Grid = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-08-02 05:30:19 +00:00
|
|
|
m_NavMesh->~dNavMesh();
|
|
|
|
delete m_NavMesh;
|
|
|
|
m_NavMesh = nullptr;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dpWorld::StepWorld(float deltaTime) {
|
2022-07-25 02:26:51 +00:00
|
|
|
if (m_Grid) {
|
|
|
|
m_Grid->Update(deltaTime);
|
|
|
|
return;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Pre update:
|
|
|
|
for (auto entity : m_StaticEntities) {
|
|
|
|
if (!entity || entity->GetSleeping()) continue;
|
|
|
|
entity->PreUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Do actual update:
|
|
|
|
for (auto entity : m_DynamicEntites) {
|
|
|
|
if (!entity || entity->GetSleeping()) continue;
|
2022-07-25 02:26:51 +00:00
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
entity->Update(deltaTime);
|
|
|
|
|
|
|
|
for (auto other : m_StaticEntities) {
|
|
|
|
if (!other || other->GetSleeping() || entity->GetObjectID() == other->GetObjectID()) continue;
|
|
|
|
|
|
|
|
other->CheckCollision(entity); //swap "other" and "entity" if you want dyn objs to handle collisions.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dpWorld::AddEntity(dpEntity* entity) {
|
|
|
|
if (m_Grid) entity->SetGrid(m_Grid); //This sorts this entity into the right cell
|
|
|
|
else { //old method, slow
|
|
|
|
if (entity->GetIsStatic()) m_StaticEntities.push_back(entity);
|
|
|
|
else m_DynamicEntites.push_back(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dpWorld::RemoveEntity(dpEntity* entity) {
|
|
|
|
if (!entity) return;
|
|
|
|
|
|
|
|
if (m_Grid) {
|
|
|
|
m_Grid->Delete(entity);
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
if (entity->GetIsStatic()) {
|
|
|
|
for (size_t i = 0; i < m_StaticEntities.size(); ++i) {
|
|
|
|
if (m_StaticEntities[i] == entity) {
|
|
|
|
delete m_StaticEntities[i];
|
|
|
|
m_StaticEntities[i] = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 13:39:57 +00:00
|
|
|
} else {
|
2021-12-05 17:54:36 +00:00
|
|
|
for (size_t i = 0; i < m_DynamicEntites.size(); ++i) {
|
|
|
|
if (m_DynamicEntites[i] == entity) {
|
|
|
|
delete m_DynamicEntites[i];
|
|
|
|
m_DynamicEntites[i] = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dpWorld::ShouldUseSP(unsigned int zoneID) {
|
2022-08-02 05:30:19 +00:00
|
|
|
// TODO: Add to this list as needed.
|
|
|
|
// Only large maps should be added as tiling likely makes little difference on small maps.
|
|
|
|
|
2021-12-05 17:54:36 +00:00
|
|
|
switch (zoneID) {
|
2022-08-02 05:30:19 +00:00
|
|
|
case 1100: // Avant Gardens
|
|
|
|
case 1200: // Nimbus Station
|
|
|
|
case 1300: // Gnarled Forest
|
|
|
|
case 1400: // Forbidden Valley
|
|
|
|
case 1800: // Crux Prime
|
|
|
|
case 1900: // Nexus Tower
|
|
|
|
case 2000: // Ninjago
|
|
|
|
return true;
|
2021-12-05 17:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|