Złączono i wszystko działa, ale trzeba zrobić mocny refactor Planszy bo za dużo niewiadomo skąd biorących się pętli
This commit is contained in:
@@ -26,16 +26,11 @@ public:
|
|||||||
virtual void moveDown() = 0;
|
virtual void moveDown() = 0;
|
||||||
virtual void shoot() = 0;
|
virtual void shoot() = 0;
|
||||||
|
|
||||||
std::vector<Bullet>& getBullets();
|
|
||||||
sf::Sprite& getSprite();
|
|
||||||
Position getPosition();
|
|
||||||
void updateBullets();
|
void updateBullets();
|
||||||
void setMovingSpeed(float speed);
|
void setMovingSpeed(float speed);
|
||||||
void dealDamage();
|
void dealDamage();
|
||||||
void healUP();
|
void healUP();
|
||||||
|
|
||||||
void updateBullets();
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Position position;
|
Position position;
|
||||||
sf::Sprite actorSprite;
|
sf::Sprite actorSprite;
|
||||||
@@ -44,7 +39,6 @@ protected:
|
|||||||
sf::Texture rocketTexture;
|
sf::Texture rocketTexture;
|
||||||
std::vector<Bullet> bullets;
|
std::vector<Bullet> bullets;
|
||||||
sf::Clock damageDealClock;
|
sf::Clock damageDealClock;
|
||||||
Position position;
|
|
||||||
int hp;
|
int hp;
|
||||||
unsigned int damage;
|
unsigned int damage;
|
||||||
unsigned int firerate;
|
unsigned int firerate;
|
||||||
|
|||||||
@@ -39,30 +39,14 @@ public:
|
|||||||
delete ship; // usuwanie wskaźnika ship
|
delete ship; // usuwanie wskaźnika ship
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
sf::RenderWindow *window;
|
|
||||||
Background background;
|
Background background;
|
||||||
AudioManager audioManager;
|
AudioManager audioManager;
|
||||||
Player *ship;
|
Player *ship;
|
||||||
|
Size size;
|
||||||
sf::RenderWindow *window;
|
sf::RenderWindow *window;
|
||||||
sf::Font font;
|
sf::Font font;
|
||||||
Size size;
|
|
||||||
sf::Texture meteorTexture1;
|
|
||||||
sf::Texture meteorTexture2;
|
|
||||||
sf::Texture heartTexture;
|
|
||||||
sf::Texture heartTextureGray;
|
|
||||||
sf::Clock meteorSpawnClock;
|
sf::Clock meteorSpawnClock;
|
||||||
sf::Clock heartSpawnClock;
|
sf::Clock heartSpawnClock;
|
||||||
sf::Texture enemyBulletTexture;
|
|
||||||
sf::Texture WiazkaTexture;
|
|
||||||
sf::Texture BombaTexture;
|
|
||||||
sf::Texture playerTexture;
|
|
||||||
sf::Texture playerBulletTexture;
|
|
||||||
sf::Texture playerRocketTexture;
|
|
||||||
sf::Texture enemyTexture;
|
|
||||||
sf::Texture advancedEnemyTexture;
|
|
||||||
sf::Texture BomberEnemyTexture;
|
|
||||||
sf::Texture KamikadzeTexture;
|
|
||||||
sf::Texture WiazkowiecTexture;
|
|
||||||
sf::Clock spawnClock;
|
sf::Clock spawnClock;
|
||||||
sf::Clock scoreClock;
|
sf::Clock scoreClock;
|
||||||
sf::Clock shooterSpawnClock;
|
sf::Clock shooterSpawnClock;
|
||||||
@@ -71,6 +55,21 @@ private:
|
|||||||
sf::Clock BomberSpawnClock;
|
sf::Clock BomberSpawnClock;
|
||||||
sf::Clock KamikadzeSpawnClock;
|
sf::Clock KamikadzeSpawnClock;
|
||||||
sf::Clock WiazkowiecSpawnClock;
|
sf::Clock WiazkowiecSpawnClock;
|
||||||
|
sf::Texture playerTexture;
|
||||||
|
sf::Texture playerBulletTexture;
|
||||||
|
sf::Texture playerRocketTexture;
|
||||||
|
sf::Texture enemyTexture;
|
||||||
|
sf::Texture enemyBulletTexture;
|
||||||
|
sf::Texture advancedEnemyTexture;
|
||||||
|
sf::Texture BomberEnemyTexture;
|
||||||
|
sf::Texture BombaTexture;
|
||||||
|
sf::Texture KamikadzeTexture;
|
||||||
|
sf::Texture WiazkowiecTexture;
|
||||||
|
sf::Texture WiazkaTexture;
|
||||||
|
sf::Texture meteorTexture1;
|
||||||
|
sf::Texture meteorTexture2;
|
||||||
|
sf::Texture heartTexture;
|
||||||
|
sf::Texture heartTextureGray;
|
||||||
std::vector<Enemy> enemies;
|
std::vector<Enemy> enemies;
|
||||||
std::vector<AdvancedEnemy> AEnemies;
|
std::vector<AdvancedEnemy> AEnemies;
|
||||||
std::vector<Bomber> BEnemies;
|
std::vector<Bomber> BEnemies;
|
||||||
@@ -81,7 +80,6 @@ private:
|
|||||||
std::vector<sf::Sprite> heartStats;
|
std::vector<sf::Sprite> heartStats;
|
||||||
bool gameOver = false;
|
bool gameOver = false;
|
||||||
unsigned int score = 0;
|
unsigned int score = 0;
|
||||||
bool isPlayerSpawned = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //PLANSZA_H
|
#endif //PLANSZA_H
|
||||||
|
|||||||
@@ -29,9 +29,7 @@ public:
|
|||||||
void moveUp() override;
|
void moveUp() override;
|
||||||
void moveDown() override;
|
void moveDown() override;
|
||||||
void takeDamage();
|
void takeDamage();
|
||||||
bool isAlive() const;
|
|
||||||
void update();
|
|
||||||
void onHit();
|
|
||||||
void update();
|
void update();
|
||||||
std::vector<Rocket>& getRockets();
|
std::vector<Rocket>& getRockets();
|
||||||
|
|
||||||
@@ -42,11 +40,8 @@ private:
|
|||||||
std::vector<Rocket> rockets;
|
std::vector<Rocket> rockets;
|
||||||
sf::Texture rocketTexture;
|
sf::Texture rocketTexture;
|
||||||
sf::Texture bulletTexture;
|
sf::Texture bulletTexture;
|
||||||
bool isBlinking = false;
|
|
||||||
sf::Color originalColor;
|
sf::Color originalColor;
|
||||||
|
|
||||||
sf::Clock immortalityClock; // Zegar kontrolujący czas nieśmiertelności
|
sf::Clock immortalityClock; // Zegar kontrolujący czas nieśmiertelności
|
||||||
int health = 3; // Liczba punktów życia gracza
|
|
||||||
float immortalityDuration = 1.5f; // Czas trwania nieśmiertelności w sec
|
float immortalityDuration = 1.5f; // Czas trwania nieśmiertelności w sec
|
||||||
bool isImmortal = false; // flaga na immortal
|
bool isImmortal = false; // flaga na immortal
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::Render
|
|||||||
spawnClock.restart();
|
spawnClock.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Refactor tej metody bo rozrosła się za bardzo już
|
||||||
void Plansza::update() {
|
void Plansza::update() {
|
||||||
// tło
|
// tło
|
||||||
background.update();
|
background.update();
|
||||||
@@ -136,18 +136,18 @@ void Plansza::update() {
|
|||||||
update_hearts();
|
update_hearts();
|
||||||
ship->updateBullets();
|
ship->updateBullets();
|
||||||
|
|
||||||
ship.update();
|
ship->update();
|
||||||
window->draw(ship->getSprite());
|
window->draw(ship->getSprite());
|
||||||
|
|
||||||
for (auto &meteor: meteors) {
|
for (auto &meteor: meteors) {
|
||||||
if (ship->getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
|
if (ship->getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
|
||||||
ship.onHit();
|
ship->takeDamage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto heartIt = hearts.begin(); heartIt != hearts.end();) {
|
for (auto heartIt = hearts.begin(); heartIt != hearts.end();) {
|
||||||
if (ship.getSprite().getGlobalBounds().intersects(heartIt->getSprite().getGlobalBounds())) {
|
if (ship->getSprite().getGlobalBounds().intersects(heartIt->getSprite().getGlobalBounds())) {
|
||||||
ship.healUP();
|
ship->healUP();
|
||||||
heartIt = hearts.erase(heartIt);
|
heartIt = hearts.erase(heartIt);
|
||||||
} else {
|
} else {
|
||||||
++heartIt;
|
++heartIt;
|
||||||
@@ -156,9 +156,9 @@ void Plansza::update() {
|
|||||||
|
|
||||||
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end();) {
|
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end();) {
|
||||||
bool meteorHit = false;
|
bool meteorHit = false;
|
||||||
for (auto rocketIt = ship.getBullets().begin(); rocketIt != ship.getBullets().end();) {
|
for (auto rocketIt = ship->getBullets().begin(); rocketIt != ship->getBullets().end();) {
|
||||||
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
||||||
ship.getBullets().erase(rocketIt);
|
ship->getBullets().erase(rocketIt);
|
||||||
meteorIt = getMeteors().erase(meteorIt);
|
meteorIt = getMeteors().erase(meteorIt);
|
||||||
meteorHit = true;
|
meteorHit = true;
|
||||||
break;
|
break;
|
||||||
@@ -173,9 +173,9 @@ void Plansza::update() {
|
|||||||
|
|
||||||
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end();) {
|
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end();) {
|
||||||
bool meteorHit = false;
|
bool meteorHit = false;
|
||||||
for (auto rocketIt = ship.getRockets().begin(); rocketIt != ship.getRockets().end();) {
|
for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) {
|
||||||
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
||||||
ship.getRockets().erase(rocketIt);
|
ship->getRockets().erase(rocketIt);
|
||||||
meteorIt = getMeteors().erase(meteorIt);
|
meteorIt = getMeteors().erase(meteorIt);
|
||||||
meteorHit = true;
|
meteorHit = true;
|
||||||
break;
|
break;
|
||||||
@@ -189,13 +189,7 @@ void Plansza::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Warunek przeganej
|
|
||||||
if (gameOver) {
|
if (gameOver) {
|
||||||
ship->takeDamage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!ship->isAlive()) {
|
|
||||||
sf::RenderWindow errorWindow(sf::VideoMode(350, 200), "The end");
|
sf::RenderWindow errorWindow(sf::VideoMode(350, 200), "The end");
|
||||||
sf::Font font;
|
sf::Font font;
|
||||||
if (!font.loadFromFile("../assets/fonts/arial.ttf")) {
|
if (!font.loadFromFile("../assets/fonts/arial.ttf")) {
|
||||||
@@ -662,7 +656,7 @@ void Plansza::update() {
|
|||||||
|
|
||||||
|
|
||||||
// Statystyka życia gracza (wyświetlanie)
|
// Statystyka życia gracza (wyświetlanie)
|
||||||
switch (ship.getHP()) {
|
switch (ship->getHP()) {
|
||||||
case 0:
|
case 0:
|
||||||
heartStats[0].setTexture(heartTextureGray);
|
heartStats[0].setTexture(heartTextureGray);
|
||||||
heartStats[1].setTexture(heartTextureGray);
|
heartStats[1].setTexture(heartTextureGray);
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
#include <SFML/Graphics/Text.hpp>
|
#include <SFML/Graphics/Text.hpp>
|
||||||
|
|
||||||
Player::Player(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) {
|
Player::Player(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) {
|
||||||
|
hp = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
Player* Player::getInstance(int x, int y, const sf::Texture& texture) {
|
Player* Player::getInstance(int x, int y, const sf::Texture& texture) {
|
||||||
@@ -21,7 +21,6 @@ Player* Player::getInstance(int x, int y, const sf::Texture& texture) {
|
|||||||
void Player::loadTexture() {
|
void Player::loadTexture() {
|
||||||
bulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png");
|
bulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png");
|
||||||
rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png");
|
rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png");
|
||||||
hp = 3;
|
|
||||||
damageDealClock.restart();
|
damageDealClock.restart();
|
||||||
originalColor = actorSprite.getColor();
|
originalColor = actorSprite.getColor();
|
||||||
}
|
}
|
||||||
@@ -64,43 +63,10 @@ void Player::update() {
|
|||||||
|
|
||||||
void Player::takeDamage() {
|
void Player::takeDamage() {
|
||||||
if (!isImmortal) {
|
if (!isImmortal) {
|
||||||
if (health > 0) {
|
if (hp > 0) {
|
||||||
health--;
|
hp--;
|
||||||
std::cout << "Player hit! Remaining health: " << health << "\n";
|
|
||||||
isImmortal = true; // Aktywuj chwilową nieśmiertelność
|
isImmortal = true; // Aktywuj chwilową nieśmiertelność
|
||||||
immortalityClock.restart();
|
immortalityClock.restart();
|
||||||
|
|
||||||
|
|
||||||
if (health <= 0) {
|
|
||||||
std::cout << "Player has been destroyed!\n";
|
|
||||||
std::cout << "You lost the game!\n";
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Player::isAlive() const {
|
|
||||||
return health > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Player::onHit() {
|
|
||||||
dealDamage();
|
|
||||||
isBlinking = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Player::update() {
|
|
||||||
if (isBlinking) {
|
|
||||||
auto elapsed = damageDealClock.getElapsedTime().asMilliseconds();
|
|
||||||
if (elapsed < 1000) { // miganie przez 1 sekundę
|
|
||||||
if ((elapsed / 100) % 2 == 0) {
|
|
||||||
actorSprite.setColor(sf::Color(255, 255, 255, 128)); // półprzeźroczysty
|
|
||||||
} else {
|
|
||||||
actorSprite.setColor(originalColor); // oryginalny kolor
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
isBlinking = false;
|
|
||||||
actorSprite.setColor(originalColor); // przywróć oryginalny kolor
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user