IEntity* GroundFactory::create(const EntityProperties& properties) { b2BodyDef bodyDef = b2BodyDef(); bodyDef.position.Set(pixel2meter(properties.x), pixel2meter(properties.y)); bodyDef.angle = radian2degree(properties.angle); bodyDef.type = b2_staticBody; b2Body *body = GameWorld::getInstance()->getPhysicsWorld()->CreateBody(&bodyDef); b2PolygonShape polygonShape; polygonShape.SetAsBox(pixel2meter(properties.width / 2), pixel2meter(properties.height / 2)); b2FixtureDef fixtureDef; fixtureDef.shape = &polygonShape; fixtureDef.density = properties.density; fixtureDef.friction = properties.friction; fixtureDef.restitution = properties.restitution; fixtureDef.filter.categoryBits = COLLISION_BIT_GROUND; fixtureDef.filter.maskBits = COLLISION_BIT_BLOBBY | COLLISION_BIT_WEAPON | COLLISION_BIT_GROUND | COLLISION_BIT_OBJECT | COLLISION_BIT_BULLET; body->CreateFixture(&fixtureDef); body->ResetMassData(); Ground *ground = new Ground(); ground->addBody(body); return ground; }
Personaje::Personaje(Mundo* mundo, Uint8 numero_jugador, char* nombre_client) { angulo_arma = 0; conectado = 1; arma_seleccionada =0; energia = ENERGIA_WORM; nombre_cliente = nombre_client; muerto = false; shape = NULL; radio = 0; dir_imagen = "TPTaller/imagenes/gusanitoderecha.png"; nro_jugador = numero_jugador; b2Vec2 escalas = mundo->GetEscalas(); alto = escalas.y / 50; ancho = escalas.x / 60; b2Vec2* vertices_tierra = mundo->GetVerticesTierra(); bool posicion_correcta = false; b2Vec2 posicion; //b2Vec2 aux=b2Vec2(10, 10); while (!posicion_correcta){ posicion.x+=10; //aux+=10; int indice = random( (mundo->GetCantVerticesTierra())); posicion = vertices_tierra[indice] - b2Vec2(0,alto/2); if (posicion.y < mundo->getAgua()->GetNivel()){ posicion_correcta=true; } } b2World* world = mundo->devolver_world(); b2BodyDef bodyDef = b2BodyDef(); // creo el body def bodyDef.position = posicion; // le asigno una posicion bodyDef.userData = this; // no se si funciona bien esto, bodyDef.type = b2_dynamicBody; body = world->CreateBody(&bodyDef); b2MassData massData = b2MassData(); massData.mass = 0.1; massData.center = b2Vec2(0, 0); massData.I = RECT_INERCIA_ROT; body->SetMassData(&massData); shape2 = new b2CircleShape(); shape2->m_radius = ancho/2; b2Filter filtro = b2Filter(); filtro.groupIndex = INDICE_GRUPO; b2FixtureDef fd; // creo un fixture fd.filter = filtro; fd.restitution = 0; fd.friction = 0.5; fd.shape = shape2; body->CreateFixture(&fd); // al body le pongo la fixture creada body->SetAwake(false); for (int i = 0 ; i<4; i++){ seleccionado[i] = false; } orientacion = 1; daniado_turno_actual = false; this->movio = 0; this->salto = 0; }
EntityFactory::EntityFactory(b2World& b2world, BehaviourFactory* _bf, DrawableContainer* _drawContainer) : world(b2world), bf(_bf), drawContainer(_drawContainer) { entityRegistery = std::unordered_map<EntityType, Entity*>{ { EntityType::ENTITY, new Entity() }, { EntityType::ACTOR, new Actor() }, { EntityType::NPC, new Npc() }, { EntityType::PLAYER, new Player() }, { EntityType::PLANT, new Npc() }, { EntityType::GROUND, new Ground() }, { EntityType::GROUND2, new Ground() }, { EntityType::BAR, new Ground() } }; b2BodyDef entDef = b2BodyDef(); entDef.type = b2BodyType::b2_staticBody; entDef.fixedRotation = true; b2BodyDef ActorDef; ActorDef.type = b2BodyType::b2_dynamicBody; b2BodyDef NpcDef; NpcDef.type = b2BodyType::b2_dynamicBody; b2BodyDef PlayerDef; PlayerDef.gravityScale = 1; PlayerDef.fixedRotation = true; PlayerDef.linearDamping = 0.5f; PlayerDef.angularDamping = 1; PlayerDef.type = b2BodyType::b2_dynamicBody; b2BodyDef PlantDef = b2BodyDef(); PlantDef.gravityScale = 1; PlantDef.fixedRotation = true; PlantDef.linearDamping = 0.5f; PlantDef.angularDamping = 1; PlantDef.type = b2BodyType::b2_dynamicBody; bodyRegistery = std::unordered_map<EntityType, b2BodyDef>{ { EntityType::ENTITY, entDef }, { EntityType::ACTOR, ActorDef }, { EntityType::NPC, NpcDef }, { EntityType::PLAYER, PlayerDef }, { EntityType::PLANT, PlantDef }, { EntityType::GROUND, entDef }, { EntityType::GROUND2, entDef }, { EntityType::BAR, entDef } }; }
TileMap::TileMap(Game& world, TextureManager& textureManager, const TMX& tmx) : BaseGameEntity(world, b2BodyDef()) , mWorld(world) , mTextures() , mpCollider(nullptr) , mpNavGraph(nullptr) , mDrawFlags(0) , mCellSpaceNeighborhoodRange(1) , mpCellSpacePartition(nullptr) { setDrawOrder(std::numeric_limits<int>::max()); std::vector<std::vector<sf::VertexArray>> layers; tmx.makeVertices(textureManager, mTextures, layers); for (auto it = layers.begin(); it != layers.end(); ++it) { int index = it - layers.begin(); if (it->size() != mTextures.size()) { throw std::runtime_error("Texture and layer component counts are inconsistent."); } auto pLayer = std::make_unique<Layer>(mWorld, std::move(*it), mTextures); pLayer->setDrawOrder(index); attachNode(std::move(pLayer)); } const sf::Transform& transform = getWorld().getPixelToWorldTransform(); mpCollider = std::unique_ptr<CompositeCollider>(tmx.makeCollider(transform)); mpNavGraph = std::unique_ptr<NavGraph>(tmx.makeNavGraph(transform)); mCellSpaceNeighborhoodRange = calculateAverageGraphEdgeLength(*mpNavGraph) + 1; mpCellSpacePartition = std::make_unique<NavCellSpace>((float)tmx.getTileWidth() / tmx.getWidth(), (float)tmx.getTileHeight() * tmx.getHeight(), tmx.getWidth() / 4, tmx.getHeight() / 4, mpNavGraph->numNodes()); TileMap::NavGraph::ConstNodeIterator nodeIter(*mpNavGraph); for (const TileMap::NavGraph::Node* pNode = nodeIter.begin(); !nodeIter.end(); pNode = nodeIter.next()) { mpCellSpacePartition->addEntity(pNode); } std::vector<b2Fixture*> fixtures; mpCollider->createFixtures(getBody(), fixtures); }
void Box2DPhysicsComponent::createCollisionBody( b2World* physics, GameObject* object, b2BodyType type, bool sensorOnly ) { // Create instances mBodyDef = b2BodyDef(); mPolygonShape = b2PolygonShape(); mFixtureDef = b2FixtureDef(); // Set type mBodyDef.type = type; // Set position and create the body mBodyDef.position.Set( object->getPosition().x / SCALE, object->getPosition().y / SCALE ); mBody = physics->CreateBody( &mBodyDef ); /* * SHAPE * * Dynamic bodies get a standard polygon box shape. * * Static and kinematic bodies get a a rectangular shape made of * egde chains (one per side; so four total) to fix the ghost vertices * problem. This is done by defining previous and next vertices that * Box2D can use in its calculation. * * More info: * http://www.box2d.org/manual.html#_Toc258082970 * http://www.iforce2d.net/b2dtut/ghost-vertices * */ if ( type != b2_dynamicBody ) { // Top b2Vec2 vs[2]; vs[0].Set( (-object->getSize().x / 2.f) / SCALE, -object->getSize().y / 2.f / SCALE ); vs[1].Set( (object->getSize().x / 2.f) / SCALE, -object->getSize().y / 2.f / SCALE ); mChainShape[0].CreateChain( vs, 2 ); mChainShape[0].SetPrevVertex( b2Vec2( (-object->getSize().x - object->getSize().x / 2.f) / SCALE, object->getSize().y / 2.f / SCALE ) ); mChainShape[0].SetNextVertex( b2Vec2( (object->getSize().x + object->getSize().x / 2.f) / SCALE, object->getSize().y / 2.f / SCALE ) ); mBody->CreateFixture( &mChainShape[0], 0.f ); // Bottom vs[0].Set( (-object->getSize().x / 2.f) / SCALE, object->getSize().y / 2.f / SCALE ); vs[1].Set( (object->getSize().x / 2.f) / SCALE, object->getSize().y / 2.f / SCALE ); mChainShape[1].CreateChain( vs, 2 ); mChainShape[1].SetPrevVertex( b2Vec2( (-object->getSize().x - object->getSize().x / 2.f) / SCALE, object->getSize().y / 2.f / SCALE ) ); mChainShape[1].SetNextVertex( b2Vec2( (object->getSize().x + object->getSize().x / 2.f) / SCALE, object->getSize().y / 2.f / SCALE ) ); mBody->CreateFixture( &mChainShape[1], 0.f ); // Left vs[0].Set( (-object->getSize().x / 2.f) / SCALE, -object->getSize().y / 2.f / SCALE ); vs[1].Set( (-object->getSize().x / 2.f) / SCALE, object->getSize().y / 2.f / SCALE ); mChainShape[2].CreateChain( vs, 2 ); mChainShape[2].SetPrevVertex( b2Vec2( -object->getSize().x / 2.f / SCALE, (-object->getSize().y - object->getSize().y / 2.f) / SCALE ) ); mChainShape[2].SetNextVertex( b2Vec2( -object->getSize().x / 2.f / SCALE, (object->getSize().y + object->getSize().y / 2.f) / SCALE ) ); mBody->CreateFixture( &mChainShape[2], 0.f ); // Right vs[0].Set( (object->getSize().x / 2.f) / SCALE, -object->getSize().y / 2.f / SCALE ); vs[1].Set( (object->getSize().x / 2.f) / SCALE, object->getSize().y / 2.f / SCALE ); mChainShape[3].CreateChain( vs, 2 ); mChainShape[3].SetPrevVertex( b2Vec2( object->getSize().x / 2.f / SCALE, (-object->getSize().y - object->getSize().y / 2.f) / SCALE ) ); mChainShape[3].SetNextVertex( b2Vec2( object->getSize().x / 2.f / SCALE, (object->getSize().y + object->getSize().y / 2.f) / SCALE ) ); mBody->CreateFixture( &mChainShape[3], 0.f ); } else { mPolygonShape.SetAsBox( (object->getSize().x / 2.f) / SCALE, (object->getSize().y / 2.f) / SCALE ); mFixtureDef.shape = &mPolygonShape; mFixtureDef.density = 1.f; mFixtureDef.friction = 1.0f; if( sensorOnly ) { mFixtureDef.isSensor = true; } mBody->CreateFixture( &mFixtureDef ); } }
TileMap::Layer::Layer(Game& world, std::vector<sf::VertexArray>&& vas, std::vector<const sf::Texture*>& textures) : BaseGameEntity(world, b2BodyDef()) , mVertexArrays(std::move(vas)) , mTextures(&textures) {}