add mapservice

This commit is contained in:
aozhiwei 2019-06-13 09:10:24 +08:00
parent 566ceb0570
commit f32fbf92be
3 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,63 @@
#include "precompile.h"
#include <memory.h>
#include "mapservice.h"
MapService::MapService()
{
}
MapService::~MapService()
{
if (map_cells_) {
free(map_cells_);
map_cells_ = nullptr;
}
}
void MapService::Init(int width, int height, int cell_width)
{
if (width < 1 || height < 1 || cell_width < 1) {
abort();
}
map_width_ = width;
map_height_ = height;
cell_width_ = cell_width;
max_grid_id_ = map_width_ * map_height_;
map_cells_ = (list_head*)malloc(sizeof(list_head) * width * height);
memset(map_cells_, sizeof(list_head) * width * height, 0);
}
void MapService::UnInit()
{
}
bool MapService::Walkable(float world_x, float world_y)
{
int grid_id = GetGridId(world_x, world_y);
if (grid_id < 0 || grid_id >= max_grid_id_) {
return false;
}
list_head& node = map_cells_[grid_id];
return node.next == nullptr && node.prev == nullptr;
}
void MapService::TouhcAroundCell(float world_x, float world_y,
std::function<bool (ColliderComponent*)> callback)
{
}
int MapService::GetGridId(float world_x, float world_y)
{
int x = world_x + cell_width_;
int y = world_y + cell_width_;
assert(x >= 0 && y >= 0);
int grid_id = x/cell_width_ + (y/cell_width_) * map_width_;
return grid_id;
}

View File

@ -0,0 +1,35 @@
#pragma once
class Entity;
class Human;
class Bullet;
class Human;
class Entity;
class Room;
class ColliderComponent;
class MapService
{
public:
MapService();
~MapService();
void Init(int width, int height, int cell_width);
void UnInit();
bool Walkable(float world_x, float world_y);
void TouhcAroundCell(float world_x, float world_y,
std::function<bool (ColliderComponent*)> callback);
private:
int GetGridId(float world_x, float world_y);
private:
list_head* map_cells_ = nullptr;
int map_width_ = 0;
int map_height_ = 0;
int cell_width_ = 0;
int max_grid_id_ = 0;
int grid_offset_arr_[9] = {0};
};

View File

@ -1301,3 +1301,4 @@ void Room::NotifyWxVoip()
},
&xtimer_attacher.timer_list_);
}