-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
207 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// | ||
// Created by Matq on 10/12/2024. | ||
// | ||
|
||
#include "Particles.h" | ||
|
||
#include <utility> | ||
#include "../technical stuff/TextManager.h" | ||
#include "../game/GameWorld.h" | ||
|
||
void Particle::Tick() { | ||
m_Vel *= m_VelDamping; | ||
m_OrientationVel *= m_OrientationVelDamping; | ||
m_Position += m_Vel; | ||
m_Orientation += m_OrientationVel; | ||
|
||
|
||
if (m_Lifetime == 0 || m_Vel.Length() <= 0.001) | ||
m_Active = false; | ||
|
||
m_Lifetime--; | ||
} | ||
|
||
void Particle::Draw(Drawing* drawing) const { | ||
SDL_FRect asd = { | ||
(float)m_Position.x, | ||
(float)m_Position.y, | ||
(float)m_Size.x, | ||
(float)m_Size.y, | ||
}; | ||
|
||
drawing->RenderTextureExFCamera(m_Texture->SDLTexture(), nullptr, asd, m_Orientation, nullptr, SDL_FLIP_NONE); | ||
} | ||
|
||
Particles::Particles(GameWorld* game_world) { | ||
m_World = game_world; | ||
m_Drawing = game_world->GameWindow()->Render(); | ||
m_CreateParticleIndex = 0; | ||
|
||
for (auto & particle : m_Particles) | ||
particle = Particle(); | ||
} | ||
|
||
bool Particles::PlayParticle(Particle particle) { | ||
bool success = false; | ||
for (int i = 0; i < 1024; i++) { | ||
// Occupy space if available | ||
if (!m_Particles[m_CreateParticleIndex].m_Active) { | ||
m_Particles[m_CreateParticleIndex] = std::move(particle); | ||
success = true; | ||
} | ||
|
||
// Go to the next index | ||
m_CreateParticleIndex++; | ||
if (m_CreateParticleIndex >= 1024) | ||
m_CreateParticleIndex = 0; | ||
|
||
// Leave if found a space successfully | ||
if (success) return true; | ||
} | ||
|
||
// There was no space for the particle | ||
return false; | ||
} | ||
|
||
void Particles::Tick() { | ||
for (auto& particle : m_Particles) { | ||
if (!particle.m_Active) | ||
continue; | ||
|
||
if (particle.m_Position.x < 0) { | ||
particle.m_Position.x = 0; | ||
particle.m_Vel.x *= -1; | ||
} | ||
if (particle.m_Position.x > m_World->GetWidth()) { | ||
particle.m_Position.x = m_World->GetWidth(); | ||
particle.m_Vel.x *= -1; | ||
} | ||
if (particle.m_Position.y < 0) { | ||
particle.m_Position.y = 0; | ||
particle.m_Vel.y *= -1; | ||
} | ||
if (particle.m_Position.y > m_World->GetHeight()) { | ||
particle.m_Position.y = m_World->GetHeight(); | ||
particle.m_Vel.y *= -1; | ||
} | ||
|
||
particle.Tick(); | ||
} | ||
} | ||
|
||
void Particles::Draw() { | ||
for (auto& particle : m_Particles) { | ||
if (!particle.m_Active) | ||
continue; | ||
|
||
particle.Draw(m_Drawing); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// Created by Matq on 10/12/2024. | ||
// | ||
|
||
#pragma once | ||
|
||
#include "../technical stuff/Drawing.h" | ||
#include "../technical stuff/Vec2.h" | ||
|
||
struct Particle { | ||
Texture* m_Texture; | ||
Vec2d m_Position; | ||
Vec2d m_Size; | ||
Vec2d m_Vel; | ||
double m_VelDamping; | ||
double m_Orientation; | ||
double m_OrientationVel; | ||
double m_OrientationVelDamping; | ||
unsigned long long m_Lifetime; | ||
bool m_Active; | ||
|
||
Particle() { | ||
m_Texture = nullptr; | ||
m_Position = {}; | ||
m_Size = {}; | ||
m_Vel = {}; | ||
m_Orientation = {}; | ||
m_OrientationVel = {}; | ||
m_Active = false; | ||
} | ||
Particle(Texture* texture, const Vec2d& pos, const Vec2d& size, const Vec2d& vel, double vel_damping, double orientation, double orientation_vel, double orientation_vel_damping, unsigned long long lifetime) { | ||
m_Texture = texture; | ||
m_Position = pos; | ||
m_Size = size; | ||
m_Vel = vel; | ||
m_VelDamping = vel_damping; | ||
m_Orientation = orientation; | ||
m_OrientationVel = orientation_vel; | ||
m_OrientationVelDamping = orientation_vel_damping; | ||
m_Lifetime = lifetime; | ||
|
||
m_Active = true; | ||
} | ||
|
||
void Tick(); | ||
void Draw(Drawing* drawing) const; | ||
|
||
}; | ||
|
||
class GameWorld; | ||
class Particles { | ||
private: | ||
GameWorld* m_World; | ||
Drawing* m_Drawing; | ||
Particle m_Particles[1024]; | ||
size_t m_CreateParticleIndex; | ||
|
||
public: | ||
explicit Particles(GameWorld* game_world); | ||
|
||
// Manipulating | ||
bool PlayParticle(Particle particle); | ||
|
||
// Ticking | ||
void Tick(); | ||
void Draw(); | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters