void Level::addCuboid(const rapidxml::xml_node<> &cuboidNode) { osg::Vec3 from = getVectorFromXMLNode("position", cuboidNode); osg::Vec3 size = getVectorFromXMLNode("size", cuboidNode); Cuboid *cuboid = NULL; if(cuboidNode.first_attribute("type") != 0) { std::string type = cuboidNode.first_attribute("type")->value(); if(type == "accelerate") cuboid = new AccelerationCuboid(from, size); else if(type == "decelerate") cuboid = new DecelerationCuboid(from, size); } if(cuboid == NULL) { cuboid = new Cuboid(from, size); } _level->addChild(cuboid->getNode()); _collisionObjects.push_back(cuboid->getRigidBody()); // save minimal x values for buckets of close y values // needed for detection whether player is falling to dead int yBucketIndex = (int)((from.y() + size.y()) / 20.0f); while(_minZValues.size() <= yBucketIndex) _minZValues.push_back(from.z()); // if current cuboid is lower then z -> adjust bucket value if(from.z() < _minZValues[yBucketIndex]) _minZValues[yBucketIndex] = from.z(); }
void Level::addTunnel(const rapidxml::xml_node<> &tunnelNode) { osg::Node *tunnelModel = osgDB::readNodeFile(TUNNEL_MODEL_FILE); osg::PositionAttitudeTransform *tunnelTransform = new osg::PositionAttitudeTransform(); osg::Vec3 position = getVectorFromXMLNode("position", tunnelNode); tunnelTransform->addChild(tunnelModel); tunnelTransform->setPosition(position); tunnelTransform->setScale(osg::Vec3f(1.0f, atof(tunnelNode.first_attribute("length")->value()), 1.0f)); tunnelTransform->setNodeMask(RECEIVE_SHADOW_MASK); _level->addChild(tunnelTransform); /* btConvexTriangleMeshShape* mesh = osgbBullet::btConvexTriMeshCollisionShapeFromOSG(tunnelPat); btTransform shapeTransform; shapeTransform.setIdentity(); cs->addChildShape(shapeTransform, mesh); */ }
void Level::loadMapFromFile(const std::string &mapfile) { // load XML document rapidxml::file<> mf(mapfile.c_str()); rapidxml::xml_document<> xml_doc; xml_doc.parse<0>(mf.data()); // parse XML document for(rapidxml::node_iterator<char> it(xml_doc.first_node()); it.dereference() != NULL; ++it) { CollisionObject *collisionObject = 0; if(strcmp(it->name(), "cuboid") == 0) { osg::Vec3 from = getVectorFromXMLNode("position", *it); osg::Vec3 size = getVectorFromXMLNode("size", *it); if(it->first_attribute("type") == 0) collisionObject = new DefaultCuboid(from, size); else if(std::string(it->first_attribute("type")->value()) == "accelerate") collisionObject = new AccelerationCuboid(from, size); else if(std::string(it->first_attribute("type")->value()) == "decelerate") collisionObject = new DecelerationCuboid(from, size); else collisionObject = new DefaultCuboid(from, size); int yBucketIndex = (int)((from.y() + size.y()) / 20.0f); while((int)_deadlyAltitudes.size() <= yBucketIndex) _deadlyAltitudes.push_back(from.z()); // if current cuboid is lower then z -> adjust bucket value if(from.z() < _deadlyAltitudes[yBucketIndex]) _deadlyAltitudes[yBucketIndex] = from.z(); _shadowedScene->addChild(collisionObject); } else if(strcmp(it->name(), "tunnel") == 0) { collisionObject = new Tunnel(getVectorFromXMLNode("position", *it), atof(it->first_attribute("length")->value())); _shadowedScene->addChild(((CollisionModel *)collisionObject)->getNode()); } else if(strcmp(it->name(), "cuboidtunnel") == 0) { collisionObject = new CuboidTunnel(getVectorFromXMLNode("position", *it), atof(it->first_attribute("length")->value())); _shadowedScene->addChild(((CollisionModel *)collisionObject)->getNode()); } else if(strcmp(it->name(), "finish") == 0) { osg::Vec3 position = getVectorFromXMLNode("position", *it); collisionObject = new Finish(position); _finishs.push_back(position); _shadowedScene->addChild(((CollisionModel *)collisionObject)->getNode()); } else throw std::runtime_error("Error: Unknown element \'" + std::string(it->name()) + "\' in level file!"); if(collisionObject != 0 && strcmp(it->name(), "finish") != 0) _physicsWorld->addRigidBody(collisionObject->getRigidBody()); } }