#include "PlayerContainer.h" using namespace std; using namespace graphic; using namespace xml; //Loads xml files and creates elements (unit/buildings and sceneelements) bool PlayerContainer::CreateElements(const char *xmlpath) { try { XmlHandler->XmlSetDoc(xmlpath); } catch(Exception e) { e.PrintError(); exit(-1); } do { // Get Element pointer and store it in storage _element_storage.push_back(XmlHandler->GetElement()); // Add element to the object map _objectmap_pointer->AddObject(XmlHandler->GetElement(), XmlHandler->GetRowPos(), XmlHandler->GetColPos()); _id++; }while(XmlHandler->NextTag()); //Keep reading while there are more xml tags return true; } // Places containers units in a group at row,col void PlayerContainer::PlaceUnitGroup(int row, int col) { int x = row; for( std::vector::iterator it = _element_storage.begin(); it != _element_storage.end(); it++ ) { //TODO A script or xml file should deside what should be placed in a group if(!(*it)->GetType().compare("unit")) { // Move unit until it is at destination _objectmap_pointer->MoveObject((*it), x, col); (*it)->SetPosition(_objectmap_pointer->GetObjX((*it)), _objectmap_pointer->GetObjY((*it))); (*it)->SetDirection(DirectionTowardsCenter(*it)); x++; } } } // Calculates the visibilty for the objectmap with containers Elements void PlayerContainer::ComputeVisibility() { // reset TilesMap visibility _objectmap_pointer->ResetMapVisibility(); for(std::vector::iterator it = _element_storage.begin(); it != _element_storage.end(); it++) { //TODO Should first check what elements have vision, these operations should not be hard coded but set from scripts or xml files if(!(*it)->GetType().compare("unit")) _objectmap_pointer->ComputeObjVisibility((*it), (*it)->GetCurStat("vision"), (*it)->GetDirection()); else if(!(*it)->GetType().compare("building")) _objectmap_pointer->ComputeObjVisibility((*it), (*it)->GetCurStat("vision")); } } // Find and delete a element at row,col if it belongs to playercontainer void PlayerContainer::DeleteElement(int row, int col) { Element * aElement = (Element *)_objectmap_pointer->GetObject(row, col); if(aElement) DeleteElement(aElement->GetCurStat(STAT_ID)); } // Delete selected element void PlayerContainer::DeleteElement(Element * element) { if(element) DeleteElement(element->GetCurStat(STAT_ID)); } // Find and delete a element with ID if it belongs to playercontainer void PlayerContainer::DeleteElement(int id) { std::vector::iterator aElementIt = LocateElement(id); if( aElementIt != _element_storage.end()) Delete(aElementIt); } // return element present at row,col if it belongs to playercontainer Element * PlayerContainer::GetElement(int row, int col) { Element * aElement = (Element *)_objectmap_pointer->GetObject(row, col); return GetElement(aElement->GetCurStat(STAT_ID)); } // return element with ID only own elements are checked Element * PlayerContainer::GetElement(int id) { std::vector::iterator aElementIt = LocateElement(id); if( aElementIt != _element_storage.end()) return((*aElementIt)); return NULL; } // Returns an element and removes it from own list Element * PlayerContainer::GiveElement(int row, int col) { Element * element = GetElement(row,col); if(element) return GiveElement(element->GetCurStat(STAT_ID)); return NULL; } // Returns an element and removes it from own list Element * PlayerContainer::GiveElement(int id) { Element * element = GetElement(id); if(element) _element_storage.erase(LocateElement(id)); return element; } // adds a already created Element to this container void PlayerContainer::AddExistingElement(Element * element) { if(element) _element_storage.push_back(element); } /*---- Private ----*/ // TODO create this method int PlayerContainer::DirectionTowardsCenter(Element * sel_unit) { /* // relative origin => center of the unit image f_origin = iso2scr(sel_unit->GetX() + (sel_unit->GetImage()->GetW() / 2), sel_unit->GetY() + (sel_unit->GetImage()->GetH() / 2), tm.GetOriginX(), tm.GetOriginY()); // get orientation orientation = RelDirection((int)round(f_origin->x), (int)round(f_origin->y), sel_unit->GetX(), sel_unit->GetY()); delete f_origin; */ return SOUTH; } // Delete any element type void PlayerContainer::Delete(std::vector::iterator it) { if(it != _element_storage.end()) { IsoObject * isoobject = (*it); Element * element = (*it); // Remove element from objectmap _objectmap_pointer->RemoveObject(isoobject); Unit * unit = NULL; Building * build = NULL; SceneElement * scene = NULL; TerrainElement * terra = NULL; // Delete correct element type if((unit = dynamic_cast (element))) delete unit; else if((build = dynamic_cast (element))) delete build; else if((scene = dynamic_cast (element))) delete scene; else if((terra = dynamic_cast (element))) delete terra; // Delete from container _element_storage.erase(it); } } // Search in storage if element with same id is there std::vector::iterator PlayerContainer::LocateElement(int id) { std::vector::iterator it = _element_storage.begin(); for(; it != _element_storage.end(); it++) { if( (*it)->GetCurStat(STAT_ID) == id) return it; } // it == _element_storage.end() return it; } PlayerContainer::~PlayerContainer() { int size = _element_storage.size(); std::vector::iterator it; // delete all Elements from the vector for( int i=0; i < size; i++ ) { it = _element_storage.begin(); Delete(it); } }