This commit is contained in:
aozhiwei 2023-11-04 08:51:03 +08:00
parent 16119fb69b
commit 8451dfd4c1
7 changed files with 29 additions and 28 deletions

View File

@ -90,7 +90,7 @@ namespace mc
switch (collider_union_xobj->At("type")->AsXValue().GetInt()) { switch (collider_union_xobj->At("type")->AsXValue().GetInt()) {
case kBoxCollider: case kBoxCollider:
{ {
Collider* c = new BoxCollider(); auto c = std::make_shared<BoxCollider>();
c->type = kBoxCollider; c->type = kBoxCollider;
c->Read(collider_union_xobj->At("box")); c->Read(collider_union_xobj->At("box"));
colliders.push_back(c); colliders.push_back(c);
@ -98,7 +98,7 @@ namespace mc
break; break;
case kMeshCollider: case kMeshCollider:
{ {
Collider* c = new MeshCollider(); auto c = std::make_shared<MeshCollider>();
c->type = kMeshCollider; c->type = kMeshCollider;
c->Read(collider_union_xobj->At("mesh")); c->Read(collider_union_xobj->At("mesh"));
colliders.push_back(c); colliders.push_back(c);
@ -115,7 +115,7 @@ namespace mc
{ {
auto childs_arr = xobj->At("childs"); auto childs_arr = xobj->At("childs");
for (int i = 0; i < childs_arr->Size(); ++i) { for (int i = 0; i < childs_arr->Size(); ++i) {
ColliderNode* node = new ColliderNode(); auto node = std::make_shared<ColliderNode>();
node->parent = this; node->parent = this;
node->Read(childs_arr->At(i)); node->Read(childs_arr->At(i));
if (childs.find(node->name) != childs.end()) { if (childs.find(node->name) != childs.end()) {

View File

@ -100,8 +100,8 @@ namespace mc
std::string name; std::string name;
Bounds bounds; Bounds bounds;
Transform transform; Transform transform;
std::vector<Collider*> colliders; std::vector<std::shared_ptr<Collider>> colliders;
std::map<std::string, ColliderNode*> childs; std::map<std::string, std::shared_ptr<ColliderNode>> childs;
void Read(std::shared_ptr<a8::XObject> xobj); void Read(std::shared_ptr<a8::XObject> xobj);
}; };

View File

@ -62,7 +62,8 @@ static float frand()
return (float)rand()/(float)RAND_MAX; return (float)rand()/(float)RAND_MAX;
} }
static void TraverseMapColliderNode(mc::ColliderNode* node, std::function<void (mc::ColliderNode*)> cb) static void TraverseMapColliderNode(std::shared_ptr<mc::ColliderNode> node,
std::function<void (std::shared_ptr<mc::ColliderNode>)> cb)
{ {
for (auto& pair : node->childs) { for (auto& pair : node->childs) {
cb(pair.second); cb(pair.second);
@ -820,7 +821,7 @@ bool MapInstance::PtInHouse(const glm::vec3& pt, glm::vec3& nearest_pt)
void MapInstance::LoadHouse() void MapInstance::LoadHouse()
{ {
if (GetMapMeta()->collider_info) { if (GetMapMeta()->collider_info) {
mc::ColliderNode* building_root = GetMapMeta()->collider_info->GetNode("building"); std::shared_ptr<mc::ColliderNode> building_root = GetMapMeta()->collider_info->GetNode("building");
if (building_root) { if (building_root) {
for (auto& pair : building_root->childs) { for (auto& pair : building_root->childs) {
auto node = pair.second; auto node = pair.second;
@ -895,16 +896,16 @@ void MapInstance::LoadHouse()
//求三角形min_y max_y //求三角形min_y max_y
things_.reserve(1024 * 10); things_.reserve(1024 * 10);
auto cb = auto cb =
[this, box_tri_arr] (mc::ColliderNode* node) [this, box_tri_arr] (std::shared_ptr<mc::ColliderNode> node)
{ {
if (!node->colliders.empty()) { if (!node->colliders.empty()) {
for (auto c : node->colliders) { for (auto c : node->colliders) {
if (c->type == mc::kMeshCollider) { if (c->type == mc::kMeshCollider) {
mc::MeshCollider* mesh_collider = (mc::MeshCollider*)c; mc::MeshCollider* mesh_collider = (mc::MeshCollider*)c.get();
mesh_collider->mesh.triangles.reserve(mesh_collider->mesh.raw_triangles.size() / 3); mesh_collider->mesh.triangles.reserve(mesh_collider->mesh.raw_triangles.size() / 3);
for (int i = 0; i < mesh_collider->mesh.raw_triangles.size(); i += 3) { for (int i = 0; i < mesh_collider->mesh.raw_triangles.size(); i += 3) {
auto& tri = a8::FastAppend(mesh_collider->mesh.triangles); auto& tri = a8::FastAppend(mesh_collider->mesh.triangles);
tri.node = node; tri.node = node.get();
int v0 = mesh_collider->mesh.raw_triangles[i + 0]; int v0 = mesh_collider->mesh.raw_triangles[i + 0];
int v1 = mesh_collider->mesh.raw_triangles[i + 1]; int v1 = mesh_collider->mesh.raw_triangles[i + 1];
int v2 = mesh_collider->mesh.raw_triangles[i + 2]; int v2 = mesh_collider->mesh.raw_triangles[i + 2];
@ -928,7 +929,7 @@ void MapInstance::LoadHouse()
map_service_->AddTriangle(&tri); map_service_->AddTriangle(&tri);
}//end for i }//end for i
} else if (c->type == mc::kBoxCollider) { } else if (c->type == mc::kBoxCollider) {
mc::BoxCollider* box_collider = (mc::BoxCollider*)c; mc::BoxCollider* box_collider = (mc::BoxCollider*)c.get();
#if 0 #if 0
if (node->name == "Wall2_1 (18)") { if (node->name == "Wall2_1 (18)") {
printf("node->name %s %f,%f,%f %f,%f,%f\n", printf("node->name %s %f,%f,%f %f,%f,%f\n",
@ -951,7 +952,7 @@ void MapInstance::LoadHouse()
box_collider->triangles.reserve(2 * 6); box_collider->triangles.reserve(2 * 6);
for (int i = 0; i < 12; ++i) { for (int i = 0; i < 12; ++i) {
auto& tri = a8::FastAppend(box_collider->triangles); auto& tri = a8::FastAppend(box_collider->triangles);
tri.node = node; tri.node = node.get();
tri.vert0 = new_points[box_tri_arr[i][0]]; tri.vert0 = new_points[box_tri_arr[i][0]];
tri.vert1 = new_points[box_tri_arr[i][1]]; tri.vert1 = new_points[box_tri_arr[i][1]];
tri.vert2 = new_points[box_tri_arr[i][2]]; tri.vert2 = new_points[box_tri_arr[i][2]];

View File

@ -12,7 +12,7 @@ namespace mc
struct HouseInfo struct HouseInfo
{ {
mc::ColliderNode* node = nullptr; std::shared_ptr<mc::ColliderNode> node;
std::vector<float> verts; std::vector<float> verts;
}; };
@ -82,7 +82,7 @@ class MapInstance : public std::enable_shared_from_this<MapInstance>
std::list<HouseInfo> houses_; std::list<HouseInfo> houses_;
std::vector<mc::ColliderNode*> things_; std::vector<std::shared_ptr<mc::ColliderNode>> things_;
std::set<dtPolyRef> connectable_polys_; std::set<dtPolyRef> connectable_polys_;
std::string map_tpl_name_; std::string map_tpl_name_;

View File

@ -35,7 +35,7 @@ namespace mt
std::vector<int> safearea_list; std::vector<int> safearea_list;
std::vector<std::shared_ptr<WorldObject>> _world_objects; std::vector<std::shared_ptr<WorldObject>> _world_objects;
std::map<int, std::vector<std::shared_ptr<WorldObject>>> _group_world_objects; std::map<int, std::vector<std::shared_ptr<WorldObject>>> _group_world_objects;
MapCollider* collider_info = nullptr; std::shared_ptr<MapCollider> collider_info = nullptr;
std::vector<std::tuple<std::shared_ptr<WorldObject>, int>> moba_born_points; std::vector<std::tuple<std::shared_ptr<WorldObject>, int>> moba_born_points;
std::vector<std::vector<std::tuple<std::shared_ptr<WorldObject>, int>>> moba_path_points; std::vector<std::vector<std::tuple<std::shared_ptr<WorldObject>, int>>> moba_path_points;

View File

@ -9,13 +9,13 @@
#include "navmesh.pb.h" #include "navmesh.pb.h"
std::vector<mt::MapCollider*> mt::MapCollider::raw_list; std::vector<std::shared_ptr<mt::MapCollider>> mt::MapCollider::raw_list;
std::map<std::string, mt::MapCollider*> mt::MapCollider::name_hash; std::map<std::string, std::shared_ptr<mt::MapCollider>> mt::MapCollider::name_hash;
namespace mt namespace mt
{ {
MapCollider* MapCollider::GetByName(const std::string& name) std::shared_ptr<MapCollider> MapCollider::GetByName(const std::string& name)
{ {
auto itr = name_hash.find(name); auto itr = name_hash.find(name);
return itr != name_hash.end() ? itr->second : nullptr; return itr != name_hash.end() ? itr->second : nullptr;
@ -49,7 +49,7 @@ namespace mt
auto xobj_nodes = xobj_root.At("nodes"); auto xobj_nodes = xobj_root.At("nodes");
for (int i = 0; i < xobj_nodes->Size(); ++i) { for (int i = 0; i < xobj_nodes->Size(); ++i) {
auto xobj_node = xobj_nodes->At(i); auto xobj_node = xobj_nodes->At(i);
mc::ColliderNode* node = new mc::ColliderNode(); auto node = std::make_shared<mc::ColliderNode>();
node->Read(xobj_node); node->Read(xobj_node);
if (nodes_.find(node->name) != nodes_.end()) { if (nodes_.find(node->name) != nodes_.end()) {
abort(); abort();
@ -82,13 +82,13 @@ namespace mt
abort(); abort();
} }
const std::string terrain_name = "_terrain_"; const std::string terrain_name = "_terrain_";
mc::ColliderNode* node = new mc::ColliderNode(); auto node = std::make_shared<mc::ColliderNode>();
node->name = terrain_name; node->name = terrain_name;
if (nodes_.find(node->name) != nodes_.end()) { if (nodes_.find(node->name) != nodes_.end()) {
abort(); abort();
} }
{ {
mc::MeshCollider* mesh = new mc::MeshCollider(); auto mesh = std::make_shared<mc::MeshCollider>();
mesh->type = mc::kMeshCollider; mesh->type = mc::kMeshCollider;
mesh->ca_type = mc::kCA_Surface; mesh->ca_type = mc::kCA_Surface;
mesh->mesh.vertices.reserve(verts_pb.vectors().size()); mesh->mesh.vertices.reserve(verts_pb.vectors().size());
@ -129,7 +129,7 @@ namespace mt
} }
} }
mc::ColliderNode* MapCollider::GetNode(const std::string& name) std::shared_ptr<mc::ColliderNode> MapCollider::GetNode(const std::string& name)
{ {
auto itr = nodes_.find(name); auto itr = nodes_.find(name);
return itr != nodes_.end() ? itr->second : nullptr; return itr != nodes_.end() ? itr->second : nullptr;

View File

@ -13,12 +13,12 @@ namespace mt
class MapCollider class MapCollider
{ {
protected: protected:
static std::vector<MapCollider*> raw_list; static std::vector<std::shared_ptr<MapCollider>> raw_list;
static std::map<std::string, MapCollider*> name_hash; static std::map<std::string, std::shared_ptr<MapCollider>> name_hash;
public: public:
static MapCollider* GetByName(const std::string& name); static std::shared_ptr<MapCollider> GetByName(const std::string& name);
static void StaticPostInit(); static void StaticPostInit();
static void LoadAll(); static void LoadAll();
@ -28,11 +28,11 @@ namespace mt
void Load(const std::string& filename); void Load(const std::string& filename);
void LoadTerrain(const std::string& filename); void LoadTerrain(const std::string& filename);
void SaveToObjFile(const std::string& filename, const navmesh::vertex& vert_pb); void SaveToObjFile(const std::string& filename, const navmesh::vertex& vert_pb);
mc::ColliderNode* GetNode(const std::string& name); std::shared_ptr<mc::ColliderNode> GetNode(const std::string& name);
std::map<std::string, mc::ColliderNode*>& GetNodes() { return nodes_; }; std::map<std::string, std::shared_ptr<mc::ColliderNode>>& GetNodes() { return nodes_; };
private: private:
std::map<std::string, mc::ColliderNode*> nodes_; std::map<std::string, std::shared_ptr<mc::ColliderNode>> nodes_;
}; };
} }