/*PRIVATE FUNCTIONS*/ void Projectile::updateCurrent(sf::Time dt, CommandQueue& commands) { if (isGuided()) { const float approachRate = 200.f; sf::Vector2f newVel = unitVector(approachRate * dt.asSeconds() * targetDirection + getVelocity()); newVel *= getMaxSpeed(); float angle = std::atan2(newVel.y, newVel.x); setRotation(toDegree(angle) + 90.f); setVelocity(newVel); //rotation and velocity set } Entity::updateCurrent(dt, commands); //rotation and velocity drawn on screen }
Projectile::Projectile(Type type, const TextureHolder& textures) : Entity(1) , mType(type) , mSprite(textures.get(Table[type].texture), Table[type].textureRect) , mTargetDirection() { centerOrigin(mSprite); // Add particle system for missiles if (isGuided()) { std::unique_ptr<EmitterNode> smoke(new EmitterNode(Particle::Smoke)); smoke->setPosition(0.f, getBoundingRect().height / 2.f); attachChild(std::move(smoke)); std::unique_ptr<EmitterNode> propellant(new EmitterNode(Particle::Propellant)); propellant->setPosition(0.f, getBoundingRect().height / 2.f); attachChild(std::move(propellant)); } }
/*PUBLIC FUNCTIONS*/ void Projectile::guideTowards(sf::Vector2f position) { assert(isGuided()); targetDirection = unitVector(position - getWorldPosition()); }