MapObject* CMapObjectManager::getObjectNearBy(int cx,int cy,MapObjectType type) { Rect rect; m_pMap->getViewRect(rect); float x = 0; float y = 0; MapObject* pRet = NULL; const int minD = 3; //最小格子距离 for ( MapObjectList::iterator itr = m_Objects.begin();itr != m_Objects.end();++itr) { MapObject* pObject = *itr; if ( pObject && !pObject->GetDeleted() ) { x = pObject->getPositionX(); y = pObject->getPositionY(); int distance = Distance(cx,cy,pObject->GetCellX(),pObject->GetCellY()); if ( rect.containsPoint(Point(x,y)) && pObject->GetType() == type && distance < minD ) { pRet = pObject; //minD = distance; } } } return pRet; }
int CMapObjectManager::getObjectsInView(MapObjectType type,MapObjectList& objects) { Rect rect; m_pMap->getViewRect(rect); float x = 0; float y = 0; for ( MapObjectList::iterator itr = m_Objects.begin();itr != m_Objects.end();++itr) { MapObject* pObject = *itr; if ( pObject && !pObject->GetDeleted() ) { x = pObject->getPositionX(); y = pObject->getPositionY(); if ( rect.containsPoint(Point(x,y)) && pObject->GetType() == type ) { objects.push_back(pObject); } } } return (int)objects.size(); }