예제 #1
0
ATHObject* ObjectGenerator::GeneratePlanet(float2 _fPos, float _fMinRadius, float _fMaxRadius, float3 _fColor)
{
	// The new Object
	Planet* pNewObject = new Planet();

	// Decide stats
	int nSlotCount = GetNextPlanetSlotCount();
	pNewObject->SetProperty("structure-slot-count", &nSlotCount, APT_INT);
	float fPlanetRadius = PLANET_SLOT_LENGTH / (2.0f * sin(3.141592f / nSlotCount));
	pNewObject->SetProperty("radius", &fPlanetRadius, APT_FLOAT);

	// Set the mass
	float fDensity = 1.0f;
	float fArea = b2_pi * fPlanetRadius * fPlanetRadius;
	pNewObject->SetMass(fArea * fDensity);

	// Create the b2Body
	b2BodyDef bodyDef;
	bodyDef.position = b2Vec2(_fPos.vX, _fPos.vY);
	bodyDef.type = b2_kinematicBody;
	
	b2Body* pPlanetBody = m_pObjectManager->m_pWorld->CreateBody(&bodyDef);

	// Create the planet fixture
	b2FixtureDef fixtureDef;
	b2CircleShape planetCircleShape;

	planetCircleShape.m_radius = fPlanetRadius;
	fixtureDef.shape = &planetCircleShape;
	pPlanetBody->CreateFixture(&fixtureDef);

	// Create the gravity sensor
	planetCircleShape.m_radius = fPlanetRadius * 5.0f;
	fixtureDef.shape = &planetCircleShape;
	fixtureDef.isSensor = true;
	pPlanetBody->CreateFixture(&fixtureDef);
	pNewObject->SetProperty("gravity-radius", &planetCircleShape.m_radius, APT_FLOAT);

	// Create the image
	ATHRenderNode* pRenderNode = GeneratePlanetTexture(_fColor, fPlanetRadius);

	// Init the game object
	pNewObject->Init(pRenderNode, pPlanetBody);
	m_pObjectManager->AddObject(pNewObject);

	return pNewObject;
}