2020-01-05 02:04:06 +03:00
|
|
|
#include "SynGame.hpp"
|
2020-01-04 17:32:20 +03:00
|
|
|
|
2020-01-05 02:04:06 +03:00
|
|
|
class Rocket : public PhysicEntity
|
2020-01-04 17:32:20 +03:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
float thrustForce;
|
|
|
|
float rotationForce;
|
|
|
|
float rotation;
|
|
|
|
sf::Texture texture;
|
|
|
|
sf::Sprite sprite;
|
|
|
|
public:
|
2020-01-05 02:04:06 +03:00
|
|
|
ParticleSystem particleSystem;
|
2020-01-04 17:32:20 +03:00
|
|
|
Rocket();
|
|
|
|
void Rotate(float, bool = true);
|
|
|
|
void Thrust(float);
|
2020-01-05 02:04:06 +03:00
|
|
|
void Update(float);
|
2020-01-04 17:32:20 +03:00
|
|
|
void SetThrustForce(float);
|
|
|
|
void SetRotationForce(float);
|
|
|
|
void SetTexture(sf::Texture, float = 1.0);
|
|
|
|
sf::Sprite GetSprite();
|
|
|
|
sf::Vector2f GetLandingPoint(bool = true);
|
2020-01-05 02:04:06 +03:00
|
|
|
void SetPosition(float, float, bool = true);
|
2020-01-04 17:32:20 +03:00
|
|
|
};
|
|
|
|
|
2020-01-05 02:04:06 +03:00
|
|
|
Rocket::Rocket() : PhysicEntity()
|
2020-01-04 17:32:20 +03:00
|
|
|
{
|
|
|
|
thrustForce = rotationForce = rotation = 0.0f;
|
2020-01-05 02:04:06 +03:00
|
|
|
SetGravity();
|
2020-01-04 17:32:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Rocket::Rotate(float deltaTime, bool isRight)
|
|
|
|
{
|
|
|
|
if(!isActive) return;
|
|
|
|
|
|
|
|
rotation += (isRight ? rotationForce : -rotationForce) * deltaTime;
|
2020-01-05 02:04:06 +03:00
|
|
|
|
|
|
|
if(rotation > RotationLimit)
|
|
|
|
rotation = RotationLimit;
|
|
|
|
else if(rotation < -RotationLimit)
|
|
|
|
rotation = -RotationLimit;
|
|
|
|
|
2020-01-04 17:32:20 +03:00
|
|
|
sprite.setRotation(rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rocket::Thrust(float deltaTime)
|
|
|
|
{
|
|
|
|
if(!isActive) return;
|
|
|
|
|
|
|
|
float force = thrustForce * deltaTime;
|
|
|
|
velocity.x += sin(rotation * DegToRad) * force;
|
|
|
|
velocity.y += cos(rotation * DegToRad) * force;
|
2020-01-07 19:44:40 +03:00
|
|
|
particleSystem.Play();
|
2020-01-04 17:32:20 +03:00
|
|
|
}
|
|
|
|
|
2020-01-05 02:04:06 +03:00
|
|
|
void Rocket::Update(float deltaTime)
|
2020-01-04 17:32:20 +03:00
|
|
|
{
|
2020-01-05 02:04:06 +03:00
|
|
|
PhysicEntity::Update(deltaTime);
|
2020-01-07 19:44:40 +03:00
|
|
|
|
|
|
|
particleSystem.SetPosition(GetLandingPoint(false));
|
|
|
|
particleSystem.SetRotation(rotation);
|
|
|
|
particleSystem.SetLocalVelocity(velocity);
|
2020-01-05 02:04:06 +03:00
|
|
|
particleSystem.Update(deltaTime);
|
2020-01-07 19:44:40 +03:00
|
|
|
|
2020-01-04 17:32:20 +03:00
|
|
|
sprite.setPosition(GetPosition());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rocket::SetThrustForce(float thrustForce)
|
|
|
|
{
|
|
|
|
this -> thrustForce = thrustForce;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rocket::SetRotationForce(float rotationForce)
|
|
|
|
{
|
|
|
|
this -> rotationForce = rotationForce;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Rocket::SetTexture(sf::Texture texture, float size)
|
|
|
|
{
|
|
|
|
this -> texture = texture;
|
|
|
|
sprite = sf::Sprite(this -> texture);
|
|
|
|
sprite.setScale(size, size);
|
|
|
|
sprite.setOrigin(texture.getSize().x / 2, texture.getSize().y / 2);
|
|
|
|
}
|
|
|
|
|
2020-01-05 02:04:06 +03:00
|
|
|
void Rocket::SetPosition(float x, float y, bool inverse)
|
2020-01-04 17:32:20 +03:00
|
|
|
{
|
2020-01-05 02:04:06 +03:00
|
|
|
Entity::SetPosition(x, y, inverse);
|
|
|
|
sprite.setPosition(position);
|
2020-01-04 17:32:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sf::Sprite Rocket::GetSprite()
|
|
|
|
{
|
|
|
|
return sprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Vector2f Rocket::GetLandingPoint(bool inverse)
|
|
|
|
{
|
|
|
|
sf::Vector2f result = position;
|
|
|
|
result.x -= sin(rotation * DegToRad) * texture.getSize().y * sprite.getScale().y / 2;
|
|
|
|
result.y -= cos(rotation * DegToRad) * texture.getSize().y * sprite.getScale().y / 2;
|
|
|
|
if(inverse) result.y *= -1;
|
|
|
|
return result;
|
|
|
|
}
|