This commit is contained in:
aozhiwei 2023-02-09 12:10:49 +08:00
parent 1f2d8d6f46
commit 9b83137f1d
2 changed files with 60 additions and 1 deletions

View File

@ -7,6 +7,7 @@
#include "entity.h"
#include "roomobstacle.h"
#include "room.h"
#include "mapcollider.h"
MapService::MapService()
{
@ -78,7 +79,65 @@ bool MapService::CanAdd(const glm::vec3& pos, int rad)
void MapService::AddTriangle(mc::Triangle* tri)
{
glm::vec3* verts[3] = {&tri->vert0, &tri->vert1, &tri->vert2};
float min_x = verts[0]->x;
float max_x = verts[0]->x;
float min_y = verts[0]->z;
float max_y = verts[0]->z;
{
for (int i = 1; i < 3; ++i) {
if (verts[i]->x < min_x) {
min_x = verts[i]->x;
}
if (verts[i]->x > max_x) {
max_x = verts[i]->x;
}
if (verts[i]->z < min_y) {
min_y = verts[i]->z;
}
if (verts[i]->z > max_y) {
max_y = verts[i]->z;
}
}
min_x -= 1.0f;
max_x += 1.0f;
min_y -= 1.0f;
max_y += 1.0f;
}
{
int min_grid_x = floor(min_x / cell_width_);
int min_grid_y = floor(min_y / cell_width_);
int max_grid_x = ceil(max_x / cell_width_);
int max_grid_y = ceil(max_y / cell_width_);
if (min_grid_x == -1) {
min_grid_x = 0;
}
if (min_grid_x < 0) {
A8_ABORT();
}
if (max_grid_x >= map_width_) {
max_grid_x = map_width_ - 1;
}
if (min_grid_y == -1) {
min_grid_y = 0;
}
if (min_grid_y < 0) {
A8_ABORT();
}
if (max_grid_y >= map_height_) {
max_grid_y = map_height_ - 1;
}
for (int x = min_grid_x; x <= max_grid_x; ++x) {
for (int y = min_grid_y; y <= max_grid_y; ++y) {
int grid_id = x + y * map_width_;
list_head* head = &map_cells_[grid_id];
CellNode* node = new CellNode();
node->tri = tri;
list_add_tail(&node->entry, head);
}
}
}
}
int MapService::GetGridId(float world_x, float world_y)

View File

@ -10,7 +10,7 @@ namespace mc
struct CellNode
{
list_head entry;
CellNode* next = nullptr;
mc::Triangle* tri = nullptr;
};
class Room;