-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrick.cpp
More file actions
124 lines (113 loc) · 4 KB
/
Copy pathBrick.cpp
File metadata and controls
124 lines (113 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: https://pvs-studio.com
#include "Brick.h"
#include "Game.h"
#include "Ball.h"
#include "Bonus.h"
#include <cstdlib>
Brick::Brick(float x, float y, float width, float height, sf::Color color)
: isVisible(true), originalColor(color) {
shape.setPosition(sf::Vector2f(x, y));
shape.setSize(sf::Vector2f(width, height));
shape.setFillColor(color);
shape.setOutlineThickness(1.f);
shape.setOutlineColor(sf::Color(
static_cast<uint8_t>(color.r * 0.7f),
static_cast<uint8_t>(color.g * 0.7f),
static_cast<uint8_t>(color.b * 0.7f)
));
}
void Brick::draw(sf::RenderWindow& window) const {
if (isVisible) {
window.draw(shape);
}
}
sf::FloatRect Brick::getBounds() const {
return shape.getGlobalBounds();
}
void Brick::updateColorBasedOnHealth(int currentHealth, int maxHealth) {
if (maxHealth <= 0 || currentHealth <= 0) return;
float factor = static_cast<float>(currentHealth) / static_cast<float>(maxHealth);
shape.setFillColor(sf::Color(
static_cast<uint8_t>(originalColor.r * factor),
static_cast<uint8_t>(originalColor.g * factor),
static_cast<uint8_t>(originalColor.b * factor)
));
}
NormalBrick::NormalBrick(float x, float y, sf::Color color, int initialHealth)
: Brick(x, y, BRICK_WIDTH, BRICK_HEIGHT, color), health(initialHealth), maxHealth(initialHealth) {
if (health <= 0) health = 1;
}
bool NormalBrick::onHit(Game& game, Ball& ballHit) {
if (!isVisible) return false;
health--;
game.addScore(SCORE_BRICK_HIT_NORMAL);
if (health <= 0) {
isVisible = false;
game.addScore(SCORE_BRICK_DESTROYED_NORMAL);
return true;
}
else {
if (maxHealth > 1) {
updateColorBasedOnHealth(health, maxHealth);
}
return false;
}
}
IndestructibleBrick::IndestructibleBrick(float x, float y, sf::Color color)
: Brick(x, y, BRICK_WIDTH, BRICK_HEIGHT, color) {}
bool IndestructibleBrick::onHit(Game& game, Ball& ballHit) {
if (!isVisible) return false;
return false;
}
BonusDropperBrick::BonusDropperBrick(float x, float y, sf::Color color, int initialHealth)
: Brick(x, y, BRICK_WIDTH, BRICK_HEIGHT, color), health(initialHealth) {
if (health <= 0) health = 1;
}
bool BonusDropperBrick::onHit(Game& game, Ball& ballHit) {
if (!isVisible) return false;
health--;
game.addScore(SCORE_BRICK_HIT_NORMAL);
if (health <= 0) {
isVisible = false;
game.addScore(SCORE_BRICK_DESTROYED_BONUS);
return true;
}
else {
if (health < 2) {
updateColorBasedOnHealth(health, 2);
}
return false;
}
}
std::unique_ptr<Bonus> BonusDropperBrick::createBonus(float x_pos, float y_pos) {
int randomBonusType = std::rand() % 7;
switch (randomBonusType) {
case 0: return std::make_unique<WidenPaddleBonus>(x_pos, y_pos);
case 1: return std::make_unique<NarrowPaddleBonus>(x_pos, y_pos);
case 2: return std::make_unique<FastBallBonus>(x_pos, y_pos);
case 3: return std::make_unique<SlowBallBonus>(x_pos, y_pos);
case 4: return std::make_unique<StickyPaddleBonus>(x_pos, y_pos);
case 5: return std::make_unique<SafetyNetBonus>(x_pos, y_pos);
case 6: return std::make_unique<MultiBallBonus>(x_pos, y_pos);
default: return std::make_unique<WidenPaddleBonus>(x_pos, y_pos);
}
}
SpeedUpBrick::SpeedUpBrick(float x, float y, sf::Color color, int initialHealth)
: Brick(x, y, BRICK_WIDTH, BRICK_HEIGHT, color), health(initialHealth) {
if (health <= 0) health = 1;
}
bool SpeedUpBrick::onHit(Game& game, Ball& ballHit) {
if (!isVisible) return false;
health--;
game.addScore(SCORE_BRICK_HIT_NORMAL);
if (health <= 0) {
isVisible = false;
ballHit.applySpeedChange(BALL_SPEED_CHANGE_FACTOR);
game.addScore(SCORE_BRICK_DESTROYED_SPEED);
return true;
}
else {
return false;
}
}