This commit is contained in:
aozhiwei 2020-08-12 15:30:24 +08:00
parent d5abdfbc95
commit ab8e7eee21
3 changed files with 64 additions and 1 deletions

View File

@ -45,6 +45,7 @@ class Entity
float GetY() { return pos_.y; }
void SetX(float x) { pos_.x = x; }
void SetY(float y) { pos_.y = y; }
std::list<ColliderComponent*>* GetColliders() { return &colliders_; }
protected:
bool IsClientCached(Human* hum);

View File

@ -1,4 +1,4 @@
#include "precompile.h"
#include "precompile.h"
#include "mapinstance.h"
#include "mapservice.h"
@ -10,6 +10,7 @@
#include "metamgr.h"
#include "room.h"
#include "entityfactory.h"
#include "collider.h"
#ifdef FIND_PATH_TEST
const int MAP_GRID_WIDTH = 40;
@ -57,6 +58,9 @@ void MapInstance::Init()
if (current_uniid_ >= FIXED_OBJECT_MAXID) {
abort();
}
#ifdef DEBUG
OutputObjFile();
#endif
}
void MapInstance::UnInit()
@ -272,3 +276,60 @@ int MapInstance::AllocUniid()
}
return current_uniid_;
}
void MapInstance::OutputObjFile()
{
std::vector<a8::Vec2> vertexs;
std::vector<std::tuple<int, int, int, int>> faces;
vertexs.reserve(10000);
for (auto& pair : uniid_hash_) {
for (ColliderComponent* collider : *pair.second->GetColliders()) {
if (collider->type == CT_Aabb) {
AabbCollider* aabb_box = (AabbCollider*)collider;
{
a8::Vec2 vert = collider->owner->GetPos() + aabb_box->_min;
vert.x = vert.x - map_meta_->i->map_width() / 2.0f;
vert.y = vert.y - map_meta_->i->map_height() / 2.0f;
vertexs.push_back(vert);
vert.y += aabb_box->_max.y - aabb_box->_min.y;
vertexs.push_back(vert);
vert.x += aabb_box->_max.x - aabb_box->_min.x;
vertexs.push_back(vert);
vert.y -= aabb_box->_max.y - aabb_box->_min.y;
vertexs.push_back(vert);
}
faces.push_back
(std::make_tuple
(
vertexs.size() - 1,
vertexs.size() - 2,
vertexs.size() - 3,
vertexs.size() - 4
));
}
}
}
{
std::string filename = a8::Format("%s.obj", {map_tpl_name_});
FILE* fp = fopen(filename.c_str(), "wb");
for (auto& vert : vertexs) {
std::string data = a8::Format("v %f %f %f\r\n",
{
vert.x,
vert.y,
0
});
fwrite(data.data(), 1, data.size(), fp);
}
for (auto& tuple : faces) {
std::string data = a8::Format("f %d %d %d %d\r\n",
{
std::get<0>(tuple),
std::get<1>(tuple),
});
fwrite(data.data(), 1, data.size(), fp);
}
fclose(fp);
}
}

View File

@ -31,6 +31,7 @@ class MapInstance
std::function<void (Obstacle*)> on_precreate);
Entity* GetEntityByUniId(int uniid);
int AllocUniid();
void OutputObjFile();
private:
int current_uniid_ = 0;