コード例 #1
0
void AsteroidsGenerator::generate(Asteroid& asteroid,
                                  const Point* const position) const
{
    Point pointFrom, pointTo;
    getPoints(pointFrom, pointTo);

    if (position != 0) {
        asteroid.generate(edgeCountMin_,
                         edgeCountMax_,
                         radiusMin_ / 2.0f,
                         radiusMax_ / 2.0f);
        pointFrom = *position;
    }
    else {
        asteroid.generate(edgeCountMin_,
                          edgeCountMax_,
                          radiusMin_,
                          radiusMax_);
    }
    
    asteroid.setPosition(pointFrom);

    pointTo.x -= pointFrom.x;
    pointTo.y -= pointFrom.y;

    const float velocity = velocityMin_ + rand() % int(velocityMax_ - velocityMin_);
    const float angularVelocity = velocityMin_ + rand() % int(velocityMax_ - velocityMin_);

    if (position != 0) {
        asteroid.setAngularVelocity(2.0f * angularVelocity);
        asteroid.setGeneration(2);

        PointFunctions::normalize(pointTo, 2.0f * velocity);
    }
    else {
        asteroid.setAngularVelocity(angularVelocity);
        asteroid.setGeneration(1);

        PointFunctions::normalize(pointTo, velocity);
    }

    asteroid.setVelocity(pointTo.x, pointTo.y);
}
コード例 #2
0
ファイル: spacewar.cpp プロジェクト: Cusimonster/Asteroids
//----------------------------------------
// activate new Asteroid
//----------------------------------------
void Spacewar::spawnAsteroid(VECTOR2 pos, VECTOR2 vel)
{
	Asteroid* first = nullptr;
	for(int i=0; i<MAX_ASTEROIDS; i++)
	{
		if(!(asteroids[i].getActive()))
		{
			first = &asteroids[i];
			break;
		}
	}
	if(first != nullptr)
	{
		first->setActive(true);
		first->setVelocity(vel);
		first->setX(pos.x);
		first->setY(pos.y);
		first->setDegrees(rand());
	}
}