Exemplo n.º 1
0
    RigidBody2D::RigidBody2D(const RigidBody2D& other, Object& newObj)
        : Collider2D(other, newObj)
    {
        b2BodyDef bd;
        b2FixtureDef fdf;

        auto& pos = getObject()->getGlobalPosition();
        bd.angle = glm::eulerAngles(getObject()->getGlobalRotation()).z;
        bd.position = b2Vec2(pos.x, pos.y);
        bd.userData = this;

        auto om = other.m_body;

        bd.type = om->GetType();
        newObj.setIgnoreParent(other.getObject()->ignoresParent());
        fdf.isSensor = om->GetFixtureList()->IsSensor();
        bd.allowSleep = om->IsSleepingAllowed();

        m_body = other.m_worldRef2D.m_worldData2D->CreateBody(&bd);

        auto omf = om->GetFixtureList();

        fdf.filter = omf->GetFilterData();
        fdf.friction = omf->GetFriction();
        fdf.restitution = omf->GetRestitution();

        fdf.shape = omf->GetShape();
        fdf.density = omf->GetDensity();

        m_body->CreateFixture(&fdf);
    }
Exemplo n.º 2
0
void b2Body::Freeze()
{
	/*
	check !IsFrozenByUser() - to prevent duplicate call DestroyProxy
	check !IsFrozen() - to prevent freeze bodies frozen automatically 
	on reaches the boundary of the world AABB.
	*/
	if (!IsFrozenByUser() && !IsFrozen()) 
	{
		/*
		e_frozenByUserFlag required to prevent unfreeze body, 
		frozen automatically on reaches the boundary of the world AABB.
		*/
		m_flags |= e_frozenByUserFlag;
		m_flags |= e_frozenFlag;
		for (b2Fixture* f = GetFixtureList(); f; f = f->m_next)
		{
			f->DestroyProxy(m_world->m_broadPhase);
		}

		for (b2JointEdge* j = m_jointList; j; j = j->next)
		{
			j->other->Freeze();
		}
	}
}
Exemplo n.º 3
0
	STARTDECL(ph_deleteshape) (Value &fixture_id)
	{
		CheckPhysics();
		auto fixture = fixtures->Get(fixture_id.ival);
		if (fixture)
		{
			auto body = fixture->GetBody();
			body->DestroyFixture(fixture);
			if (!body->GetFixtureList()) world->DestroyBody(body);
			fixtures->Delete(fixture_id.ival);
		}
		return Value();
	}
Exemplo n.º 4
0
void b2Body::UnFreeze()
{
	if (IsFrozenByUser())
	{
		m_flags &= ~e_frozenByUserFlag;
		m_flags &= ~e_frozenFlag;
		for (b2Fixture* f = GetFixtureList(); f; f = f->m_next)
		{
			f->CreateProxy(m_world->m_broadPhase, m_xf);
		}
		for (b2JointEdge* j = m_jointList; j; j = j->next)
		{
			j->other->UnFreeze();
		}
	}
}
Exemplo n.º 5
0
////////////////////////////////////////////////////////////////////////
///
/// @fn bool BonusModifierChangeZone::Apply()
///
/// Applies the real effect on the node
/// returns false if the bonus finished the execution (no time to live)
/// in case it returns false, complete its execution
///
///
/// @return bool
///
////////////////////////////////////////////////////////////////////////
bool BonusModifierChangeZone::Apply()
{
    if(mOwner)
    {
        auto existingModifiers = mOwner->GetModifiers();
        // recherche pour un bonus déjà présent
        for(auto it = existingModifiers.begin(); it != existingModifiers.end(); ++it)
        {
            BonusModifierChangeZone* modifier = dynamic_cast<BonusModifierChangeZone*>(*it);
            if(modifier && modifier != this)
            {
                // resets time to live on the modifier
                modifier->mTimeToLive = mTimeToLive;
                /// return false to indicate that the bonus was not added to the node, even though the 
                /// bonus itself was applied.
                return false;
            }
        }
#if BOX2D_PLAY 

        auto body = mOwner->getPhysicBody();
        if(body)
        {
            for(auto fixture = body->GetFixtureList(); fixture; fixture = fixture->GetNext())
            {
                b2Filter filter = fixture->GetFilterData();
                if(filter.maskBits & CATEGORY_MIDLANE)
                {
                    // removes the wall as categorie that can block the node
                    filter.maskBits &= ~CATEGORY_MIDLANE;
                    fixture->SetFilterData(filter);
                    mFixtures.push_back(fixture);
                }
            }
        }
#endif
    }

    // if any fixtures were found, it means that this node had a modifiation applied
    return mFixtures.size() != 0;
}
Exemplo n.º 6
0
int main(int argc, char** argv)
{
	RenderWindow window(sf::VideoMode(W_WIDTH,W_HEIGHT),"Hello",sf::Style::Close);
		
	View view{ FloatRect{0,0,V_WIDTH,V_HEIGHT} };
	View defaultView = View{ FloatRect{ 0,0, 1600, 1200 } };

	//window.setView(view);

	Physics physics{ {0.f, 9.8f}, 10.f };

	//LightMap lights{view};
	LightMap lights{ defaultView };

	auto playerLight = lights.createLight("../assets/lightmask.png", 400, 370, 3.f);

	//lights.createLight("../assets/lightmask.png", 200, 100, 3.f);

	Clock clock;

	physics.spawnStaticBox(40.f, 60.f, 800.f, 40.f);
	physics.spawnStaticBox(80.f, 110.f, 800.f, 40.f);
	
	auto pPlayerBody = physics.spawnDynamicCircle(3.f, 400.f, 0.f);
	
	Texture tex;
	tex.loadFromFile("../assets/tile.png");
	tex.setSmooth(true);

	Shader blendShader;

	if (!blendShader.loadFromFile("../src/test.frag", Shader::Fragment))
		std::cout << "Failed to load blend fragment shader" << std::endl;

	Texture playerTex;
	playerTex.loadFromFile("../assets/player.png");
	playerTex.setSmooth(true);

	sf::Event ev;

	Texture worldTex;
	worldTex.create(W_WIDTH, W_HEIGHT);
	Sprite displaySprite;

	Font font;
	font.loadFromFile("../assets/kenney_bold.ttf");

	Text framerate;
	framerate.setFont(font);
	framerate.setCharacterSize(40);
	framerate.setPosition(1100, 200);
	framerate.setColor(sf::Color::White);

	while (window.isOpen())
	{
		while (window.pollEvent(ev))
		{
			if (ev.type == Event::Closed)
			{
				window.close();
			}

			if (ev.type == Event::KeyPressed)
			{
				if (ev.key.code == sf::Keyboard::Left)
				{	
					if (pPlayerBody->GetLinearVelocity().x - (MS_X / 100) > -(2 * MS_X / 100))
						pPlayerBody->ApplyForceToCenter({-MS_X, 0.f},true);
					else
						pPlayerBody->ApplyForceToCenter({ -(2 * MS_X / 100) - pPlayerBody->GetLinearVelocity().x , 0.f }, true);
				}

				if (ev.key.code == sf::Keyboard::Right)
				{
					if (pPlayerBody->GetLinearVelocity().x + (MS_X / 100) < (2 * MS_X / 100))
						pPlayerBody->ApplyForceToCenter({ MS_X, 0.f }, true);
					else
						pPlayerBody->ApplyForceToCenter({ (2 * MS_X / 100) - pPlayerBody->GetLinearVelocity().x , 0.f }, true);
				}

				if (ev.key.code == sf::Keyboard::Space)
				{
					if (pPlayerBody->GetLinearVelocity().y + (MS_X / 100)  < (2 * MS_X / 100))
						pPlayerBody->ApplyForceToCenter({ 0.f, -MS_X * 2 }, true);
					//else
						//pPlayerBody->ApplyForceToCenter({ 0.f, (3 * MS_X / 100) - pPlayerBody->GetLinearVelocity().y }, true);
				}
			}

			if (ev.type == Event::JoystickButtonPressed)
			{
				std::cout << ev.joystickButton.button << std::endl;
			}

			if (ev.type == Event::JoystickMoved)
			{
				std::cout << ev.joystickMove.axis << std::endl;
			}
		}

		physics.step(1.f / 30.f);
		
		window.clear();

		auto playerPos = pPlayerBody->GetPosition();
		view.setCenter(SCALE * playerPos.x, SCALE * playerPos.y);
		//window.setView(view);

		auto lightSize = playerLight->getSprite().getTexture()->getSize();

		// 3.f is the player body radius
		playerLight->setPosition(
			playerPos.x * SCALE - lightSize.x - 3.f * SCALE * 2
			, playerPos.y * SCALE - lightSize.y - 3.f * SCALE * 2);

		auto bodyIt = physics.getBodyList();

		//lights.updateView(view);
		lights.render();

		while (bodyIt != nullptr)
		{
			auto pos = bodyIt->GetPosition();

			if (bodyIt->GetType() == b2_dynamicBody)
			{
				CircleShape sprite;
							
				sprite.setTexture(&playerTex);

				auto playerShape = *(b2CircleShape*)(bodyIt->GetFixtureList()->GetShape());

				sprite.setRadius(playerShape.m_radius * SCALE);
				
				sprite.setOrigin(playerShape.m_radius * SCALE, playerShape.m_radius * SCALE);
				
				sprite.setPosition(SCALE * pos.x, SCALE * pos.y);

				sprite.setRotation(bodyIt->GetAngle() * 180 / b2_pi);
				
				window.draw(sprite);
			}

			else // ground
			{
				RectangleShape sprite;
				
				sprite.setSize({800.f, 40.f});
				
				sprite.setOrigin(400, 20);

				sprite.setPosition(pos.x * SCALE, pos.y * SCALE);

				sprite.setRotation(bodyIt->GetAngle() * 180 / b2_pi);

				sprite.setTexture(&tex);

				window.draw(sprite);
			}
			
			bodyIt = bodyIt->GetNext();
		}

		window.draw(Sprite{ lights.getLightMapTexture() }, BlendMultiply);

		float frameTime = 1.f / clock.getElapsedTime().asSeconds();
		clock.restart();
		framerate.setString(std::to_string((int)frameTime) + " fps");

		window.draw(framerate);

		window.display();
	}
}
void CommandMoveLeft::update(float dt) {
	if (!_block)
		return;

	auto body1 = _block->getBody();
	if (!body1) {
		stopBlock();
		return;
	}
	auto body2 = _block->getAttachedBody();
	if (!body2) {
		stopBlock();
		return;
	}
	float32 offset = ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::MOVEOFFSET);
	float32 blockSizeInMeters = _blockSize.width / ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::SCALE_RATIO_BOX2D);

	if (body1->GetFixtureList()->GetFilterData().categoryBits == Block::blockFlags::PASSIVE
		|| body2->GetFixtureList()->GetFilterData().categoryBits == Block::blockFlags::PASSIVE)
		stopBlock();
	
	if (body1->GetFixtureList()->GetFilterData().categoryBits == Block::blockFlags::NEED_TO_STOP
		|| body2->GetFixtureList()->GetFilterData().categoryBits == Block::blockFlags::NEED_TO_STOP) {
		Vec2 posOnField = _block->getPosOnField();
		Sprite *sprite1 = (Sprite*)body1->GetUserData();
		Sprite *sprite2 = (Sprite*)body2->GetUserData();
		Size size = sprite1->getContentSize();
		b2Filter filter;

		if (GameField::getBlock({ posOnField.x - 1, posOnField.y })){
			stopBlock();

			filter = body1->GetFixtureList()->GetFilterData();
			filter.categoryBits = Block::blockFlags::ACTIVE;
			body1->GetFixtureList()->SetFilterData(filter);

			filter = body2->GetFixtureList()->GetFilterData();
			filter.categoryBits = Block::blockFlags::ACTIVE;
			body2->GetFixtureList()->SetFilterData(filter);

			body1->SetTransform(_positionOldFirst, 0);
			body2->SetTransform(_positionOldSecond, 0);

			sprite1->setPosition({ (body1->GetPosition().x 
					* ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::SCALE_RATIO_BOX2D)) - size.width / 2
				, (body1->GetPosition().y 
					* ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::SCALE_RATIO_BOX2D)) - size.height / 2 });
			sprite2->setPosition({ (body2->GetPosition().x 
					* ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::SCALE_RATIO_BOX2D)) - size.width / 2
				, (body2->GetPosition().y 
					* ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::SCALE_RATIO_BOX2D)) - size.height / 2 });

			SimpleUI *simpleUI = MainGameScene::getUI();
			UserInput *input = (UserInput*)simpleUI->getChildrenByName(UserInput::name());
			input->dropInputEvents();

			return;
		}

		if (!_isUndo) {
			body1->SetTransform({ _positionOldFirst.x - blockSizeInMeters, body1->GetPosition().y }, 0);
			body2->SetTransform({ _positionOldSecond.x - blockSizeInMeters, body2->GetPosition().y }, 0);
		}
		else {
			body1->SetTransform({ _positionOldFirst.x + blockSizeInMeters, body1->GetPosition().y }, 0);
			body2->SetTransform({ _positionOldSecond.x + blockSizeInMeters, body2->GetPosition().y }, 0);
		}
		sprite1->setPosition({ (body1->GetPosition().x 
				* ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::SCALE_RATIO_BOX2D)) - size.width / 2
			, (body1->GetPosition().y 
				* ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::SCALE_RATIO_BOX2D)) - size.height / 2 });
		sprite2->setPosition({ (body2->GetPosition().x 
				* ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::SCALE_RATIO_BOX2D)) - size.width / 2
			, (body2->GetPosition().y 
				* ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::SCALE_RATIO_BOX2D)) - size.height / 2 });

		stopBlock();

		filter = body1->GetFixtureList()->GetFilterData();
		filter.categoryBits ^= Block::blockFlags::NEED_TO_STOP;
		filter.categoryBits = Block::blockFlags::STOPPED;
		body1->GetFixtureList()->SetFilterData(filter);
		
		filter = body2->GetFixtureList()->GetFilterData();
		filter.categoryBits ^= Block::blockFlags::NEED_TO_STOP;
		filter.categoryBits = Block::blockFlags::STOPPED;
		body2->GetFixtureList()->SetFilterData(filter);

		body1->SetActive(false);
		body1->SetActive(true);
		body2->SetActive(false);
		body2->SetActive(true);
	}

	if ((_positionOldFirst.x - body1->GetPosition().x) < blockSizeInMeters
			&& !_isUndo
			&& _isExecute) {
		if ((_positionOldFirst.x - (body1->GetPosition().x - offset)) >= blockSizeInMeters) {
			while (offset > 0) {
				offset -= ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::MOVEOFFSET) / 100;
				if ((_positionOldFirst.x - (body1->GetPosition().x - offset)) < blockSizeInMeters) {
					stopBlock();
					body1->SetTransform({ body1->GetPosition().x - offset, body1->GetPosition().y }, 0);
					body2->SetTransform({ body2->GetPosition().x - offset, body2->GetPosition().y }, 0);
					return;
				}
			}
		}
		CCASSERT(offset, "offset can't be 0");
		if (body1->GetLinearVelocity().x > 0
			&& (_positionOldFirst.x - body1->GetPosition().x) < (blockSizeInMeters / 7)) {  
			stopBlock();
			body1->SetTransform(_positionOldFirst, 0);
			body2->SetTransform(_positionOldSecond, 0);
			return;
		}
		body1->SetLinearVelocity({ (float32)(body1->GetLinearVelocity().x - offset), body1->GetLinearVelocity().y });
		body2->SetLinearVelocity({ (float32)(body2->GetLinearVelocity().x - offset), body2->GetLinearVelocity().y });
		_positionOldFirst.y = body1->GetPosition().y;
		_positionOldSecond.y = body2->GetPosition().y;
	}
	else if ((body1->GetPosition().x - _positionOldFirst.x) < blockSizeInMeters
				&& _isUndo
				&& _isExecute) {
		if (((body1->GetPosition().x + offset) - _positionOldFirst.x) >= blockSizeInMeters) {
			while (offset > 0) {
				offset -= (ConstantsRegistry::getValueForKey(ConstantsRegistry::constants::MOVEOFFSET) / 100) * 10;
				if (((body1->GetPosition().x + offset) - _positionOldFirst.x) < blockSizeInMeters) {
					stopBlock();
					body1->SetTransform({ body1->GetPosition().x + offset, body1->GetPosition().y }, 0);
					body2->SetTransform({ body2->GetPosition().x + offset, body2->GetPosition().y }, 0);
					return;
				}
			}
		}
		CCASSERT(offset, "offset can't be 0");
		if (body1->GetLinearVelocity().x < 0
			&& (_positionOldFirst.x - body1->GetPosition().x) < (blockSizeInMeters / 7)) {
			stopBlock();
			body1->SetTransform(_positionOldFirst, 0);
			body2->SetTransform(_positionOldSecond, 0);
			return;
		}
		body1->SetLinearVelocity({ (float32)(body1->GetLinearVelocity().x + offset), body1->GetLinearVelocity().y });
		body2->SetLinearVelocity({ (float32)(body2->GetLinearVelocity().x + offset), body2->GetLinearVelocity().y });
		_positionOldFirst.y = body1->GetPosition().y;
		_positionOldSecond.y = body2->GetPosition().y;
	}
	else
		stopBlock();
}