diff --git a/.vscode/settings.json b/.vscode/settings.json index acd47f4..2dac349 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,45 @@ "sstream": "cpp", "*.m": "cpp", "cmath": "cpp", - "type_traits": "cpp" + "type_traits": "cpp", + "algorithm": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "new": "cpp", + "ostream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "tuple": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "xfacet": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xmemory0": "cpp", + "xstddef": "cpp", + "xtr1common": "cpp", + "xutility": "cpp", + "iostream": "cpp" } } \ No newline at end of file diff --git a/Landing/Entity.hpp b/Landing/Entity.hpp new file mode 100644 index 0000000..afd7226 --- /dev/null +++ b/Landing/Entity.hpp @@ -0,0 +1,36 @@ +#include "SynGame.hpp" + +class Entity +{ + protected: + sf::Vector2f position; + public: + Entity(); + void SetPosition(float, float, bool = true); + void SetPosition(sf::Vector2f, bool = true); + sf::Vector2f GetPosition(bool = true); +}; + +Entity::Entity() +{ + position = sf::Vector2f(0.0, 0.0); +} + +void Entity::SetPosition(float x, float y, bool inverse) +{ + position.x = x; + position.y = !inverse ? -y : y; +} + +void Entity::SetPosition(sf::Vector2f position, bool inverse) +{ + this -> position = position; + this -> position.y = !inverse ? -this -> position.y : this -> position.y; +} + +sf::Vector2f Entity::GetPosition(bool inverse) +{ + sf::Vector2f result = position; + if(inverse) result.y *= -1; + return result; +} \ No newline at end of file diff --git a/Landing/Particles.hpp b/Landing/Particles.hpp new file mode 100644 index 0000000..cbe9028 --- /dev/null +++ b/Landing/Particles.hpp @@ -0,0 +1,154 @@ +#include +#include "SynGame.hpp" + +#pragma region Particle + class Particle : public PhysicEntity + { + private: + sf::Vertex vertex; + float lifeTime; + float remainingLifeTime; + void ChangeColorOverLifeTime(); + public: + Particle(); + Particle(sf::Color); + void Update(float); + void ResetRemainingLifeTime(); + void SetLifeTime(float); + bool IsLifeTimeOver(); + sf::Vertex &GetVertex(); + }; + + Particle::Particle() : PhysicEntity() + { + vertex.color = sf::Color::White; + lifeTime = remainingLifeTime = 0.0; + } + + Particle::Particle(sf::Color color) : PhysicEntity() + { + vertex.color = color; + lifeTime = remainingLifeTime = 0.0; + } + + void Particle::Update(float deltaTime) + { + PhysicEntity::Update(deltaTime); + + if(!isActive) + return; + + std::cout << position.x << ", " << position.y << "\n"; + + remainingLifeTime -= deltaTime; + ChangeColorOverLifeTime(); + } + + void Particle::ChangeColorOverLifeTime() + { + int value = 255 * (remainingLifeTime / lifeTime); + vertex.color.a = value; + } + + void Particle::ResetRemainingLifeTime() + { + remainingLifeTime = lifeTime; + } + + void Particle::SetLifeTime(float time) + { + remainingLifeTime = lifeTime = time; + } + + bool Particle::IsLifeTimeOver() + { + return remainingLifeTime <= 0.0; + } + + sf::Vertex &Particle::GetVertex() + { + vertex.position = position; + return vertex; + } +#pragma endregion +#pragma region Particle System + class ParticleSystem : public Entity + { + private: + std::vector particles; + sf::Color color; + int particleSize; + float particleLifeTime; + public: + ParticleSystem(); + void SetSize(int); + void SetParticleLifeTime(float); + void Update(float); + Particle &operator[](int); + int Size(); + }; + + ParticleSystem::ParticleSystem() + { + particles.clear(); + color = sf::Color::White; + particleSize = 0; + particleLifeTime = 0.1; + } + + void ParticleSystem::Update(float deltaTime) + { + int i; + Particle *particle; + + for (i = 0; i < particleSize; i++) + { + particle = &particles[i]; + if(particle -> IsLifeTimeOver()) + { + particle -> ResetRemainingLifeTime(); + particle -> SetPosition(position.x, position.y, false); + particle -> SetVelocity(sf::Vector2f(Random(-10.0, 10.0), Random(-10.0, 10.0))); + } + particle -> Update(deltaTime); + } + } + + void ParticleSystem::SetSize(int size) + { + while(particleSize != size) + if(particleSize > size) + { + delete &particles[--particleSize]; + particles.pop_back(); + } + else + { + Particle newParticle = Particle(color); + newParticle.SetLifeTime(particleLifeTime); + particles.push_back(newParticle); + particleSize++; + } + } + + void ParticleSystem::SetParticleLifeTime(float lifeTime) + { + if(lifeTime < 0.1) + return; + + int i; + particleLifeTime = lifeTime; + for (i = 0; i < particleSize; i++) + particles[i].SetLifeTime(lifeTime); + } + + Particle &ParticleSystem::operator[](int index) + { + return particles[index]; + } + + int ParticleSystem::Size() + { + return particles.size(); + } +#pragma endregion diff --git a/Landing/PhysicEntity.hpp b/Landing/PhysicEntity.hpp new file mode 100644 index 0000000..30ae945 --- /dev/null +++ b/Landing/PhysicEntity.hpp @@ -0,0 +1,64 @@ +#include "SynGame.hpp" + +class PhysicEntity : public Entity +{ + private: + void ApplyGravity(float); + void ApplyVelocity(float); + protected: + sf::Vector2f velocity; + float gravity; + bool isActive; + public: + PhysicEntity(); + void Update(float); + void SetVelocity(sf::Vector2f); + void SetGravity(float = 10); + void SetActive(bool = true); + float GetSpeed(); +}; + +PhysicEntity::PhysicEntity() : Entity() +{ + velocity = sf::Vector2f(0.0, 0.0); + gravity = 0.0; + isActive = true; +} + +void PhysicEntity::Update(float deltaTime) +{ + if(!isActive) return; + + ApplyGravity(deltaTime); + ApplyVelocity(deltaTime); +} + +void PhysicEntity::ApplyGravity(float deltaTime) +{ + velocity.y -= gravity * deltaTime; +} + +void PhysicEntity::ApplyVelocity(float deltaTime) +{ + position += velocity * deltaTime; +} + +void PhysicEntity::SetVelocity(sf::Vector2f velocity) +{ + this -> velocity = velocity; +} + +void PhysicEntity::SetGravity(float gravity) +{ + this -> gravity = gravity; +} + +void PhysicEntity::SetActive(bool active) +{ + isActive = active; +} + +float PhysicEntity::GetSpeed() +{ + return sqrt(velocity.x * velocity.x + velocity.y * velocity.y); +} \ No newline at end of file diff --git a/Landing/Randomizer.hpp b/Landing/Randomizer.hpp new file mode 100644 index 0000000..4444a29 --- /dev/null +++ b/Landing/Randomizer.hpp @@ -0,0 +1,14 @@ +#include +#include + +template +T Random(T min, T max) +{ + T result; + long int value; + static unsigned long int counter = time(0); + srand(time(0) * counter++); + value = ((rand() * counter) % (int)((max - min) * 100000000.0)); + result = value / 100000000.0 + min; + return result; +} \ No newline at end of file diff --git a/Landing/Rocket.hpp b/Landing/Rocket.hpp index 0bdf85c..0b7a57e 100644 --- a/Landing/Rocket.hpp +++ b/Landing/Rocket.hpp @@ -1,55 +1,38 @@ -#include -#include -#include +// #include +// #include +#include "SynGame.hpp" +// #include "Particles.hpp" +// #include "PhysicEntity.hpp" #define DegToRad 0.0174533 +#define RotationLimit 45.0 -class Rocket +class Rocket : public PhysicEntity { 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: + ParticleSystem particleSystem; Rocket(); - void Update(float); void Rotate(float, bool = true); void Thrust(float); + void Update(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); + void SetPosition(float, float, bool = true); }; -Rocket::Rocket() +Rocket::Rocket() : PhysicEntity() { - 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); + SetGravity(); } void Rocket::Rotate(float deltaTime, bool isRight) @@ -57,6 +40,12 @@ 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; + sprite.setRotation(rotation); } @@ -67,31 +56,18 @@ void Rocket::Thrust(float deltaTime) float force = thrustForce * deltaTime; velocity.x += sin(rotation * DegToRad) * force; velocity.y += cos(rotation * DegToRad) * force; + + particleSystem.SetPosition(position); + particleSystem.Update(deltaTime); } -void Rocket::ApplyGravity(float deltaTime) +void Rocket::Update(float deltaTime) { - velocity.y -= gravity * deltaTime; -} - -void Rocket::ApplyVelocity(float deltaTime) -{ - position += velocity * deltaTime; + PhysicEntity::Update(deltaTime); + particleSystem.Update(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; @@ -110,14 +86,10 @@ void Rocket::SetTexture(sf::Texture texture, float size) sprite.setOrigin(texture.getSize().x / 2, texture.getSize().y / 2); } -void Rocket::SetActive(bool active) +void Rocket::SetPosition(float x, float y, bool inverse) { - isActive = active; -} - -float Rocket::GetSpeed() -{ - return sqrt(velocity.x * velocity.x + velocity.y * velocity.y); + Entity::SetPosition(x, y, inverse); + sprite.setPosition(position); } sf::Sprite Rocket::GetSprite() @@ -125,13 +97,6 @@ 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; diff --git a/Landing/Rocket.png b/Landing/Rocket.png index a10516d..270b499 100644 Binary files a/Landing/Rocket.png and b/Landing/Rocket.png differ diff --git a/Landing/SynGame.hpp b/Landing/SynGame.hpp new file mode 100644 index 0000000..bf8f8c1 --- /dev/null +++ b/Landing/SynGame.hpp @@ -0,0 +1,11 @@ +#ifndef SynClasses + #define SynClasses + #include + #include + #include + #include "Randomizer.hpp" + #include "Entity.hpp" + #include "PhysicEntity.hpp" + #include "Particles.hpp" + #include "Rocket.hpp" +#endif diff --git a/Landing/main.cpp b/Landing/main.cpp index 5c498a5..6ea4688 100644 --- a/Landing/main.cpp +++ b/Landing/main.cpp @@ -1,7 +1,7 @@ #include #include #include -#include "Rocket.hpp" +#include "SynGame.hpp" #define WindowSize sf::VideoMode(960, 540) #define WindowStyle sf::Style::Titlebar | sf::Style::Close @@ -49,6 +49,8 @@ int main() player.SetGravity(10 * SizeMultiplier); player.SetPosition(480, -50); player.SetActive(true); + player.particleSystem.SetParticleLifeTime(1.5); + player.particleSystem.SetSize(25); text.setFont(font); window.setVerticalSyncEnabled(true); @@ -101,6 +103,8 @@ int main() window.clear(); window.draw(text); window.draw(&terrain[0], 320, sf::PrimitiveType::LinesStrip); + for (int i = 0; i < player.particleSystem.Size(); i++) + window.draw(&player.particleSystem[i].GetVertex(), 1, sf::PrimitiveType::Points); window.draw(player.GetSprite()); window.display(); }