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

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

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