Serduszka działają

do zrobienia jeszcze animacja uderzenia się w meteoryt (miganie) i wybuch statku przy uderzeniu się w meteoryt po raz trzeci
This commit is contained in:
2024-12-06 12:24:39 +01:00
parent 3b637508e1
commit 2f9e0ba236
6 changed files with 213 additions and 138 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 958 B

View File

@@ -5,6 +5,7 @@
#include "SFML/Graphics/Texture.hpp" #include "SFML/Graphics/Texture.hpp"
#include "Bullet.h" #include "Bullet.h"
#include "Position.h" #include "Position.h"
#include "SFML/System/Clock.hpp"
class Actor { class Actor {
@@ -14,8 +15,9 @@ public:
void loadTexture(std::string path); void loadTexture(std::string path);
sf::Sprite& getSprite(); sf::Sprite& getSprite();
unsigned int getHP();
Position getPosition(); Position getPosition();
std::vector<Bullet>& getBullets();
virtual void move(float deltaX, float deltaY) = 0; virtual void move(float deltaX, float deltaY) = 0;
virtual void moveLeft() = 0; virtual void moveLeft() = 0;
@@ -24,19 +26,19 @@ public:
virtual void moveDown() = 0; virtual void moveDown() = 0;
virtual void shoot() = 0; virtual void shoot() = 0;
std::vector<Bullet>& getBullets();
void updateBullets(); void updateBullets();
void setMovingSpeed(float speed); void setMovingSpeed(float speed);
void dealDamage();
void healUP();
protected: protected:
sf::Sprite actorSprite; sf::Sprite actorSprite;
sf::Texture actorTexture; sf::Texture actorTexture;
sf::Texture bulletTextureLeft; sf::Texture bulletTextureLeft;
sf::Texture bulletTextureRight; sf::Texture bulletTextureRight;
sf::Clock damageDealClock;
Position position; Position position;
unsigned int hp; int hp;
unsigned int damage; unsigned int damage;
unsigned int firerate; unsigned int firerate;
float moving_speed; float moving_speed;

View File

@@ -25,19 +25,21 @@ public:
void update_hearts(); void update_hearts();
void update(); void update();
private: private:
Size size; sf::RenderWindow *window;
Background background; Background background;
Player ship; Player ship;
AudioManager audioManager; AudioManager audioManager;
Size size;
sf::Texture meteorTexture1; sf::Texture meteorTexture1;
sf::Texture meteorTexture2; sf::Texture meteorTexture2;
sf::Texture heartTexture; sf::Texture heartTexture;
sf::Texture heartTextureGray;
sf::Clock meteorSpawnClock; sf::Clock meteorSpawnClock;
sf::Clock heartSpawnClock; sf::Clock heartSpawnClock;
std::vector<Meteor> meteors; std::vector<Meteor> meteors;
std::vector<Heart> hearts; std::vector<Heart> hearts;
sf::RenderWindow *window; std::vector<sf::Sprite> heartStats;
bool gameOver = false;
}; };
#endif //PLANSZA_H #endif //PLANSZA_H

View File

@@ -24,6 +24,25 @@ Position Actor::getPosition() {
return {position.x, position.y}; return {position.x, position.y};
} }
unsigned int Actor::getHP() {
return hp;
}
void Actor::dealDamage() {
if(damageDealClock.getElapsedTime().asSeconds() > 1) {
if(hp > 0) {
hp--;
}
damageDealClock.restart();
}
}
void Actor::healUP() {
if(hp < 3){
hp++;
}
}
std::vector<Bullet> &Actor::getBullets() { std::vector<Bullet> &Actor::getBullets() {
return bullets; return bullets;
} }

View File

@@ -1,13 +1,11 @@
#include <random> #include <random>
#include <iostream> #include <iostream>
#include "../headers/Plansza.h" #include "../headers/Plansza.h"
#include "../headers/ObjectItem.hpp"
Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow) Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow)
: background("../assets/img/background/background.png", 2.0f), : background("../assets/img/background/background.png", 2.0f),
ship(static_cast<int>(mainWindow->getSize().x) / 2, static_cast<int>(mainWindow->getSize().y) - 100, "../assets/ship/Dreadnought-Base.png") ship(static_cast<int>(mainWindow->getSize().x) / 2, static_cast<int>(mainWindow->getSize().y) - 100, "../assets/ship/Dreadnought-Base.png")
{ {
window = mainWindow; window = mainWindow;
size.height = static_cast<int>(windowHeight); size.height = static_cast<int>(windowHeight);
size.width = static_cast<int>(windowWidth); size.width = static_cast<int>(windowWidth);
@@ -26,6 +24,14 @@ ship(static_cast<int>(mainWindow->getSize().x) / 2, static_cast<int>(mainWindow-
meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png"); meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png");
heartTexture.loadFromFile("../assets/img/hearts/heart.png"); heartTexture.loadFromFile("../assets/img/hearts/heart.png");
heartTextureGray.loadFromFile("../assets/img/hearts/heart_gray.png");
heartStats.emplace_back(sf::Sprite(heartTexture));
heartStats.emplace_back(sf::Sprite(heartTexture));
heartStats.emplace_back(sf::Sprite(heartTexture));
heartStats[0].setPosition(10, 10);
heartStats[1].setPosition(45, 10);
heartStats[2].setPosition(80, 10);
meteorSpawnClock.restart(); meteorSpawnClock.restart();
} }
@@ -68,7 +74,7 @@ void Plansza::update() {
audioManager.playSoundEffect("shoot_alt", 70.f); // Odtworzenie dźwięku dla alternatywnego strzału audioManager.playSoundEffect("shoot_alt", 70.f); // Odtworzenie dźwięku dla alternatywnego strzału
} }
// generowanie nowego meteoru // generowanie nowego meteoru i serduszka
spawn_meteor(); spawn_meteor();
spawn_hearts(); spawn_hearts();
@@ -103,35 +109,18 @@ void Plansza::update() {
// trochę dziwny sposób ale jednak działa // trochę dziwny sposób ale jednak działa
for (auto& meteor : getMeteors()) { for (auto &meteor: meteors) {
if (ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) { if (ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
std::cout << "You lost the game!\n"; ship.dealDamage();
sf::RenderWindow errorWindow(sf::VideoMode(350, 200), "The end"); }
sf::Font font;
if (!font.loadFromFile("../assets/fonts/arial.ttf")) {
std::cerr << "Error loading font\n";
exit(-500);
} }
sf::Text text("Your ship is destroyed!", font, 24);
text.setFillColor(sf::Color::Red);
text.setPosition(50, 80);
// zatrzymanie muzyki i odtworzenie dźwięku przegranej for (auto heartIt = hearts.begin(); heartIt != hearts.end();) {
audioManager.playSoundEffect("fail", 70.f); if (ship.getSprite().getGlobalBounds().intersects(heartIt->getSprite().getGlobalBounds())) {
audioManager.stopBackgroundMusic(); ship.healUP();
sf::Event event{}; heartIt = hearts.erase(heartIt);
while (errorWindow.isOpen()) { } else {
while (errorWindow.pollEvent(event)) { ++heartIt;
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
errorWindow.close();
window->close();
exit(-2);
}
}
errorWindow.clear();
errorWindow.draw(text);
errorWindow.display();
}
} }
} }
@@ -168,10 +157,70 @@ void Plansza::update() {
++meteorIt; ++meteorIt;
} }
} }
// Warunek przeganej
if (gameOver) {
std::cout << "You lost the game!\n";
sf::RenderWindow errorWindow(sf::VideoMode(350, 200), "The end");
sf::Font font;
if (!font.loadFromFile("../assets/fonts/arial.ttf")) {
std::cerr << "Error loading font\n";
exit(-500);
}
sf::Text text("Your ship is destroyed!", font, 24);
text.setFillColor(sf::Color::Red);
text.setPosition(50, 80);
// zatrzymanie muzyki i odtworzenie dźwięku przegranej
audioManager.playSoundEffect("fail", 70.f);
audioManager.stopBackgroundMusic();
sf::Event event{};
while (errorWindow.isOpen()) {
while (errorWindow.pollEvent(event)) {
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
errorWindow.close();
window->close();
exit(-2);
}
}
errorWindow.clear();
errorWindow.draw(text);
errorWindow.display();
}
} }
// Meteor-related niżej // Statystyka życia gracza (wyświetlanie)
switch (ship.getHP()) {
case 0:
heartStats[0].setTexture(heartTextureGray);
heartStats[1].setTexture(heartTextureGray);
heartStats[2].setTexture(heartTextureGray);
gameOver = true;
break;
case 1:
heartStats[0].setTexture(heartTexture);
heartStats[1].setTexture(heartTextureGray);
heartStats[2].setTexture(heartTextureGray);
break;
case 2:
heartStats[0].setTexture(heartTexture);
heartStats[1].setTexture(heartTexture);
heartStats[2].setTexture(heartTextureGray);
break;
case 3:
heartStats[0].setTexture(heartTexture);
heartStats[1].setTexture(heartTexture);
heartStats[2].setTexture(heartTexture);
break;
}
for (auto heart: heartStats) {
window->draw(heart);
}
}
// Meteor-related niżej
void Plansza::spawn_meteor() { void Plansza::spawn_meteor() {
if (meteorSpawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy if (meteorSpawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy
if (meteors.size() < 5) { // jeśli jest mniej niż 5 meteorów na planszy if (meteors.size() < 5) { // jeśli jest mniej niż 5 meteorów na planszy

View File

@@ -1,9 +1,12 @@
#include "../headers/Player.h" #include <utility>
#include "../headers/Bullet.h"
Player::Player(int x, int y, std::string path) : Actor(x, y, path) { #include "../headers/Player.h"
Player::Player(int x, int y, std::string path) : Actor(x, y, std::move(path)) {
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();
}; };
void Player::shoot() { void Player::shoot() {