This commit is contained in:
Asrın Doğan 2020-01-04 17:32:20 +03:00
parent fc5af1441f
commit c1ac111941
5 changed files with 272 additions and 0 deletions

14
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
"files.associations": {
"ios": "cpp",
"system_error": "cpp",
"vector": "cpp",
"xiosbase": "cpp",
"xstring": "cpp",
"xtree": "cpp",
"sstream": "cpp",
"*.m": "cpp",
"cmath": "cpp",
"type_traits": "cpp"
}
}

142
Landing/Rocket.hpp Normal file
View File

@ -0,0 +1,142 @@
#include <SFML/Graphics.hpp>
#include <vector>
#include <cmath>
#define DegToRad 0.0174533
class Rocket
{
private:
float thrustForce;
float rotationForce;
float rotation;
float gravity;
sf::Texture texture;
sf::Sprite sprite;
sf::Vector2f position;
sf::Vector2f velocity;
bool isActive;
void ApplyGravity(float);
void ApplyVelocity(float);
public:
Rocket();
void Update(float);
void Rotate(float, bool = true);
void Thrust(float);
void SetThrustForce(float);
void SetRotationForce(float);
void SetGravity(float);
void SetPosition(float, float, bool = true);
void SetTexture(sf::Texture, float = 1.0);
void SetActive(bool = true);
float GetSpeed();
sf::Sprite GetSprite();
sf::Vector2f GetPosition(bool = true);
sf::Vector2f GetLandingPoint(bool = true);
};
Rocket::Rocket()
{
velocity.x = velocity.y = 0.0f;
position.x = position.y = 0.0f;
thrustForce = rotationForce = rotation = 0.0f;
gravity = 10;
isActive = false;
}
void Rocket::Update(float deltaTime)
{
if(!isActive) return;
ApplyGravity(deltaTime);
ApplyVelocity(deltaTime);
}
void Rocket::Rotate(float deltaTime, bool isRight)
{
if(!isActive) return;
rotation += (isRight ? rotationForce : -rotationForce) * deltaTime;
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;
}
void Rocket::ApplyGravity(float deltaTime)
{
velocity.y -= gravity * deltaTime;
}
void Rocket::ApplyVelocity(float deltaTime)
{
position += velocity * deltaTime;
sprite.setPosition(GetPosition());
}
void Rocket::SetPosition(float x, float y, bool inverse)
{
position.x = x;
position.y = !inverse ? -y : y;
sprite.setPosition(position);
}
void Rocket::SetGravity(float gravity)
{
this -> gravity = gravity;
}
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::SetActive(bool active)
{
isActive = active;
}
float Rocket::GetSpeed()
{
return sqrt(velocity.x * velocity.x + velocity.y * velocity.y);
}
sf::Sprite Rocket::GetSprite()
{
return sprite;
}
sf::Vector2f Rocket::GetPosition(bool inverse)
{
sf::Vector2f result = position;
if(inverse) result.y *= -1;
return result;
}
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;
}

BIN
Landing/Rocket.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 429 B

114
Landing/main.cpp Normal file
View File

@ -0,0 +1,114 @@
#include <SFML/Graphics.hpp>
#include <sstream>
#include <cmath>
#include "Rocket.hpp"
#define WindowSize sf::VideoMode(960, 540)
#define WindowStyle sf::Style::Titlebar | sf::Style::Close
#define WindowTitle "Test Window Syntriax"
#define SizeMultiplier 5
#define WinThreshold 1.5
sf::Vertex CreateVertex(double, double);
float Terrain(float x, float divider)
{
return sin(x / divider);
}
int main()
{
std::ostringstream textToDisplay;
sf::RenderWindow window(WindowSize, WindowTitle, WindowStyle);
sf::Event event;
sf::Clock clock;
sf::Text text;
sf::Font font;
sf::Texture playerTexture;
Rocket player;
sf::Vertex terrain[320];
float timePassed;
float deltaTime;
bool isFocused = true;
for (int i = 0; i < 320; i++)
{
terrain[i].position.x = i * 3;
terrain[i].position.y = Terrain(i * 3, SizeMultiplier * 25) * SizeMultiplier * 25 + 400;
}
if (!playerTexture.loadFromFile("Rocket.png"))
return -1;
if (!font.loadFromFile("../OpenSans-Bold.ttf"))
return -1;
player.SetTexture(playerTexture, SizeMultiplier);
player.SetRotationForce(45);
player.SetThrustForce(25 * SizeMultiplier);
player.SetGravity(10 * SizeMultiplier);
player.SetPosition(480, -50);
player.SetActive(true);
text.setFont(font);
window.setVerticalSyncEnabled(true);
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::LostFocus)
isFocused = false;
else if (event.type == sf::Event::GainedFocus)
isFocused = true;
}
if(!isFocused)
{
timePassed = clock.getElapsedTime().asSeconds();
continue;
}
deltaTime = clock.getElapsedTime().asSeconds() - timePassed;
timePassed = clock.getElapsedTime().asSeconds();
textToDisplay.str("");
textToDisplay.clear();
//Collision
if(terrain[(int)player.GetLandingPoint().x / 3].position.y - player.GetLandingPoint().y < 0.0)
{
player.SetActive(false);
textToDisplay << (player.GetSpeed() / SizeMultiplier < WinThreshold ? "Win" : "Loose");
textToDisplay << "\nCollided";
textToDisplay << "\nSpeed: " << player.GetSpeed() / SizeMultiplier;
textToDisplay << "\nWin Speed: " << WinThreshold;
}
text.setString(textToDisplay.str());
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
player.Thrust(deltaTime);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
player.Rotate(deltaTime, true);
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
player.Rotate(deltaTime, false);
player.Update(deltaTime);
window.clear();
window.draw(text);
window.draw(&terrain[0], 320, sf::PrimitiveType::LinesStrip);
window.draw(player.GetSprite());
window.display();
}
return 0;
}
sf::Vertex CreateVertex(double x, double y)
{
return sf::Vertex(sf::Vector2f(x, y));
}

2
Landing/makefile Normal file
View File

@ -0,0 +1,2 @@
main.exe : *.cpp
g++ -o main.exe *.cpp ../SFML/*.a