Files
mir_server/server/LogicServer/scene/EntityGrid.h

304 lines
7.2 KiB
C
Raw Normal View History

2025-01-09 17:45:40 +08:00
#pragma once
/************************
***********************/
//class CLinkedNode<Entity*> EntityNode;
class CBaseEntityList;
class CEntity;
class CScene;
/*
x坐标一样的放一个列表1
*/
#ifndef GRID_USE_STL_MAP
struct EntityHandlePos
{
EntityHandle hd; //实体的handle
WORD pos; //位置
};
typedef CBaseList<EntityHandlePos> EntityPosVector;
#else
struct HandleCmpter
{
bool operator()(const EntityHandle& _x, const EntityHandle& _y) const
{ return Uint64(_x) < Uint64(_y); }
};
typedef std::map<EntityHandle, WORD, HandleCmpter> EntityPosVector;
#endif
// 定义游戏中单个实体格子
class MapEntityGrid
{
public:
MapEntityGrid(){
//m_entitys.reserve(16);
}
~MapEntityGrid();
// 添加实体
void addEntity(EntityHandle handle, int posY){
#ifndef GRID_USE_STL_MAP
for (INT_PTR i = 0; i < m_entitys.count(); i++)
{
if (m_entitys[i].hd == handle)
{
return ;
}
}
EntityHandlePos pos;
pos.hd = handle;
pos.pos = (WORD)posY;
m_entitys.add(pos);
#else
assert(m_entitys.find(handle) == m_entitys.end());
m_entitys[handle] = posY;
#endif
}
void setEntityPos(EntityHandle handle,int posY)
{
#ifndef GRID_USE_STL_MAP
for (INT_PTR i = m_entitys.count() -1; i >-1 ; i--)
{
if (m_entitys[i].hd == handle)
{
m_entitys[i].pos=(WORD)posY;
return ;
}
}
#else
assert(m_entitys.find(handle) != m_entitys.end());
m_entitys[handle] = posY;
#endif
}
// 移除实体
void removeEntity(EntityHandle handle)
{
#ifndef GRID_USE_STL_MAP
for (INT_PTR i = m_entitys.count() -1; i >-1 ; i--)
{
if (m_entitys[i].hd == handle)
{
m_entitys.remove(i);
return ;
}
}
#else
//玩家初始化不成功,可能不在格子里
EntityPosVector::iterator it = m_entitys.find(handle);
//assert(m_entitys.find(handle) != m_entitys.end());
if(it != m_entitys.end())
{
m_entitys.erase(it);
}
#endif
}
int GetTypeEntityPosCount(int nType,int nPosY)
{
int nCount=0;
#ifndef GRID_USE_STL_MAP
for(INT_PTR i=0;i < m_entitys.count(); i++)
{
if((int)m_entitys[i].hd.GetType() == nType && m_entitys[i].pos == nPosY)
{
nCount ++;
}
}
#else
typedef typename EntityPosVector::iterator Iter;
Iter it;
Iter end = m_entitys.end();
for (it = m_entitys.begin(); it != end; it++)
{
if((int)(*it).first.GetType() == nType && (*it).second == nPosY)
nCount ++;
}
#endif
return nCount;
}
EntityPosVector m_entitys; // 格子中的实体列表
};
//typedef HandleMgr<CEntityList,CEntityListHandle > CEntityListMgr;
class CEntityGrid
{
friend class CScene;
friend class AStart;
friend class ASMapCell;
public:
CEntityGrid();
virtual ~CEntityGrid();
void SetScene(CScene* pScene) {m_pScene = pScene;}
//inline CEntityList* GetList(INT_PTR x,INT_PTR y);
MapEntityGrid* GetList(INT_PTR x);
/*inline VOID SetList(INT_PTR x,INT_PTR y,CEntityList* pList);*/
//释放资源
VOID Release();
//只清理怪物和玩家,以备重用
void Reset();
//释放一个list
VOID ReleaseList(CEntityList* el);
CEntityList* GetEntityList();
//重新设置网格的大小
VOID Init(int nRowCount,int nColCount);
/*
* Comments:
* Param CEntity * pEntity:
* Param int nPosx: x
* Param int nPosY: y
* @Return int: truefalse
*/
//bool AddEntity(CEntity * pEntity,INT_PTR nPosx,INT_PTR nPosY);
/*
* Comments:
* Param CEntity * pEntity:
* @Return void:
*/
void TelePort( CEntity * pEntity );
//判断是否可以通过传送门
bool CanPassPort( CEntity * pEntity,int nPassId);
/*
* Comments:
* Param CEntity * pEntity:
* Param int nPosx:nposx会作为一个返回值
* Param int nPosY:
* Param bool bShowErr:
* @Return bool:
*/
bool CanAddEntity(CEntity * pEntity,INT_PTR& nX,INT_PTR& nY,bool bShowErr = true, bool bCanAlwaysEnter=false);
/*
* Comments:退
* Param CEntity * pEntity:
* Param int nX:
* Param int nY:
*/
void DeleteEntity(CEntity * pEntity);
//移动一个实体
int Move(CEntity * pEntity,INT_PTR nDir,INT_PTR nSpeed=1);
/*
* Comments:
* Param CEntity * pEntity:
* Param INT_PTR nNewX:
* Param INT_PTR nNewY:
* Param bool boNew:
* @Return int:
*/
int MoveTo(CEntity * pEntity,INT_PTR nNewX,INT_PTR nNewY,bool boNew = false,bool bCanAlwaysEnter=false);
/*
* Comments:穿穿
* Param INT_PTR nEntityType:
* Param INT_PTR x:x
* Param INT_PTR y:y
* @Return bool:false
*/
bool CanCross(INT_PTR nEntityType, INT_PTR x, INT_PTR y,bool bCanAlwaysEnter=false,bool bCanEnterFire = false);
/*
* Comments:
* Param INT_PTR & nX:
* Param INT_PTR & nY:
* Param BYTE nDir:
* Param int nSpeed:
* @Return VOID:
*/
static VOID NewPosition(INT_PTR& nX,INT_PTR& nY,INT_PTR nDir,INT_PTR nSpeed);
/*
* Comments:
* Param CEntity* pEntity:
* Param INT_PTR & nX:
* Param INT_PTR & nY:
* Param INT_PTR nDir:
* Param INT_PTR nSpeed:
* @Return bool:
*/
bool CanMoveTo(CEntity* pEntity, INT_PTR& nX,INT_PTR& nY,INT_PTR nDir,BYTE nSpeed);
/*
* Comments:
* Param CEntity* pEntity:
* Param INT_PTR x:x坐标
* Param INT_PTR y:y坐标
* @Return bool:
*/
bool CanMoveThisGrid(CEntity* pEntity,INT_PTR x ,INT_PTR y);
/*
* Comments:
* Param CEntity * pEntity:
* Param CScene * pScene:
* Param INT_PTR x: x
* Param INT_PTR y:y
* Param INT_PTR & nResultX: x
* Param INT_PTR & nResultY:y
* Param INT_PTR & nType:
* @Return bool:truefalse
*/
static bool GetMoveablePoint(CEntity *pEntity,CScene * pScene, INT_PTR x,INT_PTR y,INT_PTR &nResultX,INT_PTR &nResultY,bool bCanAlwaysEnter = false,INT_PTR nType = 1);
//将区域下发给玩家
inline void SendAreaConfig(SCENEAREA* pArea,CActor *pActor);
protected:
/*
* Comments:
* Param CActor * pActor:
* @Return void:
*/
void CrossArea(CActor* pActor);
private:
/*
* Comments:xy坐标是否能走动的点
* Param int x:x坐标
* Param int y:y坐标
* @Return bool:
*/
//bool CanMove(INT_PTR x,INT_PTR y);
private:
//网格的行数和列数
int m_nRow;
int m_nCol;
//一个二维数组保存链表的指针
//CEntityList** m_pGrid;
MapEntityGrid* m_pGrids; // 格子集合,用于一个维护一个场景中的所有小格子列表
CScene* m_pScene;
public:
//static CEntityListMgr* g_EntityListMgr; //实体链表的内存管理类,全局变量
};