bool ObjectFactory::search_nodes(TiXmlNode *node) { int result = true; if (node->Type() == TiXmlNode::TINYXML_ELEMENT) { if (strcmp(node->Value(), "player") == 0) { result = match_node_type(node->ToElement()); } else if (strcmp(node->Value(), "collectable") == 0) { result = match_node_type(node->ToElement()); } else if (strcmp(node->Value(), "item") == 0) { result = match_node_type(node->ToElement()); } } for (TiXmlNode *child = node->FirstChild(); child != 0; child = child->NextSibling()) { if (!search_nodes(child)) { result = false; } } return result; }
/** Get node closest to another node with a certain property. * @param node_name the name of the node from which to start * @param consider_unconnected consider unconnected node for the search * of the closest node * @param property property the node must have to be considered, * empty string to not check for any property * @return node closest to the given point in the global frame, or an * invalid node if such a node cannot be found. The node will obviously * not be the node with the name @p node_name. */ NavGraphNode NavGraph::closest_node_to(const std::string &node_name, bool consider_unconnected, const std::string &property) const { NavGraphNode n = node(node_name); std::vector<NavGraphNode> nodes = search_nodes(property); float min_dist = HUGE; std::vector<NavGraphNode>::iterator i; std::vector<NavGraphNode>::iterator elem = nodes.begin(); for (i = nodes.begin(); i != nodes.end(); ++i) { if (! consider_unconnected && i->unconnected()) continue; float dx = i->x() - n.x(); float dy = i->y() - n.y(); float dist = sqrtf(dx * dx + dy * dy); if ((sqrtf(dx * dx + dy * dy) < min_dist) && (i->name() != node_name)) { min_dist = dist; elem = i; } } if (elem == nodes.end()) { return NavGraphNode(); } else { return *elem; } }
Object* ObjectFactory::create_object(const char *name, MediaDB *media, const char *type, int x, int y, const Tmx::Object *obj) { Object *object = 0; Object::Direction dir = Object::Right; if (!type && obj) { type = obj->GetType().c_str(); if (!name) { name = obj->GetName().c_str(); } x = obj->GetX(); y = obj->GetY(); dir = get_dir(obj); } if (strcmp(type, "Player") == 0) { std::string pathname = Object::get_prefix() + std::string(name); TiXmlDocument doc(pathname.c_str()); if (doc.LoadFile()) { search_nodes(&doc); } if (strcmp(priv_object_type, "knight") == 0) { object = new Knight(name, media, x, y, dir); } else if (strcmp(priv_object_type, "climber") == 0) { object = new Climber(name, media, x, y, dir); } else if (strcmp(priv_object_type, "flyer") == 0) { object = new Flyer(name, media, x, y, dir); } else if (strcmp(priv_object_type, "natator") == 0) { object = new Natator(name, media, x, y, dir); } else if (strcmp(priv_object_type, "dragon") == 0) { object = new Dragon(name, media, x, y, dir); } else { object = new Player(name, media, x, y, dir); } } else if (strcmp(type, "Collectable") == 0) { std::string pathname = Object::get_prefix() + std::string(name); TiXmlDocument doc(pathname.c_str()); if (doc.LoadFile()) { search_nodes(&doc); } if (strcmp(priv_object_type, "coin") == 0) { object = new Coin(name, media, x, y); } else if (strcmp(priv_object_type, "heart_container") == 0) { object = new HeartContainer(name, media, x, y); } else if (strcmp(priv_object_type, "heart_refill") == 0) { object = new HeartRefill(name, media, x, y); } else if (strcmp(priv_object_type, "potion") == 0) { object = new Potion(name, media, x, y); } else if (strcmp(priv_object_type, "stone") == 0) { object = new Stone(name, media, x, y); } else if (strcmp(priv_object_type, "fire_refill") == 0) { object = new FireRefill(name, media, x, y); } else if (strcmp(priv_object_type, "tornado_refill") == 0) { object = new TornadoRefill(name, media, x, y); } else if (strcmp(priv_object_type, "arrow_refill") == 0) { object = new ArrowRefill(name, media, x, y); } else if (strcmp(priv_object_type, "boomerang_refill") == 0) { object = new BoomerangRefill(name, media, x, y); } else if (strcmp(priv_object_type, "thunder_refill") == 0) { object = new ThunderRefill(name, media, x, y); } else if (strcmp(priv_object_type, "break_rock") == 0) { object = new BreakRock(name, media, x, y); } else if (strcmp(priv_object_type, "create_rock") == 0) { object = new CreateRock(name, media, x, y); } } else if (strcmp(type, "Item") == 0) { std::string pathname = Object::get_prefix() + std::string(name); TiXmlDocument doc(pathname.c_str()); if (doc.LoadFile()) { search_nodes(&doc); } if (strcmp(priv_object_type, "key") == 0) { object = new Key(name, media, x, y); } else if (strcmp(priv_object_type, "arm") == 0) { object = new Arm(name, media, x, y); } else if (strcmp(priv_object_type, "shield") == 0) { object = new Shield(name, media, x, y); } else if (strcmp(priv_object_type, "armour") == 0) { object = new Armour(name, media, x, y); } } else if (strcmp(type, "Walker") == 0) { object = new Walker(name, media, x, y, dir); } else if (strcmp(type, "Crawler") == 0) { object = new Crawler(name, media, x, y, dir); } else if (strcmp(type, "Erupter") == 0) { object = new Erupter(name, media, x, y, dir); } else if (strcmp(type, "Guardian") == 0) { object = new Guardian(name, media, x, y, dir); } else if (strcmp(type, "Shooter") == 0) { object = new Shooter(name, media, x, y, dir); } else if (strcmp(type, "Diver") == 0) { object = new Diver(name, media, x, y, dir); } else if (strcmp(type, "Hovering") == 0) { if (obj) { object = new Hovering(name, media, x, y, obj->GetWidth(), dir); } } else if (strcmp(type, "Floater") == 0) { object = new Floater(name, media, x, y, dir); } else if (strcmp(type, "Swimmer") == 0) { object = new Swimmer(name, media, x, y, dir); } else if (strcmp(type, "Haunter") == 0) { object = new Haunter(name, media, x, y, obj->GetWidth(), obj->GetHeight(), dir); } else if (strcmp(type, "Waver") == 0) { object = new Waver(name, media, x, y, dir); } else if (strcmp(type, "Dancer") == 0) { object = new Dancer(name, media, x, y, dir); } else if (strcmp(type, "Roller") == 0) { object = new Roller(name, media, x, y, obj->GetPolyline()); } else if (strcmp(type, "Falling") == 0) { object = new Falling(name, media, x, y, dir); } else if (strcmp(type, "Obstacle") == 0) { object = new Obstacle(name, media, x, y, dir); } else if (strcmp(type, "MekaDragon") == 0) { object = new MekaDragon(name, media, x, y, dir); } else if (strcmp(type, "MummyDragon") == 0) { object = new MummyDragon(name, media, x, y, dir); } else if (strcmp(type, "Area") == 0) { if (obj) { const Tmx::PropertySet prop = obj->GetProperties(); std::string tn = prop.GetLiteralProperty(std::string("type")); object = new Area(name, media, tn.c_str(), x, y, obj->GetWidth(), obj->GetHeight()); if (object) { std::string musicname = prop.GetLiteralProperty(std::string("music")); if (musicname != std::string("No such property!")) { object->set_string("music", musicname.c_str()); } } } } else if (strcmp(type, "Chest") == 0) { object = new Chest(name, media, x, y); } if (object) { #ifdef TODO // Causes error now for areas if (!object->get_loaded()) { delete object; object = 0; } #endif // Initialize all attributes in tmx mapfile if (obj) { const Tmx::PropertySet prop = obj->GetProperties(); std::map<std::string, std::string> pmap = prop.GetList(); for (std::map<std::string, std::string>::const_iterator it = pmap.begin(); it != pmap.end(); ++it) { std::string attr_name = it->first; if (attr_name != std::string("direction") && attr_name != std::string("music")) { object->set_attribute(attr_name.c_str(), atoi(it->second.c_str())); } } } object->initialize(); } return object; }
Object* ObjectFactory::create_object(const char *name, MediaDB *media, const char *type, int x, int y, int w, int h, Object::Direction dir, const Tmx::PropertySet &prop) { Object *object = 0; if (strcmp(type, "Player") == 0) { TiXmlDocument doc(name); if (doc.LoadFile()) { search_nodes(&doc); } if (strcmp(priv_object_type, "knight") == 0) { object = new Knight(name, media, x, y, dir); } else if (strcmp(priv_object_type, "dragon") == 0) { object = new Dragon(name, media, x, y, dir); } else { object = new Player(name, media, x, y, dir); } } else if (strcmp(type, "Collectable") == 0) { TiXmlDocument doc(name); if (doc.LoadFile()) { search_nodes(&doc); } if (strcmp(priv_object_type, "coin") == 0) { object = new Coin(name, media, x, y); } } else if (strcmp(type, "Item") == 0) { TiXmlDocument doc(name); if (doc.LoadFile()) { search_nodes(&doc); } if (strcmp(priv_object_type, "key") == 0) { object = new Key(name, media, x, y); } else if (strcmp(priv_object_type, "arm") == 0) { object = new Arm(name, media, x, y); } else if (strcmp(priv_object_type, "shield") == 0) { object = new Shield(name, media, x, y); } else if (strcmp(priv_object_type, "armour") == 0) { object = new Armour(name, media, x, y); } } else if (strcmp(type, "Walker") == 0) { object = new Walker(name, media, x, y, dir); } else if (strcmp(type, "Crawler") == 0) { object = new Crawler(name, media, x, y, dir); } else if (strcmp(type, "Erupter") == 0) { object = new Erupter(name, media, x, y, dir); } else if (strcmp(type, "Guardian") == 0) { object = new Guardian(name, media, x, y, dir); } else if (strcmp(type, "LockFlyer") == 0) { object = new LockFlyer(name, media, x, y, dir); } else if (strcmp(type, "Dancer") == 0) { object = new Dancer(name, media, x, y, dir); } else if (strcmp(type, "Falling") == 0) { object = new Falling(name, media, x, y, dir); } else if (strcmp(type, "MekaDragon") == 0) { object = new MekaDragon(name, media, x, y, dir); } else if (strcmp(type, "Area") == 0) { std::string tn = prop.GetLiteralProperty(std::string("type")); object = new Area(name, media, tn.c_str(), x, y, w, h); } else if (strcmp(type, "Chest") == 0) { object = new Chest(name, media, x, y); } if (object) { #ifdef TODO // Causes error now for areas if (!object->get_loaded()) { delete object; object = 0; } #endif std::map<std::string, std::string> pmap = prop.GetList(); for (std::map<std::string, std::string>::const_iterator it = pmap.begin(); it != pmap.end(); ++it) { std::string attr_name = it->first; if (attr_name != std::string("direction") && attr_name != std::string("music")) { object->set_attribute(attr_name.c_str(), atoi(it->second.c_str())); } } object->initialize(); } return object; }