6 Commits

Author SHA1 Message Date
80a4b1b397 Złączono i wszystko działa, ale trzeba zrobić mocny refactor Planszy bo za dużo niewiadomo skąd biorących się pętli 2024-12-13 20:05:11 +01:00
fdf67f4bc2 Merge remote-tracking branch 'origin/serduszka'
# Conflicts:
#	CMakeLists.txt
#	headers/Actor.h
#	headers/Plansza.h
#	headers/Player.h
#	sources/Plansza.cpp
#	sources/Player.cpp
2024-12-13 19:48:39 +01:00
da33fcc718 Adjustin hearts to be in right upper corner due to score counter in left upper corner 2024-12-10 20:37:10 +01:00
71f8ebe285 Statek miga
przy uderzeniu w meteoryt
2024-12-06 15:16:57 +01:00
2f9e0ba236 Serduszka działają
do zrobienia jeszcze animacja uderzenia się w meteoryt (miganie) i wybuch statku przy uderzeniu się w meteoryt po raz trzeci
2024-12-06 12:24:39 +01:00
3b637508e1 Added heart and spawn of them 2024-12-05 14:52:00 +01:00
10 changed files with 227 additions and 48 deletions

View File

@@ -40,6 +40,8 @@ add_executable(LotoStatek main.cpp
sources/Wiazkowiec.cpp sources/Wiazkowiec.cpp
headers/Beam.h headers/Beam.h
sources/Beam.cpp sources/Beam.cpp
headers/Heart.hpp
sources/Heart.cpp
) )
if(WIN32) if(WIN32)

Binary file not shown.

After

Width:  |  Height:  |  Size: 958 B

View File

@@ -5,12 +5,20 @@
#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 {
public: public:
Actor(int x, int y, const sf::Texture& texture); Actor(int x, int y, const sf::Texture& texture);
sf::Sprite& getSprite();
unsigned int getHP();
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;
virtual void moveRight() = 0; virtual void moveRight() = 0;
@@ -18,13 +26,10 @@ 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 setMovingSpeed(float speed);
void updateBullets(); void updateBullets();
void setMovingSpeed(float speed);
void dealDamage();
void healUP();
protected: protected:
Position position; Position position;
@@ -33,7 +38,8 @@ protected:
sf::Texture bulletTexture; sf::Texture bulletTexture;
sf::Texture rocketTexture; sf::Texture rocketTexture;
std::vector<Bullet> bullets; std::vector<Bullet> bullets;
unsigned int hp; sf::Clock damageDealClock;
int hp;
unsigned int damage; unsigned int damage;
unsigned int firerate; unsigned int firerate;
float moving_speed; float moving_speed;

14
headers/Heart.hpp Normal file
View File

@@ -0,0 +1,14 @@
#ifndef LOTOSTATEK_HEART_HPP
#define LOTOSTATEK_HEART_HPP
#include "SFML/Graphics/Texture.hpp"
#include "SFML/Graphics/Sprite.hpp"
#include "ObjectItem.hpp"
class Heart : public ObjectItem {
public:
Heart(float x, float y, sf::Texture &texture);
void update();
};
#endif //LOTOSTATEK_HEART_HPP

View File

@@ -14,6 +14,7 @@
#include "Background.h" #include "Background.h"
#include "AudioManager.h" #include "AudioManager.h"
#include "Plansza.h" #include "Plansza.h"
#include "Heart.hpp"
#include "Size.h" #include "Size.h"
class Plansza { class Plansza {
@@ -22,7 +23,9 @@ public:
Size getSize(); Size getSize();
std::vector<Meteor> &getMeteors(); std::vector<Meteor> &getMeteors();
void spawn_meteor(); void spawn_meteor();
void spawn_hearts();
void update_meteors(); void update_meteors();
void update_hearts();
void update(); void update();
void update_score(); void update_score();
void setOutOfBounds(bool status); void setOutOfBounds(bool status);
@@ -36,25 +39,14 @@ public:
delete ship; // usuwanie wskaźnika ship delete ship; // usuwanie wskaźnika ship
} }
private: private:
Size size;
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;
sf::Texture meteorTexture1; sf::Clock meteorSpawnClock;
sf::Texture meteorTexture2; 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;
@@ -63,14 +55,31 @@ 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;
std::vector<Kamikadze> KEnemies; std::vector<Kamikadze> KEnemies;
std::vector<Wiazkowiec> WEnemies; std::vector<Wiazkowiec> WEnemies;
std::vector<Meteor> meteors; std::vector<Meteor> meteors;
std::vector<Heart> hearts;
std::vector<sf::Sprite> heartStats;
bool gameOver = false;
unsigned int score = 0; unsigned int score = 0;
bool isPlayerSpawned = false;
}; };
#endif //PLANSZA_H #endif //PLANSZA_H

View File

@@ -1,6 +1,7 @@
#ifndef LOTOSTATEK_PLAYER_H #ifndef LOTOSTATEK_PLAYER_H
#define LOTOSTATEK_PLAYER_H #define LOTOSTATEK_PLAYER_H
#include <chrono> #include <chrono>
#include <SFML/System/Clock.hpp> #include <SFML/System/Clock.hpp>
@@ -28,7 +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 update();
std::vector<Rocket>& getRockets(); std::vector<Rocket>& getRockets();
@@ -39,8 +40,8 @@ private:
std::vector<Rocket> rockets; std::vector<Rocket> rockets;
sf::Texture rocketTexture; sf::Texture rocketTexture;
sf::Texture bulletTexture; sf::Texture bulletTexture;
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
}; };

View File

@@ -21,6 +21,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.5) {
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;
} }
@@ -39,4 +58,4 @@ void Actor::updateBullets() {
void Actor::setMovingSpeed(float speed) { void Actor::setMovingSpeed(float speed) {
moving_speed = speed; moving_speed = speed;
} }

23
sources/Heart.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "../headers/Heart.hpp"
Heart::Heart(float x, float y, sf::Texture &texture) : ObjectItem(x,y,texture) {
outOfBounds = false;
texture = texture;
sprite.setTexture(texture);
sprite.setOrigin(sprite.getLocalBounds().width / 2, sprite.getLocalBounds().height / 2); // wycentrowanie sprite
sprite.setPosition(x, y);
movingSpeed = 3.0f;
// sprite.scale(0.05f, 0.05f);
position.x = x;
position.y = y;
rotationSpeed = 0;
}
void Heart::update() {
sprite.move(0.0f, movingSpeed); // przesunięcie sprajta
position.y += int(movingSpeed); // przesunięcie pozycji
// sprite.rotate(rotationSpeed); // obracanie tym meteorkiem pięknym
if(position.y > 900) {
outOfBounds = true; // jeżeli wyszedł poza granice ekranu ustaw tą zmienną
}
}

View File

@@ -40,12 +40,26 @@ Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::Render
audioManager.loadSoundEffect("shoot_alt", "../assets/sounds/shoot_alt.ogg"); audioManager.loadSoundEffect("shoot_alt", "../assets/sounds/shoot_alt.ogg");
audioManager.loadSoundEffect("fail", "../assets/sounds/fail.mp3"); audioManager.loadSoundEffect("fail", "../assets/sounds/fail.mp3");
meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png");
meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.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(565, 10);
heartStats[1].setPosition(530, 10);
heartStats[2].setPosition(495, 10);
meteorSpawnClock.restart();
spawn_player(); spawn_player();
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();
@@ -88,19 +102,26 @@ void Plansza::update() {
update_score(); // naliczanie punktów update_score(); // naliczanie punktów
// Sprawnowanie wszystkich rodzajów wrogów // Sprawnowanie wszystkich rodzajów wrogów
spawn_meteor(); spawn_meteor();
spawn_hearts();
spawn_enemy(); spawn_enemy();
spawn_advanced_enemy(); spawn_advanced_enemy();
spawn_wiazkowiec(); spawn_wiazkowiec();
spawn_bomber(); spawn_bomber();
spawn_kamikadze(); spawn_kamikadze();
// utrzymanie meteorów i pocisków w ruchu // utrzymanie meteorów i pocisków w ruchu
for (auto &meteor: getMeteors()) { for (auto &meteor: meteors) {
meteor.update(); meteor.update();
window->draw(meteor.getSprite()); window->draw(meteor.getSprite());
} }
for (auto &bullet: ship->getBullets()) { for (auto &heart: hearts) {
heart.update();
window->draw(heart.getSprite());
}for (auto &bullet: ship->getBullets()) {
bullet.update(); bullet.update();
window->draw(bullet.getSprite()); window->draw(bullet.getSprite());
} }
@@ -112,17 +133,63 @@ void Plansza::update() {
// Sprawdzenie czy meteory i pociski są poza granicami ekranu // Sprawdzenie czy meteory i pociski są poza granicami ekranu
update_meteors(); update_meteors();
update_hearts();
ship->updateBullets(); ship->updateBullets();
ship->update();
window->draw(ship->getSprite()); window->draw(ship->getSprite());
for (auto &meteor: getMeteors()) { for (auto &meteor: meteors) {
if (ship->getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) { if (ship->getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
ship->takeDamage(); ship->takeDamage();
} }
} }
if (!ship->isAlive()) { for (auto heartIt = hearts.begin(); heartIt != hearts.end();) {
if (ship->getSprite().getGlobalBounds().intersects(heartIt->getSprite().getGlobalBounds())) {
ship->healUP();
heartIt = hearts.erase(heartIt);
} else {
++heartIt;
}
}
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end();) {
bool meteorHit = false;
for (auto rocketIt = ship->getBullets().begin(); rocketIt != ship->getBullets().end();) {
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
ship->getBullets().erase(rocketIt);
meteorIt = getMeteors().erase(meteorIt);
meteorHit = true;
break;
} else {
++rocketIt;
}
}
if (!meteorHit) {
++meteorIt;
}
}
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end();) {
bool meteorHit = false;
for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) {
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
ship->getRockets().erase(rocketIt);
meteorIt = getMeteors().erase(meteorIt);
meteorHit = true;
break;
} else {
++rocketIt;
}
}
if (!meteorHit) {
++meteorIt;
}
}
if (gameOver) {
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")) {
@@ -588,8 +655,36 @@ void Plansza::update() {
} }
} // 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 (const auto& heart: heartStats) {
window->draw(heart);
}
}
// Meteor-related niżej // Meteor-related niżej
@@ -603,7 +698,7 @@ void Plansza::update_meteors() {
} }
void Plansza::spawn_meteor() { void Plansza::spawn_meteor() {
if (spawnClock.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
if (rand() % 2 == 1) { if (rand() % 2 == 1) {
meteors.emplace_back(RandomNumberGenerator::getRandomNumber(50, 499), -100, meteorTexture2); meteors.emplace_back(RandomNumberGenerator::getRandomNumber(50, 499), -100, meteorTexture2);
@@ -611,7 +706,16 @@ void Plansza::spawn_meteor() {
meteors.emplace_back(RandomNumberGenerator::getRandomNumber(50, 499), -100, meteorTexture1); meteors.emplace_back(RandomNumberGenerator::getRandomNumber(50, 499), -100, meteorTexture1);
} }
} }
spawnClock.restart(); meteorSpawnClock.restart();
}
}
void Plansza::spawn_hearts() {
if (heartSpawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy
if (hearts.size() < 5) { // jeśli jest mniej niż 5 meteorów na planszy
hearts.emplace_back(RandomNumberGenerator::getRandomNumber(50, 499), -100, heartTexture);
}
heartSpawnClock.restart();
} }
} }
@@ -673,6 +777,15 @@ void Plansza::spawn_wiazkowiec() {
} }
void Plansza::update_hearts() {
// usuwanie serduszek które wyleciały poza ekran
for (auto& heart : hearts) {
if(heart.getStatus()) {
hearts.erase(hearts.begin());
}
}
}
Size Plansza::getSize() { Size Plansza::getSize() {
return size; return size;
} }

View File

@@ -1,3 +1,5 @@
#include <utility>
#include "../headers/Player.h" #include "../headers/Player.h"
#include <iostream> #include <iostream>
@@ -6,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) {
@@ -19,6 +21,8 @@ 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");
damageDealClock.restart();
originalColor = actorSprite.getColor();
} }
void Player::shoot() { void Player::shoot() {
@@ -59,26 +63,14 @@ 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::setFirerate(unsigned int firerate) { void Player::setFirerate(unsigned int firerate) {
this->firerate = firerate; this->firerate = firerate;
} }