Exemplo n.º 1
0
void ParticleEmitter::update(float elapsedTime, const ParticleSystemPtr& system)
{
    m_elapsedTime += elapsedTime;

    // check if finished
    if(m_duration > 0 && m_elapsedTime >= m_duration + m_delay) {
        m_finished = true;
        return;
    }

    if(!m_active && m_elapsedTime > m_delay)
        m_active = true;

    if(!m_active)
        return;

    int nextBurst = std::floor((m_elapsedTime - m_delay) * m_burstRate) + 1;
    const ParticleType *type = m_particleType.get();
    for(int b = m_currentBurst; b < nextBurst; ++b) {
        // every burst created at same position.
        float pRadius = stdext::random_range(type->pMinPositionRadius, type->pMaxPositionRadius);
        float pAngle = stdext::random_range(type->pMinPositionAngle, type->pMaxPositionAngle);

        Point pPosition = m_position + Point(pRadius * std::cos(pAngle), pRadius * std::sin(pAngle));

        for(int p = 0; p < m_burstCount; ++p) {
            float pDuration = stdext::random_range(type->pMinDuration, type->pMaxDuration);

            // particles initial velocity
            float pVelocityAbs = stdext::random_range(type->pMinVelocity, type->pMaxVelocity);
            float pVelocityAngle = stdext::random_range(type->pMinVelocityAngle, type->pMaxVelocityAngle);
            PointF pVelocity(pVelocityAbs * std::cos(pVelocityAngle), pVelocityAbs * std::sin(pVelocityAngle));

            // particles initial acceleration
            float pAccelerationAbs = stdext::random_range(type->pMinAcceleration, type->pMaxAcceleration);
            float pAccelerationAngle = stdext::random_range(type->pMinAccelerationAngle, type->pMaxAccelerationAngle);
            PointF pAcceleration(pAccelerationAbs * std::cos(pAccelerationAngle), pAccelerationAbs * std::sin(pAccelerationAngle));

            ParticlePtr particle(new Particle(pPosition, type->pStartSize, type->pFinalSize,
                                                pVelocity, pAcceleration,
                                                pDuration, type->pIgnorePhysicsAfter,
                                                type->pColors, type->pColorsStops,
                                                type->pCompositionMode, type->pTexture));
            system->addParticle(particle);
        }
    }

    m_currentBurst = nextBurst;
}