coment::Entity EntityFactory::createExplosion(coment::World& world, float x, float y, float scale)
{
    coment::Entity entity = world.createEntity();

    world.addComponent(entity, Position(x, y));

    Sprite sprite;
    sprite.name = "explosion";
    sprite.color = Color({ 1.0f, 0.84f, 0.0f, 0.5f });
    sprite.scale = scale;
    sprite.layer = Sprite::Layer::PARTICLES;
    world.addComponent(entity, sprite);

    world.addComponent(entity, Expires(0.5f));

    ScaleAnimation scaleAnimation;
    scaleAnimation.active = true;
    scaleAnimation.max = scale;
    scaleAnimation.min = scale / 100.f;
    scaleAnimation.speed = -2.5f;
    scaleAnimation.repeat = false;
    world.addComponent(entity, scaleAnimation);

    return entity;
}
Esempio n. 2
0
str
MicroTime()
{
	struct timeval tv;
	if (gettimeofday(&tv,NULL)) return Expires();
	return _("%i", (tv.tv_sec << 20) + tv.tv_usec);
}
coment::Entity EntityFactory::createPlayerBullet(coment::World& world, float x, float y)
{
    coment::Entity entity = world.createEntity();

    world.addComponent<Position>(entity, Position(x, y));

    Sprite sprite;
    sprite.name = "laser";
    sprite.layer = Sprite::Layer::PARTICLES;
    world.addComponent(entity, sprite);

    world.addComponent(entity, Velocity(0.f, -800.f));
    world.addComponent(entity, Expires(1.5f));
    world.addComponent(entity, Bounds(5.f));

    world.addGroup(entity, "PLAYER_BULLETS");

    return entity;
}
coment::Entity EntityFactory::createParticle(coment::World& world, float x, float y)
{
    coment::Entity entity = world.createEntity();
    //return entity;

    // First working solution with SFML
    //sf::Vector2f velocity(0.f, (float)(std::rand() % 800 - 400));
    //sf::Transform rotationTransform;
    //rotationTransform.rotate((rand() % 36000) / 100.f);
    //velocity = rotationTransform.transformPoint(velocity);

    // Second working solution
    // http://stackoverflow.com/a/5838055/1702695
    float t = 2.f * 3.141592654f * MathUtils::randomFloat();
    float u = MathUtils::randomFloat() + MathUtils::randomFloat();
    float r = (u>1) ? 2.f-u : u;
    Vector2f velocity(r * std::cos(t), r * std::sin(t));
    velocity = velocity * (MathUtils::randomFloat() * 800.f - 400.f);

    // I benchmark it and it's faster than the first solution
    // Bench (in Release mode):
    //  solution 1: 192ms for 1'000'000 iterations
    //  solution 2:  50ms for 1'000'000 iterations

    world.addComponent(entity, Position(x, y));
    world.addComponent(entity, Velocity(velocity.x, velocity.y));
    world.addComponent(entity, Expires(7.f));

    Sprite sprite;
    sprite.name = "particle";
    sprite.color = Color({1.0f, 0.84f, 0.0f, 0.5f});
    sprite.layer = Sprite::Layer::PARTICLES;
    world.addComponent(entity, sprite);

    ColorAnimation colorAnimation;
    colorAnimation.animate[ColorAnimation::A] = true;
    colorAnimation.speed[ColorAnimation::A] = -0.2f;
    colorAnimation.min[ColorAnimation::A] = 0.f;
    colorAnimation.max[ColorAnimation::A] = 1.f;
    colorAnimation.repeat = false;
    world.addComponent(entity, colorAnimation);
}