Skip to content

Commit

Permalink
Merge pull request #664 from Wargus/clean_up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
Jarod42 committed May 3, 2024
2 parents b4054ab + e46c8cd commit c027af4
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 149 deletions.
89 changes: 47 additions & 42 deletions src/include/particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,30 @@ struct CPosition {

class GraphicAnimation
{
CGraphic *g;
const CGraphic *g;
int ticksPerFrame;
int currentFrame = 0;
int currTicks = 0;
public:
GraphicAnimation(CGraphic *g, int ticksPerFrame);
~GraphicAnimation() {}
GraphicAnimation(const CGraphic &g, int ticksPerFrame);
~GraphicAnimation() = default;

/**
** Draw the current frame of the animation.
** @param x x screen coordinate where to draw the animation.
** @param y y screen coordinate where to draw the animation.
*/
void draw(int x, int y);
void draw(int x, int y) const;

/**
** Update the animation.
** @param ticks the number of ticks elapsed since the last call.
*/
void update(int ticks);

bool isFinished();
bool isVisible(const CViewport &vp, const CPosition &pos);
GraphicAnimation *clone();
bool isFinished() const;
bool isVisible(const CViewport &vp, const CPosition &pos) const;
GraphicAnimation *clone() const;
};


Expand All @@ -81,7 +81,7 @@ class CParticle
CParticle(CPosition position, int drawlevel = 0) : pos(position), drawLevel(drawlevel) {}
virtual ~CParticle() {}

virtual CParticle *clone() = 0;
virtual CParticle *clone() const = 0;
virtual void draw() = 0;
virtual bool isVisible(const CViewport &vp) const = 0;
virtual void update(int) = 0;
Expand All @@ -102,30 +102,35 @@ class CParticle
class StaticParticle : public CParticle
{
public:
StaticParticle(CPosition position, GraphicAnimation *flame, int drawlevel = 0);
~StaticParticle() override;
StaticParticle(CPosition position, const GraphicAnimation &flame, int drawlevel = 0);
~StaticParticle() override = default;

CParticle *clone() override;
CParticle *clone() const override;
void draw() override;
bool isVisible(const CViewport &vp) const override;
void update(int ticks) override;

protected:
GraphicAnimation *animation;
std::unique_ptr<GraphicAnimation> animation;
};


// Chunk particle
class CChunkParticle : public CParticle
{
public:
CChunkParticle(CPosition position, GraphicAnimation *smokeAnimation, GraphicAnimation *debrisAnimation,
GraphicAnimation *destroyAnimation,
int minVelocity = 0, int maxVelocity = 400,
int minTrajectoryAngle = 77, int maxTTL = 0, int drawlevel = 0);
~CChunkParticle() override;

CParticle *clone() override;
CChunkParticle(CPosition position,
const GraphicAnimation &smokeAnimation,
const GraphicAnimation &debrisAnimation,
const GraphicAnimation &destroyAnimation,
int minVelocity = 0,
int maxVelocity = 400,
int minTrajectoryAngle = 77,
int maxTTL = 0,
int drawlevel = 0);
~CChunkParticle() override = default;

CParticle *clone() const override;
void draw() override;
bool isVisible(const CViewport &vp) const override;
void update(int ticks) override;
Expand All @@ -137,21 +142,21 @@ class CChunkParticle : public CParticle

protected:
CPosition initialPos;
int initialVelocity;
float trajectoryAngle;
int maxTTL;
int nextSmokeTicks;
int lifetime;
int age;
int minVelocity;
int maxVelocity;
int minTrajectoryAngle;
float height;
int smokeDrawLevel;
int destroyDrawLevel;
GraphicAnimation *debrisAnimation;
GraphicAnimation *smokeAnimation;
GraphicAnimation *destroyAnimation;
int initialVelocity = 0;
float trajectoryAngle = 0;
int maxTTL = 0;
int nextSmokeTicks = 0;
int lifetime = 0;
int age = 0;
int minVelocity = 0;
int maxVelocity = 0;
int minTrajectoryAngle = 0;
float height = 0;
int smokeDrawLevel = 0;
int destroyDrawLevel = 0;
std::unique_ptr<GraphicAnimation> debrisAnimation;
std::unique_ptr<GraphicAnimation> smokeAnimation;
std::unique_ptr<GraphicAnimation> destroyAnimation;

struct {
float x;
Expand All @@ -164,16 +169,16 @@ class CChunkParticle : public CParticle
class CSmokeParticle : public CParticle
{
public:
CSmokeParticle(CPosition position, GraphicAnimation *animation, float speedx = 0, float speedy = -22.0f, int drawlevel = 0);
~CSmokeParticle() override;
CSmokeParticle(CPosition position, const GraphicAnimation &animation, float speedx = 0, float speedy = -22.0f, int drawlevel = 0);
~CSmokeParticle() override = default;

CParticle *clone() override;
CParticle *clone() const override;
void draw() override;
bool isVisible(const CViewport &vp) const override;
void update(int ticks) override;

protected:
GraphicAnimation *puff;
std::unique_ptr<GraphicAnimation> puff;
struct {
float x;
float y;
Expand All @@ -183,16 +188,16 @@ class CSmokeParticle : public CParticle
class CRadialParticle : public CParticle
{
public:
CRadialParticle(CPosition position, GraphicAnimation *animation, int maxSpeed, int drawlevel = 0);
~CRadialParticle() override;
CRadialParticle(CPosition position, const GraphicAnimation &animation, int maxSpeed, int drawlevel = 0);
~CRadialParticle() override = default;

CParticle *clone() override;
CParticle *clone() const override;
void draw() override;
bool isVisible(const CViewport &vp) const override;
void update(int ticks) override;

protected:
GraphicAnimation *animation;
std::unique_ptr<GraphicAnimation> animation;
float direction;
int speed;
int maxSpeed;
Expand Down
72 changes: 37 additions & 35 deletions src/particle/chunkparticle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,36 +43,34 @@ static inline float deg2rad(int degrees)
}


CChunkParticle::CChunkParticle(CPosition position, GraphicAnimation *smokeAnimation, GraphicAnimation *debrisAnimation,
GraphicAnimation *destroyAnimation,
int minVelocity, int maxVelocity, int minTrajectoryAngle, int maxTTL, int drawlevel) :
CParticle(position, drawlevel), initialPos(position), maxTTL(maxTTL), nextSmokeTicks(0),
age(0), height(0.f)
CChunkParticle::CChunkParticle(CPosition position,
const GraphicAnimation &smokeAnimation,
const GraphicAnimation &debrisAnimation,
const GraphicAnimation &destroyAnimation,
int minVelocity,
int maxVelocity,
int minTrajectoryAngle,
int maxTTL,
int drawlevel) :
CParticle(position, drawlevel),
initialPos(position),
initialVelocity(minVelocity + MyRand() % (maxVelocity - minVelocity + 1)),
trajectoryAngle(deg2rad(MyRand() % (90 - minTrajectoryAngle) + minTrajectoryAngle)),
maxTTL(maxTTL),
lifetime((int) (1000 * (initialVelocity * sin(trajectoryAngle) / gravity) * 2)),
minVelocity(minVelocity),
maxVelocity(maxVelocity),
minTrajectoryAngle(minTrajectoryAngle),
debrisAnimation(debrisAnimation.clone()),
smokeAnimation(smokeAnimation.clone()),
destroyAnimation(destroyAnimation.clone())
{
float radians = deg2rad(MyRand() % 360);
direction.x = cos(radians);
direction.y = sin(radians);

this->minVelocity = minVelocity;
this->maxVelocity = maxVelocity;
this->minTrajectoryAngle = minTrajectoryAngle;
this->initialVelocity = this->minVelocity + MyRand() % (this->maxVelocity - this->minVelocity + 1);
this->trajectoryAngle = deg2rad(MyRand() % (90 - this->minTrajectoryAngle) + this->minTrajectoryAngle);
this->lifetime = (int)(1000 * (initialVelocity * sin(trajectoryAngle) / gravity) * 2);
if (maxTTL) {
this->lifetime = std::min(maxTTL, this->lifetime);
}

this->smokeAnimation = smokeAnimation->clone();
this->debrisAnimation = debrisAnimation->clone();
this->destroyAnimation = destroyAnimation->clone();
}

CChunkParticle::~CChunkParticle()
{
delete debrisAnimation;
delete smokeAnimation;
delete destroyAnimation;
const float radians = deg2rad(MyRand() % 360);
direction.x = cos(radians);
direction.y = sin(radians);
}

static float calculateScreenPos(float posy, float height)
Expand Down Expand Up @@ -110,8 +108,7 @@ void CChunkParticle::update(int ticks) /* override */
if (age >= lifetime) {
if (destroyAnimation) {
CPosition p(pos.x, calculateScreenPos(pos.y, height));
GraphicAnimation *destroyanimation = destroyAnimation->clone();
auto destroy = std::make_unique<StaticParticle>(p, destroyanimation, destroyDrawLevel);
auto destroy = std::make_unique<StaticParticle>(p, *destroyAnimation, destroyDrawLevel);
ParticleManager.add(std::move(destroy));
}

Expand All @@ -124,18 +121,15 @@ void CChunkParticle::update(int ticks) /* override */

if (age > nextSmokeTicks) {
CPosition p(pos.x, calculateScreenPos(pos.y, height));
GraphicAnimation *smokeanimation = smokeAnimation->clone();
auto smoke = std::make_unique<CSmokeParticle>(p, smokeanimation, 0, -22.0f, smokeDrawLevel);
auto smoke = std::make_unique<CSmokeParticle>(p, *smokeAnimation, 0, -22.0f, smokeDrawLevel);
ParticleManager.add(std::move(smoke));

nextSmokeTicks += MyRand() % randSmokeTicks + minSmokeTicks;
}

debrisAnimation->update(ticks);
if (debrisAnimation->isFinished()) {
GraphicAnimation *debrisanimation = debrisAnimation->clone();
delete debrisAnimation;
debrisAnimation = debrisanimation;
debrisAnimation.reset(debrisAnimation->clone());
}

float time = age / 1000.f;
Expand All @@ -148,9 +142,17 @@ void CChunkParticle::update(int ticks) /* override */
}


CParticle *CChunkParticle::clone() /* override */
CParticle *CChunkParticle::clone() const /* override */
{
CChunkParticle *particle = new CChunkParticle(pos, smokeAnimation, debrisAnimation, destroyAnimation, minVelocity, maxVelocity, minTrajectoryAngle, maxTTL, drawLevel);
CChunkParticle *particle = new CChunkParticle(pos,
*smokeAnimation,
*debrisAnimation,
*destroyAnimation,
minVelocity,
maxVelocity,
minTrajectoryAngle,
maxTTL,
drawLevel);
particle->smokeDrawLevel = smokeDrawLevel;
particle->destroyDrawLevel = destroyDrawLevel;
return particle;
Expand Down
16 changes: 7 additions & 9 deletions src/particle/graphicanimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,12 @@
#include "ui.h"
#include "video.h"

GraphicAnimation::GraphicAnimation(CGraphic *g, int ticksPerFrame) :
g(g), ticksPerFrame(ticksPerFrame)
GraphicAnimation::GraphicAnimation(const CGraphic &g, int ticksPerFrame) :
g(&g), ticksPerFrame(ticksPerFrame)
{
Assert(g);
}


void GraphicAnimation::draw(int x, int y)
void GraphicAnimation::draw(int x, int y) const
{
if (!isFinished()) {
g->DrawFrameClip(currentFrame, x - g->Width / 2, y - g->Height / 2);
Expand All @@ -61,12 +59,12 @@ void GraphicAnimation::update(int ticks)
}
}

bool GraphicAnimation::isFinished()
bool GraphicAnimation::isFinished() const
{
return currentFrame >= g->NumFrames;
}

bool GraphicAnimation::isVisible(const CViewport &vp, const CPosition &pos)
bool GraphicAnimation::isVisible(const CViewport &vp, const CPosition &pos) const
{
// invisible graphics always invisible
if (!g) {
Expand Down Expand Up @@ -96,9 +94,9 @@ bool GraphicAnimation::isVisible(const CViewport &vp, const CPosition &pos)
return false;
}

GraphicAnimation *GraphicAnimation::clone()
GraphicAnimation *GraphicAnimation::clone() const
{
return new GraphicAnimation(g, ticksPerFrame);
return new GraphicAnimation(*g, ticksPerFrame);
}

//@}
25 changes: 8 additions & 17 deletions src/particle/radialparticle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,13 @@

#include <cmath>

CRadialParticle::CRadialParticle(CPosition position, GraphicAnimation *animation, int maxSpeed, int drawlevel) :
CParticle(position, drawlevel)
CRadialParticle::CRadialParticle(CPosition position, const GraphicAnimation &animation, int maxSpeed, int drawlevel) :
CParticle(position, drawlevel),
animation(animation.clone()),
direction(MyRand() % 360),
speed((MyRand() % maxSpeed) / 10),
maxSpeed(maxSpeed)
{
Assert(animation);
this->animation = animation->clone();

const int speedReduction = 10;

this->direction = (float)(MyRand() % 360);
this->speed = (MyRand() % maxSpeed) / speedReduction;
this->maxSpeed = maxSpeed;
}

CRadialParticle::~CRadialParticle()
{
delete animation;
}

bool CRadialParticle::isVisible(const CViewport &vp) const /* override */
Expand All @@ -74,9 +65,9 @@ void CRadialParticle::update(int ticks) /* override */
}
}

CParticle *CRadialParticle::clone() /* override */
CParticle *CRadialParticle::clone() const /* override */
{
CParticle *p = new CRadialParticle(pos, animation, maxSpeed, drawLevel);
CParticle *p = new CRadialParticle(pos, *animation, maxSpeed, drawLevel);
return p;
}

Expand Down
Loading

0 comments on commit c027af4

Please sign in to comment.