//H #ifndef _INCLUDE_MAP_ #define _INCLUDE_MAP_ #include #include #include "CObjectManager.h" #include "../both/CMapDataManager.h" #include "../both/CObject.h" #include "CPlayerManager.h" #include "CPlayer.h" typedef u32 MapID; typedef s32 PlayerID; struct SZone { list listeners; list objects; }; class CMap { private: dimension2d zoneAmount; SZone* zones; MapData* data; MapID id; public: CMap(MapData* data, MapID id_); ~CMap(); void addObject(ObjectID id); bool removeObject(ObjectID id); //overload for initial needs bool removeObject(ObjectID id, u32 x, u32 y); MapID getID(); //returns movement vector vector2df update(ObjectID id, vector2df movement); }; #endif //CPP #include "CMap.h" CMap::CMap(MapData* data_, MapID id_) { data = data_; id = id_; f32 x,y; x = data->size.Width/10; y = data->size.Height/10; zoneAmount.Width = x; zoneAmount.Height = y; if(zoneAmount.Width < x) zoneAmount.Width++; if(zoneAmount.Height < y) zoneAmount.Height++; zones = new SZone[zoneAmount.Width *zoneAmount.Height]; } MapID CMap::getID() { return id; } //Checks if the Objects new Position collisdes wiht any other object //its assumed that the start position of the object was valid vector2df CMap::update(ObjectID id, vector2df movement) { u32 x = omgr->getObject(id)->getPosition().X/10; u32 y = omgr->getObject(id)->getPosition().Y/10; omgr->getObject(id)->setPosition(omgr->getObject(id)->getPosition() + movement); u32 x2 = omgr->getObject(id)->getPosition().X/10; u32 y2 = omgr->getObject(id)->getPosition().Y/10; if(x != x2 || y != y2) { this->removeObject(id, x, y); this->addObject(id); } return movement; } void CMap::addObject(ObjectID id) { u32 x = omgr->getObject(id)->getPosition().X/10; u32 y = omgr->getObject(id)->getPosition().Y/10; zones[(x*zoneAmount.Height) + y].objects.push_back(id); } bool CMap::removeObject(ObjectID id, u32 x, u32 y) { for(list::Iterator it = zones[(x*zoneAmount.Height) + y].objects.begin(); it != zones[(x*zoneAmount.Height) + y].objects.end(); it++) { if((*it) == id) { zones[(x*zoneAmount.Height) + y].objects.erase(it); return true; } } return false; } bool CMap::removeObject(ObjectID id) { logger->log(EMT_INFO, "Removing Object"); u32 x = omgr->getObject(id)->getPosition().X/10; u32 y = omgr->getObject(id)->getPosition().Y/10; return this->removeObject(id, x, y); } CMap::~CMap() { delete[] zones; }