Skip to content

Commit

Permalink
Renamed some variables & formatting improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
TechnicJelle committed Jan 31, 2024
1 parent de52486 commit c5df82a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
17 changes: 11 additions & 6 deletions src/Particle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,37 @@ void Particle::Update(const float fElapsedTime, std::vector<Particle>& sparkles)
velocity += fElapsedTime / 2 * acceleration;
position += velocity * fElapsedTime;
velocity += fElapsedTime / 2 * acceleration;
acceleration = { 0.0f, 0.0f };
acceleration = {0.0f, 0.0f};

fuse -= fElapsedTime;
if (fuse < 0.0f)
{
exploded = true;
if (rocket)
isExploded = true;
if (isRocket)
Explode(sparkles);
}
}

void Particle::Render(olc::PixelGameEngine& pge)
{
colour.a = static_cast<unsigned char>(255.0f * (fuse / initialFuse));
colour.a = static_cast<uint8_t>(255.0f * (fuse / initialFuse));
pge.Draw(position, colour);
}

bool Particle::IsExploded() const
{
return isExploded;
}

void Particle::Explode(std::vector<Particle>& sparkles) const
{
const olc::Pixel colours[] = { olc::RED, olc::GREEN, olc::BLUE, olc::YELLOW, olc::MAGENTA, olc::CYAN };
const olc::Pixel colours[] = {olc::RED, olc::GREEN, olc::BLUE, olc::YELLOW, olc::MAGENTA, olc::CYAN};
const olc::Pixel newColour = colours[random(6)];

for (int i = 0; i < random(10, 20); ++i)
{
Particle sparkle(false, position.x, position.y, random(0.8f, 1.2f), newColour);
olc::vf2d direction = { random(-1.0f, 1.0f), random(-1.0f, 1.0f) };
olc::vf2d direction = {random(-1.0f, 1.0f), random(-1.0f, 1.0f)};
direction = direction.norm();
sparkle.ApplyForce(direction * random(5000.0f, 8000.0f));

Expand Down
17 changes: 9 additions & 8 deletions src/Particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@

class Particle
{
public:
bool exploded;
private:
bool rocket;
bool isExploded = false;
bool isRocket = true;
olc::vf2d position;
olc::vf2d velocity;
olc::vf2d acceleration;
float mass;
float initialFuse;
float fuse;
float mass = 1.0f;
float initialFuse{};
float fuse{};
olc::Pixel colour;

public:
Particle() = default;

Particle(bool rocket, float x, float y, float fuse, olc::Pixel colour);
static Particle CreateRocket(const olc::PixelGameEngine& pge);

Particle(bool isRocket, float x, float y, float fuse, olc::Pixel colour);

void Update(float fElapsedTime, std::vector<Particle>& sparkles);

void Render(olc::PixelGameEngine& pge);

void ApplyForce(const olc::vf2d& force);

[[nodiscard]] bool IsExploded() const;

private:
void Explode(std::vector<Particle>& sparkles) const;
};

Expand Down
31 changes: 14 additions & 17 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "cppcoreguidelines-pro-type-member-init"

#include "olcPixelGameEngine.h"
#include "Particle.h"
#include "Utils.h"

constexpr bool vsync = false;
constexpr float physicsFPS = 200.0f;
Expand All @@ -17,11 +13,10 @@ class FireworksPGE final : public olc::PixelGameEngine
sAppName = "Fireworks";
}

std::vector<Particle> sparkles;

private:
std::array<Particle, 50> rockets;
const olc::vf2d gravity = { 0.0f, 10.0f };
std::vector<Particle> sparkles;
const olc::vf2d gravity = {0.0f, 10.0f};

public:
bool OnUserCreate() override
Expand All @@ -31,8 +26,6 @@ class FireworksPGE final : public olc::PixelGameEngine
i = Particle::CreateRocket(*this);
}

sparkles = std::vector<Particle>();

return true;
}

Expand Down Expand Up @@ -60,12 +53,12 @@ class FireworksPGE final : public olc::PixelGameEngine
while (accumulator >= dt)
{
accumulator -= dt;
for (auto& r : rockets)
for (Particle& r : rockets)
{
r.ApplyForce(gravity);
r.Update(dt, sparkles);

if (r.exploded)
if (r.IsExploded())
{
r = Particle::CreateRocket(*this);
}
Expand All @@ -77,8 +70,14 @@ class FireworksPGE final : public olc::PixelGameEngine
}

//remove all exploded particles from the sparkles vector
sparkles.erase(std::remove_if(sparkles.begin(), sparkles.end(),
[](const Particle& i) { return i.exploded; }), sparkles.end());
sparkles.erase(
std::remove_if(
sparkles.begin(),
sparkles.end(),
[](const Particle& i) { return i.IsExploded(); }
),
sparkles.end()
);
}

#pragma endregion // Physics
Expand All @@ -87,11 +86,11 @@ class FireworksPGE final : public olc::PixelGameEngine
#pragma region Rendering

Clear(olc::BLACK);
for (auto& r : rockets)
for (Particle& r : rockets)
{
r.Render(*this);
}
for (Particle s : sparkles)
for (Particle& s : sparkles)
{
s.Render(*this);
}
Expand All @@ -110,5 +109,3 @@ int main()

return 0;
}

#pragma clang diagnostic pop

0 comments on commit c5df82a

Please sign in to comment.