Beispiel #1
0
bool Neural_SB::accumForce(b2Vec2 & totForce, b2Vec2 force) const
{
    //std::cout << "\t\tTotalForce : " << totForce.x << " " << totForce.y << std::endl;
    b2Vec2 tot = totForce + force;
    if( tot.LengthSquared() <= m_fMaxForce*m_fMaxForce )
    {
        totForce += force;
        return true;
    }

    force.Normalize();
    force *= abs(m_fMaxForce - totForce.Length());
    totForce += force;
    return false;
}
void Flamethrower::onFire(b2Vec2 direction, bool constantFire, bool isPlayer)
{
	direction.Normalize();
	b2Mat22 matrix1 = b2Mat22(degree2radian(float(rand() % 600) / 100.0f));
	b2Mat22 matrix2 = b2Mat22(degree2radian(float(rand() % 600) / 100.0f));
	b2Mat22 matrix3 = b2Mat22(degree2radian(float(rand() % 600 - 300) / 100.0f));
	direction = b2Mul(matrix1, direction);
	b2Vec2 direction2 = b2Mul(matrix2, direction);
	b2Vec2 direction3 = b2Mul(matrix3, direction);
	EntityFactory *entityFactory = new FlamethrowerBulletFactory();
	EntityProperties& properties = entityFactory->getDefaultProperties();
	properties.x = meter2pixel(this->getBody(0)->GetPosition().x);
	properties.y = meter2pixel(this->getBody(0)->GetPosition().y);
	properties.special = true;
	float speed = 0.7f;
	FlamethrowerBullet *flamethrowerBullet = (FlamethrowerBullet*)entityFactory->create(properties);
	flamethrowerBullet->getBody(0)->ApplyForce(speed * 0.75f * direction, this->getBody(0)->GetPosition());
	flamethrowerBullet = (FlamethrowerBullet*)entityFactory->create(properties);
	flamethrowerBullet->getBody(0)->ApplyForce(speed * 0.75f * direction2, this->getBody(0)->GetPosition());
	flamethrowerBullet = (FlamethrowerBullet*)entityFactory->create(properties);
	flamethrowerBullet->getBody(0)->ApplyForce(speed * 0.82f * direction3, this->getBody(0)->GetPosition());
}
bool GameLevelState::update(yam2d::ESContext* _context, float _deltaTime)
{
	compFac->getPhysicsWorld()->Step(_deltaTime, 10, 10);
	map->update(_deltaTime);

	static bool ballHit = false;
	static bool ballHitThisFrame;
	static b2Vec2 directionVec;//Vector from player to ball when hit

	ballHitThisFrame = false;
	//Collisions
	unsigned int numContacts = contactListener->contacts.size();
	for (unsigned int i = 0; i < numContacts; i++)
	{
		const Contact& contact = contactListener->contacts[i];
		yam2d::GameObject* A = ((PhysicsBody*) contact.fixtureA->GetBody()->GetUserData())->getGameObject();
		yam2d::GameObject* B = ((PhysicsBody*) contact.fixtureB->GetBody()->GetUserData())->getGameObject();
		if ((A->getName() == "Ball" && B->getName() == "Player"))
		{
			//Physics
			if (!ballHit)
			{
				//Reverse last movements ( separate objects )
				A->getComponent<PhysicsBody>()->getBody()->SetTransform(b2Vec2(A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - ballVelocity.x, A->getComponent<PhysicsBody>()->getBody()->GetPosition().y - ballVelocity.y), A->getComponent<PhysicsBody>()->getBody()->GetAngle());
				B->getComponent<PhysicsBody>()->getBody()->SetTransform(b2Vec2(B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - playerSpeed.x, B->getComponent<PhysicsBody>()->getBody()->GetPosition().y - playerSpeed.y), B->getComponent<PhysicsBody>()->getBody()->GetAngle());

				//Direction
				directionVec = A->getComponent<PhysicsBody>()->getBody()->GetPosition() - B->getComponent<PhysicsBody>()->getBody()->GetPosition();
				directionVec.Normalize();
				directionVec *= 0.1f;

				//Check for side hits
				if (A->getComponent<PhysicsBody>()->getBody()->GetPosition().x + A->getSizeInTiles().x * 0.5f <
					B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - B->getSizeInTiles().x * 0.5f
					||
					A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - A->getSizeInTiles().x * 0.5f >
					B->getComponent<PhysicsBody>()->getBody()->GetPosition().x + B->getSizeInTiles().x * 0.5f)
				{
					ballVelocity.x = -ballVelocity.x + playerSpeed.x;
					ballVelocity.y -= directionVec.y;
				}
				else //No side hit
				{
					ballVelocity.y = -ballVelocity.y;
				}
				ballVelocity.x -= (playerObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x - ballObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x) / 22.0f;

			ballHit = true;
			ballHitThisFrame = true;
			}
		}
		else if ((B->getName() == "Ball" && A->getName() == "Player"))
		{
			//Physics
			if (!ballHit)
			{
				//Reverse last movements ( separate objects )
				B->getComponent<PhysicsBody>()->getBody()->SetTransform(b2Vec2(B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - ballVelocity.x, B->getComponent<PhysicsBody>()->getBody()->GetPosition().y - ballVelocity.y), B->getComponent<PhysicsBody>()->getBody()->GetAngle());
				A->getComponent<PhysicsBody>()->getBody()->SetTransform(b2Vec2(A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - playerSpeed.x, A->getComponent<PhysicsBody>()->getBody()->GetPosition().y - playerSpeed.y), A->getComponent<PhysicsBody>()->getBody()->GetAngle());

				//Direction
				directionVec = B->getComponent<PhysicsBody>()->getBody()->GetPosition() - A->getComponent<PhysicsBody>()->getBody()->GetPosition();
				directionVec.Normalize();
				directionVec *= 0.1f;

				//Check for side hits
				if (B->getComponent<PhysicsBody>()->getBody()->GetPosition().x + B->getSizeInTiles().x * 0.5f <
					A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - A->getSizeInTiles().x * 0.5f
					||
					B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - B->getSizeInTiles().x * 0.5f >
					A->getComponent<PhysicsBody>()->getBody()->GetPosition().x + A->getSizeInTiles().x * 0.5f)
				{
					ballVelocity.x = -ballVelocity.x + playerSpeed.x;
					ballVelocity.y -= directionVec.y;
				}
				else //No side hit
				{
					ballVelocity.y = -ballVelocity.y;
				}
				ballVelocity.x -= (playerObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x - ballObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x) / 22.0f;

			ballHit = true;
			ballHitThisFrame = true;
			}
		}

		if ((A->getName() == "Ball" && B->getName() == "Tile"))
		{
			//Physics
			if (!ballHit)
			{
				//Check for side hits
				if (A->getComponent<PhysicsBody>()->getBody()->GetPosition().x + A->getSizeInTiles().x * 0.5f <
					B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - B->getSizeInTiles().x * 0.5
					||
					A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - A->getSizeInTiles().x * 0.5f >
					B->getComponent<PhysicsBody>()->getBody()->GetPosition().x + B->getSizeInTiles().x * 0.5)
				{
					ballVelocity.x = -ballVelocity.x;
				}
				else
				{
					ballVelocity.y = -ballVelocity.y;
				}
			ballHit = true;
			ballHitThisFrame = true;
			}

			//Tile actions
			map->deleteGameObject(B);
			tileAmount--;
		}
		else if ((B->getName() == "Ball" && A->getName() == "Tile"))
		{
			//Physics
			if (!ballHit)
			{
				//Check for side hits
				if (B->getComponent<PhysicsBody>()->getBody()->GetPosition().x + B->getSizeInTiles().x * 0.5f <
					A->getComponent<PhysicsBody>()->getBody()->GetPosition().x - A->getSizeInTiles().x * 0.5
					||
					B->getComponent<PhysicsBody>()->getBody()->GetPosition().x - B->getSizeInTiles().x * 0.5f >
					A->getComponent<PhysicsBody>()->getBody()->GetPosition().x + A->getSizeInTiles().x * 0.5)
				{
					ballVelocity.x = -ballVelocity.x;
				}
				else
				{
					ballVelocity.y = -ballVelocity.y;
				}
			ballHit = true;
			ballHitThisFrame = true;
			}

			//Tile actionss
			map->deleteGameObject(A);
			tileAmount--;

		}
	}
	if (!ballHitThisFrame)
	{
		ballHit = false;
	}

	//Restrict ball
	if (ballObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x + ballObject->getSizeInTiles().x * 0.5f > tileSize.x
		|| ballObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x - ballObject->getSizeInTiles().x * 0.5f < 0.0f)
	{
		ballVelocity.x = -ballVelocity.x;
	}
	if (ballObject->getComponent<PhysicsBody>()->getBody()->GetPosition().y - ballObject->getSizeInTiles().x * 0.5f < 0.0f)
	{
		ballVelocity.y = -ballVelocity.y;
	}

	//Win-Lose
	if (tileAmount == 0)
	{
		//Win
		textObject->getComponent<yam2d::TextComponent>()->getText()->setText("YOU WIN!");
		textObject->setPosition(tileSize.x * 0.5f, tileSize.y * 0.5f);
		endTimer -= _deltaTime;
	}
	else if (ballObject->getComponent<PhysicsBody>()->getBody()->GetPosition().y > tileSize.y)
	{
		//Lose
		textObject->getComponent<yam2d::TextComponent>()->getText()->setText("YOU LOSE!");
		textObject->setPosition(tileSize.x * 0.5f, tileSize.y * 0.5f);
		endTimer -= _deltaTime;
	}
	if (endTimer <= 0.0f)
	{
		stateManager->setState(new MainMenuState(stateManager));
		return true;
	}
	
	//Input
	if (yam2d::getKeyState(yam2d::KEY_BACK) || yam2d::getKeyState(yam2d::KEY_ESCAPE))
	{
		stateManager->setState(new MainMenuState(stateManager));
		return true;
	}
	playerSpeed = yam2d::vec2(0.0f);
	if (!ballHit)
	{
		if (yam2d::getKeyState(yam2d::KEY_D))
		{
			playerSpeed = yam2d::vec2(0.1f, 0.0f);
		}
		if (yam2d::getKeyState(yam2d::KEY_A))
		{
			playerSpeed = yam2d::vec2(-0.1f, 0.0f);
		}
	}
	playerObject->getComponent<PhysicsBody>()->getBody()->SetTransform(playerObject->getPosition() + playerSpeed, 0.0f);

	//Restrict
	if (playerObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x + playerObject->getSizeInTiles().x * 0.5f  > tileSize.x)
	{
		playerObject->getComponent<PhysicsBody>()->getBody()->SetTransform(playerObject->getPosition() - b2Vec2(0.1f, 0.0f), 0.0f);
	}
	else if (playerObject->getComponent<PhysicsBody>()->getBody()->GetPosition().x - playerObject->getSizeInTiles().x * 0.5f  < 0.0f)
	{
		playerObject->getComponent<PhysicsBody>()->getBody()->SetTransform(playerObject->getPosition() + b2Vec2(0.1f, 0.0f), 0.0f);
	}

	//
	ballObject->getComponent<PhysicsBody>()->getBody()->SetTransform(ballObject->getPosition() + (ballVelocity), 0.0f);
	if (endTimer >= 3.0f)
	{
		textObject->getComponent<yam2d::TextComponent>()->getText()->setText("Tiles Left: " + std::to_string(tileAmount));
	}

	return true;
}