SFML_Experiments/Landing/Rocket.hpp

108 lines
2.6 KiB
C++
Raw Normal View History

// #include <SFML/Graphics.hpp>
// #include <cmath>
#include "SynGame.hpp"
// #include "Particles.hpp"
// #include "PhysicEntity.hpp"
2020-01-04 17:32:20 +03:00
#define DegToRad 0.0174533
#define RotationLimit 45.0
2020-01-04 17:32:20 +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:
ParticleSystem particleSystem;
2020-01-04 17:32:20 +03:00
Rocket();
void Rotate(float, bool = true);
void Thrust(float);
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);
void SetPosition(float, float, bool = true);
2020-01-04 17:32:20 +03:00
};
Rocket::Rocket() : PhysicEntity()
2020-01-04 17:32:20 +03:00
{
thrustForce = rotationForce = rotation = 0.0f;
SetGravity();
2020-01-04 17:32:20 +03:00
}
void Rocket::Rotate(float deltaTime, bool isRight)
{
if(!isActive) return;
rotation += (isRight ? rotationForce : -rotationForce) * deltaTime;
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;
particleSystem.SetPosition(position);
particleSystem.Update(deltaTime);
2020-01-04 17:32:20 +03:00
}
void Rocket::Update(float deltaTime)
2020-01-04 17:32:20 +03:00
{
PhysicEntity::Update(deltaTime);
particleSystem.Update(deltaTime);
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);
}
void Rocket::SetPosition(float x, float y, bool inverse)
2020-01-04 17:32:20 +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;
}