diff --git a/server/gameserver/mapservice.cc b/server/gameserver/mapservice.cc new file mode 100644 index 0000000..a039818 --- /dev/null +++ b/server/gameserver/mapservice.cc @@ -0,0 +1,63 @@ +#include "precompile.h" + +#include + +#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 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; +} diff --git a/server/gameserver/mapservice.h b/server/gameserver/mapservice.h new file mode 100644 index 0000000..0a24c12 --- /dev/null +++ b/server/gameserver/mapservice.h @@ -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 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}; + +}; diff --git a/server/gameserver/room.cc b/server/gameserver/room.cc index 6ee4e32..1293042 100644 --- a/server/gameserver/room.cc +++ b/server/gameserver/room.cc @@ -1301,3 +1301,4 @@ void Room::NotifyWxVoip() }, &xtimer_attacher.timer_list_); } +