seminar13 - arkanoid, refactored some code

master
nihonium 2 years ago
parent 7697130f21
commit 2e5c5a8dde
No known key found for this signature in database
GPG Key ID: 0251623741027CFC

@ -9,7 +9,7 @@ class Effect;
class Arkanoid class Arkanoid
{ {
protected: private:
// Константы: // Константы:
// Цвет задника // Цвет задника
const sf::Color kBackgroundColor {12, 31, 47}; const sf::Color kBackgroundColor {12, 31, 47};
@ -18,8 +18,6 @@ protected:
// Максимально возможное количество бонусов в один момент времени // Максимально возможное количество бонусов в один момент времени
const unsigned kMaxNumBonuses {10}; const unsigned kMaxNumBonuses {10};
// Поля:
// Время, которое прошло с начала игры в секундах // Время, которое прошло с начала игры в секундах
double m_time; double m_time;
// Границы игрового поля // Границы игрового поля

@ -6,6 +6,11 @@
#include "ball.hpp" #include "ball.hpp"
#include "paddle.hpp" #include "paddle.hpp"
/* =============
* ===Bonuses===
* =============
* */
Bonus::Bonus(sf::Vector2f position): m_position(position) Bonus::Bonus(sf::Vector2f position): m_position(position)
{ {
m_time = 0; m_time = 0;
@ -55,9 +60,10 @@ void TripleBallBonus::activate(Arkanoid& game)
bool isSlowed = false; bool isSlowed = false;
bool toApply = true; bool toApply = true;
Effect* slowing_effect = nullptr; Effect* slowing_effect = nullptr;
for (auto it = game.m_effects.begin(); it != game.m_effects.end();) { for (auto it = game.m_effects.begin(); it != game.m_effects.end();) {
if ((*it)->effectId == 0) { if ((*it)->effectId == _EFFECT_SLOWING_ID) {
isSlowed = true; isSlowed = true;
slowing_effect = *it; slowing_effect = *it;
break; break;
@ -100,8 +106,6 @@ void TripleBallBonus::activate(Arkanoid& game)
} }
} }
TripleBallBonus::~TripleBallBonus() {}
/* /*
* EnlargePaddleBonus * EnlargePaddleBonus
* */ * */
@ -126,8 +130,6 @@ void EnlargePaddleBonus::activate(Arkanoid& game)
} }
} }
EnlargePaddleBonus::~EnlargePaddleBonus() {}
/* /*
* ShrinkPaddleBonus * ShrinkPaddleBonus
* */ * */
@ -151,8 +153,6 @@ void ShrinkPaddleBonus::activate(Arkanoid& game)
} }
} }
ShrinkPaddleBonus::~ShrinkPaddleBonus() {}
/* /*
* SlowingBonus * SlowingBonus
* */ * */
@ -165,16 +165,18 @@ void SlowingBonus::draw(sf::RenderWindow& window) const
shape.setPosition(m_position); shape.setPosition(m_position);
window.draw(shape); window.draw(shape);
/*static sf::RectangleShape rect(sf::Vector2f{radius, radius / 2}); static sf::CircleShape clock(radius / 2);
rect.setFillColor(sf::Color::Red); clock.setOutlineColor(sf::Color::Red);
rect.setPosition(m_position - sf::Vector2f{radius /2, radius / 4}); clock.setOutlineThickness(3);
window.draw(rect);*/ clock.setPosition(m_position - sf::Vector2f{radius /2, radius / 2});
window.draw(clock);
} }
void SlowingBonus::activate(Arkanoid& game) void SlowingBonus::activate(Arkanoid& game)
{ {
bool isAlreadySlowed = false; bool isAlreadySlowed = false;
for (auto it = game.m_effects.begin(); it != game.m_effects.end();) { for (auto it = game.m_effects.begin(); it != game.m_effects.end();) {
if ((*it)->effectId == 0) { if ((*it)->effectId == _EFFECT_SLOWING_ID) {
(*it)->mDuration += mDuration; (*it)->mDuration += mDuration;
isAlreadySlowed = true; isAlreadySlowed = true;
break; break;
@ -187,10 +189,9 @@ void SlowingBonus::activate(Arkanoid& game)
} }
} }
SlowingBonus::~SlowingBonus() {} /* =============
* ===Effects===
/* * =============
* Effects
* */ * */
Effect::Effect(char id, double start_time, double duration) : effectId(id), mStartTime(start_time), mDuration(duration) {}; Effect::Effect(char id, double start_time, double duration) : effectId(id), mStartTime(start_time), mDuration(duration) {};
@ -201,30 +202,20 @@ bool Effect::isExpired(double time) {
return true; return true;
} }
SlowingEffect::SlowingEffect(double start_time, double duration) : Effect(0, start_time, duration) {}; SlowingEffect::SlowingEffect(double start_time, double duration) : Effect(_EFFECT_SLOWING_ID, start_time, duration) {};
void SlowingEffect::activate(Arkanoid& game) { void SlowingEffect::activate(Arkanoid& game) {
for (Ball& ball : game.m_balls) for (Ball& ball : game.m_balls) {
{
//if (!(ball.affectedBy & 0b00000001)) {
//ball.affectedBy |= 0b00000001;
ball.velocity = sf::Vector2f{ball.velocity.x * mSlowingFactor, ball.velocity.y * mSlowingFactor}; ball.velocity = sf::Vector2f{ball.velocity.x * mSlowingFactor, ball.velocity.y * mSlowingFactor};
//}
} }
} }
void SlowingEffect::activate(Ball& ball) { void SlowingEffect::activate(Ball& ball) {
//if (!(ball.affectedBy & 0b00000001)) { ball.velocity = sf::Vector2f{ball.velocity.x * mSlowingFactor, ball.velocity.y * mSlowingFactor};
//ball.affectedBy |= 0b00000001;
ball.velocity = sf::Vector2f{ball.velocity.x * mSlowingFactor, ball.velocity.y * mSlowingFactor};
} }
void SlowingEffect::deactivate(Arkanoid& game) { void SlowingEffect::deactivate(Arkanoid& game) {
for (Ball& ball : game.m_balls) for (Ball& ball : game.m_balls) {
{ ball.velocity = sf::Vector2f{ball.velocity.x / mSlowingFactor, ball.velocity.y / mSlowingFactor};
//if (ball.affectedBy & 0b00000001) {
ball.velocity = sf::Vector2f{ball.velocity.x / mSlowingFactor, ball.velocity.y / mSlowingFactor};
// ball.affectedBy &= 0b11111110;
//}
} }
} }

@ -17,9 +17,9 @@ protected:
public: public:
Bonus(sf::Vector2f position); Bonus(sf::Vector2f position);
void update(float dt); void update(float dt);
virtual void draw(sf::RenderWindow& window) const {}; virtual void draw(sf::RenderWindow& window) const = 0;
virtual void activate(Arkanoid& game){}; virtual void activate(Arkanoid& game) = 0;
virtual ~Bonus(){}; virtual ~Bonus() = default;
bool isColiding(const Paddle& paddle) const; bool isColiding(const Paddle& paddle) const;
// Класс Arkanoid должен быть дружественным, так как он может менять внутреннее объекта-бонуса // Класс Arkanoid должен быть дружественным, так как он может менять внутреннее объекта-бонуса
@ -31,7 +31,6 @@ public:
TripleBallBonus(sf::Vector2f position): Bonus(position) {}; TripleBallBonus(sf::Vector2f position): Bonus(position) {};
void draw(sf::RenderWindow& window) const; void draw(sf::RenderWindow& window) const;
void activate(Arkanoid& game); void activate(Arkanoid& game);
~TripleBallBonus();
}; };
class EnlargePaddleBonus : public Bonus { class EnlargePaddleBonus : public Bonus {
@ -39,7 +38,6 @@ public:
EnlargePaddleBonus(sf::Vector2f position): Bonus(position) {}; EnlargePaddleBonus(sf::Vector2f position): Bonus(position) {};
void draw(sf::RenderWindow& window) const; void draw(sf::RenderWindow& window) const;
void activate(Arkanoid& game); void activate(Arkanoid& game);
~EnlargePaddleBonus();
}; };
class ShrinkPaddleBonus : public Bonus { class ShrinkPaddleBonus : public Bonus {
@ -47,7 +45,6 @@ public:
ShrinkPaddleBonus(sf::Vector2f position): Bonus(position) {}; ShrinkPaddleBonus(sf::Vector2f position): Bonus(position) {};
void draw(sf::RenderWindow& window) const; void draw(sf::RenderWindow& window) const;
void activate(Arkanoid& game); void activate(Arkanoid& game);
~ShrinkPaddleBonus();
}; };
class SlowingBonus : public Bonus { class SlowingBonus : public Bonus {
@ -57,25 +54,27 @@ public:
SlowingBonus(sf::Vector2f position): Bonus(position) {}; SlowingBonus(sf::Vector2f position): Bonus(position) {};
void draw(sf::RenderWindow& window) const; void draw(sf::RenderWindow& window) const;
void activate(Arkanoid& game); void activate(Arkanoid& game);
~SlowingBonus();
}; };
/* /*
* Effects * Effects
* */ * */
#define _EFFECT_SLOWING_ID 0
class Effect { class Effect {
private: protected:
char effectId; char effectId;
double mStartTime; double mStartTime;
double mDuration; double mDuration;
public: public:
Effect(char id, double start_time, double duration); Effect(char id, double start_time, double duration);
virtual void activate(Arkanoid& game) {}; virtual ~Effect() = default;
virtual void activate(Ball& ball) {};
virtual void deactivate(Arkanoid& game) {}; virtual void activate(Arkanoid& game) = 0;
virtual void activate(Ball& ball) = 0;
virtual void deactivate(Arkanoid& game) = 0;
bool isExpired(double time); bool isExpired(double time);
friend class SlowingBonus; friend class SlowingBonus;
friend class TripleBallBonus; friend class TripleBallBonus;
friend class Arkanoid; friend class Arkanoid;
@ -83,8 +82,8 @@ public:
class SlowingEffect : public Effect { class SlowingEffect : public Effect {
private: private:
double mSlowingFactor = 0.1; float mSlowingFactor = 0.1;
char id = 0; char id = _EFFECT_SLOWING_ID;
public: public:
SlowingEffect(double start_time, double duration); SlowingEffect(double start_time, double duration);
void activate(Arkanoid& game); void activate(Arkanoid& game);