1 Commits

Author SHA1 Message Date
3e2d37cf72 Update README.md 2025-01-03 17:28:54 +01:00
22 changed files with 206 additions and 186 deletions

View File

@@ -1 +1,11 @@
Tu będzie lotoł statek The space-invaders type game created for university project.
The main character is a "Ship", with which you are fighting enemies that have different abilities, for example laser beams or "Kamikaze" that will try to explode at you.
Your character has only 3 HP that are showed in right upper corner.
There are 5 different types of enemies:
1. The simpliest one - just shoots you with one bullet
2. The advanced enemy - shoots you with the same bullet, but shooted 3 at the one piece of time
3. The "Kamikaze" will explode at you once reaches you, dealing 2 hp damage.
4. The Laser enemy will shoot you with laser beam both sideways and up and down direction, dealing 1 hp damage.
5. The bomber - will leave bombs all around map which will deal 1 hp damage once exploded.

View File

@@ -11,7 +11,8 @@
class Actor { class Actor {
public: public:
Actor(int x, int y, const sf::Texture& texture); Actor(int x, int y, const sf::Texture& texture);
virtual ~Actor() = default;
sf::Sprite& getSprite(); sf::Sprite& getSprite();
unsigned int getHP(); unsigned int getHP();

View File

@@ -3,6 +3,8 @@
#include "Enemy.h" #include "Enemy.h"
#include <cmath>
enum class DirectionA { enum class DirectionA {
Up, Up,
Down, Down,

View File

@@ -2,16 +2,27 @@
#define LOTOSTATEK_BEAM_H #define LOTOSTATEK_BEAM_H
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "Position.h"
class Beam { class Beam {
public: public:
Beam() = default; Beam(float x, float y, float width, float height, const sf::Color& color);
Beam(int x, int y, const sf::Texture &texture);
void draw(sf::RenderWindow &window);
void update();
void render(sf::RenderWindow& window);
sf::FloatRect getBounds() const;
bool isVisible() const;
void setVisible(bool visible);
sf::Sprite getSprite();
void setRotation(float angle);
private: private:
sf::RectangleShape beamShape;
bool visible;
sf::Texture beamTexture;
sf::Sprite beamSprite; sf::Sprite beamSprite;
}; };

View File

@@ -2,11 +2,14 @@
#define LOTOSTATEK_BULLET_H #define LOTOSTATEK_BULLET_H
#include "Projectile.h" #include "Projectile.h"
#include "SFML/Graphics/Texture.hpp"
class Bullet : public Projectile { class Bullet : public Projectile {
public: public:
Bullet(float x, float y, sf::Texture &texture) : Projectile(x,y, texture) {}; Bullet(float x, float y, sf::Texture &texture) : Projectile(x,y, texture) {};
void update() override; void update() override;
private:
float directionY;
}; };

View File

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

View File

@@ -8,7 +8,7 @@
class Meteor : public ObjectItem { class Meteor : public ObjectItem {
public: public:
Meteor(float x, float y, sf::Texture &texture_); Meteor(float x, float y, sf::Texture &texture);
void update(); void update();
}; };

View File

@@ -12,7 +12,7 @@ public:
bool getStatus(); bool getStatus();
virtual void update() = 0; virtual void update() = 0;
protected: protected:
sf::Texture texture_; sf::Texture texture;
sf::Sprite sprite; sf::Sprite sprite;
Position position; Position position;
float rotationSpeed; float rotationSpeed;

View File

@@ -6,14 +6,4 @@ struct Position {
int y; int y;
}; };
struct PositionU {
unsigned int x;
unsigned int y;
};
struct PositionF {
float x;
float y;
};
#endif //LOTOSTATEK_POSITION_H #endif //LOTOSTATEK_POSITION_H

View File

@@ -13,47 +13,42 @@ enum class DirectionW {
class Wiazkowiec : public Actor { class Wiazkowiec : public Actor {
public: public:
Wiazkowiec(int x, int y, const sf::Texture& texture, sf::RenderWindow *window); Wiazkowiec(int x, int y, const sf::Texture& texture);
void shoot() override;
void move(float deltaX, float deltaY) override; void move(float deltaX, float deltaY) override;
void moveLeft() override; void moveLeft() override;
void moveRight() override; void moveRight() override;
void moveUp() override; void moveUp() override;
void moveDown() override; void moveDown() override;
void takeDamage();
void setRandomDirection(); void setRandomDirection();
void checkIfBeamShootOutOfBounds();
void update(); void update();
void shoot() override;
void render(sf::RenderWindow &window); void render(sf::RenderWindow &window);
bool isAlive() const; bool isAlive() const;
void takeDamage();
void updateDirection();
bool isShooting() const; bool isShooting() const;
const Beam& getBeam() const;
void setPlanszaHeight(float height, float width);
void setMapBounds(float width, float height);
void setPlanszaHeight(int height, int width);
Beam* getBeam() const;
private: private:
sf::Texture WiazkaTexture; float planszaHeight = 800.f;
sf::Texture beamTexture; float planszaWidth = 600.f;
sf::Clock shootClock; sf::Clock shootClock;
sf::Clock shootingClock; sf::Texture WiazkaTexture;
DirectionW direction = DirectionW::Down;
Beam *beam; // wskaźnik na wiązkę
sf::RenderWindow *window_ptr;
void spawnBeam(); // Tworzy wiązkę
int planszaHeight = 800;
int planszaWidth = 600;
float beamDuration = 1.0f;
float movementSpeed = 2.0f; float movementSpeed = 2.0f;
bool shooting = false;
bool alive = true; bool alive = true;
DirectionW direction = DirectionW::Down;
Beam beam; // Wiązka
bool shooting = false;
sf::Clock shootingClock;
float beamDuration = 1.0f;
void spawnBeam(); // Tworzy wiązkę
}; };
#endif //WIAZKOWIEC_H #endif //WIAZKOWIEC_H

View File

@@ -3,6 +3,7 @@
#include "../headers/Plansza.h" #include "../headers/Plansza.h"
AdvancedEnemy::AdvancedEnemy(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture) : Actor(x, y, texture) { AdvancedEnemy::AdvancedEnemy(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture) : Actor(x, y, texture) {
actorSprite.setTexture(texture);
enemyBulletTexture = bulletTexture; enemyBulletTexture = bulletTexture;
hp = 2; // 2 punkty życia hp = 2; // 2 punkty życia
firerate = 2000; // Strzela co 2 firerate = 2000; // Strzela co 2

View File

@@ -2,21 +2,50 @@
#include <iostream> #include <iostream>
Beam::Beam(int x, int y, const sf::Texture& texture) { Beam::Beam(float x, float y, float width, float height, const sf::Color& color)
beamSprite.setOrigin(beamSprite.getLocalBounds().width/2, beamSprite.getLocalBounds().height/2); : visible(false) {
if (texture.getSize().x > 0 && texture.getSize().y > 0) { beamShape.setPosition(x, y);
beamShape.setSize({width, height});
beamShape.setFillColor(color);
if (!beamTexture.loadFromFile("../assets/img/wiazka/wiazka.png")) {
std::cerr << "Błąd! Nie można załadować tekstury wiazka.png" << std::endl;
}
beamSprite.setTexture(beamTexture);
if (beamTexture.getSize().x > 0 && beamTexture.getSize().y > 0) {
float scaleX = width / beamTexture.getSize().x;
float scaleY = height / beamTexture.getSize().y;
beamSprite.setScale(scaleX, scaleY);
beamSprite.setPosition(x, y); beamSprite.setPosition(x, y);
} else { } else {
std::cerr << "Błąd: Tekstura wiązki nie została poprawnie załadowana." << std::endl; std::cerr << "Błąd: Tekstura wiązki nie została poprawnie załadowana." << std::endl;
} }
beamSprite.setTexture(texture);
} }
sf::Sprite Beam::getSprite() { void Beam::draw(sf::RenderWindow& window) {
return beamSprite;
window.draw(beamSprite);
} }
void Beam::setRotation(float angle) { void Beam::update() {
beamSprite.setRotation(angle); }
void Beam::render(sf::RenderWindow& window) {
window.draw(beamSprite);
}
sf::FloatRect Beam::getBounds() const {
return beamShape.getGlobalBounds();
}
bool Beam::isVisible() const {
return visible;
}
void Beam::setVisible(bool visible) {
this->visible = visible;
} }

View File

@@ -4,6 +4,7 @@
#include <random> #include <random>
Bomber::Bomber(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture) : Actor(x, y, texture) { Bomber::Bomber(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture) : Actor(x, y, texture) {
actorSprite.setTexture(texture);
BombaTexture = bulletTexture; BombaTexture = bulletTexture;
hp = 2; // 2 punkty życia hp = 2; // 2 punkty życia
firerate = 10000; // Strzela co 10 firerate = 10000; // Strzela co 10

View File

@@ -1,5 +1,8 @@
#include "../headers/Bullet.h" #include "../headers/Bullet.h"
#include <iostream>
#include <ostream>
void Bullet::update() { void Bullet::update() {
//std::cout << "Start update: speed = " << speed << ", position.y = " << position.y << std::endl; //std::cout << "Start update: speed = " << speed << ", position.y = " << position.y << std::endl;
sprite.move(0.0f, speed); sprite.move(0.0f, speed);

View File

@@ -3,6 +3,7 @@
#include "../headers/Plansza.h" #include "../headers/Plansza.h"
Enemy::Enemy(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) { Enemy::Enemy(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) {
actorSprite.setTexture(texture);
hp = 1; // Przeciwnik ma 1 punkt życia hp = 1; // Przeciwnik ma 1 punkt życia
firerate = 2000; // Strzela co 2 firerate = 2000; // Strzela co 2
moving_speed = 2.0f; // Prędkość moving_speed = 2.0f; // Prędkość

View File

@@ -7,6 +7,7 @@
Kamikadze::Kamikadze(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) { Kamikadze::Kamikadze(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) {
actorSprite.setTexture(texture);
hp = 3; // 3 punkty życia hp = 3; // 3 punkty życia
moving_speed = 2.0f; // Prędkość moving_speed = 2.0f; // Prędkość
} }

View File

@@ -2,7 +2,7 @@
Meteor::Meteor(float x, float y, sf::Texture &texture) : ObjectItem(x, y, texture) { Meteor::Meteor(float x, float y, sf::Texture &texture) : ObjectItem(x, y, texture) {
outOfBounds = false; outOfBounds = false;
texture_ = texture; texture = texture;
sprite.setTexture(texture); sprite.setTexture(texture);
sprite.setOrigin(sprite.getLocalBounds().width / 2, sprite.getLocalBounds().height / 2); // wycentrowanie sprite sprite.setOrigin(sprite.getLocalBounds().width / 2, sprite.getLocalBounds().height / 2); // wycentrowanie sprite
sprite.setPosition(x, y); sprite.setPosition(x, y);

View File

@@ -1,11 +1,11 @@
#include "../headers/ObjectItem.hpp" #include "../headers/ObjectItem.hpp"
ObjectItem::ObjectItem(float x, float y, sf::Texture &texture) { ObjectItem::ObjectItem(float x, float y, sf::Texture &texture) {
Position position_ = {0,0}; Position position_;
position_.x = static_cast<int>(x); position_.x = x;
position_.y = static_cast<int>(y); position_.y = y;
position = position_; position = position_;
this->texture_ = texture; this->texture = texture;
} }
bool ObjectItem::getStatus() { bool ObjectItem::getStatus() {

View File

@@ -111,13 +111,13 @@ void Plansza::update() {
ship->update(); // migotanie statku ship->update(); // migotanie statku
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_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: meteors) { for (auto &meteor: meteors) {
@@ -140,7 +140,7 @@ void Plansza::update() {
window->draw(rocket.getSprite()); window->draw(rocket.getSprite());
} }
// Sprawdzenie, czy meteory i pociski są poza granicami ekranu // Sprawdzenie czy meteory i pociski są poza granicami ekranu
update_meteors(); update_meteors();
update_hearts(); update_hearts();
ship->updateBullets(); ship->updateBullets();
@@ -406,7 +406,7 @@ void Plansza::update() {
if (playerBulletIt->getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) { if (playerBulletIt->getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) {
// Usuń pocisk Bombera i pocisk gracza // Usuń pocisk Bombera i pocisk gracza
it = bomberEnemy.getBullets().erase(it); it = bomberEnemy.getBullets().erase(it);
ship->getBullets().erase(playerBulletIt); playerBulletIt = ship->getBullets().erase(playerBulletIt);
bulletDestroyed = true; bulletDestroyed = true;
break; break;
} else { } else {
@@ -420,7 +420,7 @@ void Plansza::update() {
if (rocketIt->getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) { if (rocketIt->getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) {
// Usuń pocisk Bombera i rakietę gracza // Usuń pocisk Bombera i rakietę gracza
it = bomberEnemy.getBullets().erase(it); it = bomberEnemy.getBullets().erase(it);
ship->getRockets().erase(rocketIt); rocketIt = ship->getRockets().erase(rocketIt);
bulletDestroyed = true; bulletDestroyed = true;
break; break;
} else { } else {
@@ -445,13 +445,14 @@ void Plansza::update() {
for (auto &wiazkowiec: WEnemies) { for (auto &wiazkowiec: WEnemies) {
wiazkowiec.update(); wiazkowiec.update();
if (wiazkowiec.isShooting()) { if (wiazkowiec.isShooting() && wiazkowiec.getBeam().isVisible()) {
if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam()->getSprite().getGlobalBounds())) { if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam().getBounds())) {
ship->takeDamage(); // Gracz otrzymuje obrażenia ship->takeDamage(); // Gracz otrzymuje obrażenia
} }
} }
window->draw(wiazkowiec.getSprite()); window->draw(wiazkowiec.getSprite());
wiazkowiec.render(*window);
} }
// TODO: naprawić to co średnio działa // TODO: naprawić to co średnio działa
@@ -473,7 +474,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) {
if (enemyIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { if (enemyIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) {
ship->getBullets().erase(bulletIt); bulletIt = ship->getBullets().erase(bulletIt);
enemyIt->takeDamage(); enemyIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -492,7 +493,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) {
if (advancedIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { if (advancedIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) {
ship->getBullets().erase(bulletIt); bulletIt = ship->getBullets().erase(bulletIt);
advancedIt->takeDamage(); advancedIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -511,7 +512,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) {
if (bomberIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { if (bomberIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) {
ship->getBullets().erase(bulletIt); bulletIt = ship->getBullets().erase(bulletIt);
bomberIt->takeDamage(); bomberIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -530,7 +531,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) {
if (kamikadzeIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { if (kamikadzeIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) {
ship->getBullets().erase(bulletIt); bulletIt = ship->getBullets().erase(bulletIt);
kamikadzeIt->takeDamage(); kamikadzeIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -549,7 +550,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) {
if (wiazkowiecIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { if (wiazkowiecIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) {
ship->getBullets().erase(bulletIt); bulletIt = ship->getBullets().erase(bulletIt);
wiazkowiecIt->takeDamage(); wiazkowiecIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -571,7 +572,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) {
if (enemyIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { if (enemyIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
ship->getRockets().erase(rocketIt); rocketIt = ship->getRockets().erase(rocketIt);
enemyIt->takeDamage(); enemyIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -590,7 +591,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) {
if (advancedIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { if (advancedIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
ship->getRockets().erase(rocketIt); rocketIt = ship->getRockets().erase(rocketIt);
advancedIt->takeDamage(); advancedIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -609,7 +610,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) {
if (bomberIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { if (bomberIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
ship->getRockets().erase(rocketIt); rocketIt = ship->getRockets().erase(rocketIt);
bomberIt->takeDamage(); bomberIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -628,7 +629,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) {
if (kamikadzeIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { if (kamikadzeIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
ship->getRockets().erase(rocketIt); rocketIt = ship->getRockets().erase(rocketIt);
kamikadzeIt->takeDamage(); kamikadzeIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -647,7 +648,7 @@ void Plansza::update() {
bool hit = false; bool hit = false;
for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) {
if (wiazkowiecIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { if (wiazkowiecIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
ship->getRockets().erase(rocketIt); rocketIt = ship->getRockets().erase(rocketIt);
wiazkowiecIt->takeDamage(); wiazkowiecIt->takeDamage();
hit = true; hit = true;
break; break;
@@ -686,8 +687,6 @@ void Plansza::update() {
heartStats[1].setTexture(heartTexture); heartStats[1].setTexture(heartTexture);
heartStats[2].setTexture(heartTexture); heartStats[2].setTexture(heartTexture);
break; break;
default:
break;
} }
@@ -699,7 +698,7 @@ void Plansza::update() {
void Plansza::update_meteors() { void Plansza::update_meteors() {
// usuwanie meteorów, które wyleciały poza ekran // usuwanie meteorów które wyleciały poza ekran
for (auto &meteor: meteors) { for (auto &meteor: meteors) {
if (meteor.getStatus()) { if (meteor.getStatus()) {
meteors.erase(meteors.begin()); meteors.erase(meteors.begin());
@@ -776,21 +775,19 @@ void Plansza::spawn_kamikadze() {
} }
void Plansza::spawn_wiazkowiec() { void Plansza::spawn_wiazkowiec() {
if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 5) { // Spawn co 10 sekund if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 50) { // Spawn co 10 sekund
if (WEnemies.size() < 1) {
int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50); int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50);
Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture, window); Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture);
wiazkowiec.setPlanszaHeight(size.height, size.width); // Przekazanie wysokości i szerokosci okna wiazkowiec.setPlanszaHeight(size.height, size.width); // Przekazanie wysokości i szerokosci okna
WEnemies.push_back(wiazkowiec); WEnemies.push_back(wiazkowiec);
std::cout << "Spawned Wiazkowiec Enemy at X: " << spawnX << std::endl; std::cout << "Spawned Wiazkowiec Enemy at X: " << spawnX << std::endl;
WiazkowiecSpawnClock.restart(); WiazkowiecSpawnClock.restart();
} }
}
} }
void Plansza::update_hearts() { void Plansza::update_hearts() {
// usuwanie serduszek, które wyleciały poza ekran // usuwanie serduszek które wyleciały poza ekran
for (auto& heart : hearts) { for (auto& heart : hearts) {
if(heart.getStatus()) { if(heart.getStatus()) {
hearts.erase(hearts.begin()); hearts.erase(hearts.begin());

View File

@@ -1,7 +1,10 @@
#include <utility>
#include "../headers/Player.h" #include "../headers/Player.h"
#include <iostream> #include <iostream>
#include <SFML/Graphics/Font.hpp> #include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp> #include <SFML/Graphics/Text.hpp>
#include "../headers/Plansza.h" #include "../headers/Plansza.h"
@@ -21,14 +24,8 @@ Player* Player::getInstance(int x, int y, const sf::Texture& texture) {
} }
void Player::loadTexture() { void Player::loadTexture() {
try {
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");
} catch (std::exception &e) {
std::cerr << "Failed to load textures: " << e.what() << std::endl;
exit(-500);
}
damageDealClock.restart(); damageDealClock.restart();
originalColor = actorSprite.getColor(); originalColor = actorSprite.getColor();
} }

View File

@@ -2,7 +2,7 @@
void Rocket::update() { void Rocket::update() {
sprite.move(0.0f, speed); sprite.move(0.0f, speed);
position.y += static_cast<int>(speed); position.y += int(speed);
if(position.y < -100) { if(position.y < -100) {
outOfBounds = true; outOfBounds = true;
} }

View File

@@ -1,60 +1,56 @@
#include "../headers/Wiazkowiec.h" #include "../headers/Wiazkowiec.h"
#include "../headers/Plansza.h"
#include "../headers/RandomNumberGenerator.h"
#include <iostream> #include <iostream>
#include <random>
#include "../headers/Plansza.h"
Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture, sf::RenderWindow *window) : Actor(x, y, texture), Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture& texture) : Actor(x, y, texture), beam(0, 0, 50.f, 50.f, sf::Color::Red) {
beam(nullptr) actorSprite.setTexture(texture);
{
window_ptr = window;
try {
beamTexture.loadFromFile("../assets/img/wiazka/laser.png");
} catch (std::exception &e) {
std::cerr << "Failed to load textures: " << e.what() << std::endl;
exit(-500);
}
hp = 2; // 2 punkty życia hp = 2; // 2 punkty życia
firerate = 5000; // Strzela co 10 firerate = 5000; // Strzela co 10
moving_speed = 5.0f; // Prędkość moving_speed = 5.0f; // Prędkość
} }
void Wiazkowiec::setPlanszaHeight(int height, int width) { void Wiazkowiec::setPlanszaHeight(float height, float width) {
planszaHeight = height; planszaHeight = height;
planszaWidth = width; planszaWidth = width;
} }
void Wiazkowiec::spawnBeam() { void Wiazkowiec::spawnBeam() {
int beamX = actorSprite.getPosition().x; float beamX = position.x;
int beamY = actorSprite.getPosition().y; float beamY = position.y;
float beamWidth = 50.f;
float beamHeight = 0.f;
switch (direction) { switch (direction) {
case DirectionW::Up: { case DirectionW::Up:
beam = new Beam(beamX, beamY, beamTexture); beamHeight = position.y;
beam->setRotation(180); beamY -= beamHeight;
break; beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
} break;
case DirectionW::Down: { case DirectionW::Down:
beam = new Beam(beamX, beamY, beamTexture); beamHeight = planszaHeight - position.y;
break; beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
} break;
case DirectionW::Left: { case DirectionW::Left:
beam = new Beam(beamX, beamY, beamTexture); beamHeight = 50.f;
beam->setRotation(90); beamWidth = position.x;
break; beamX -= beamWidth;
} beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
case DirectionW::Right: { beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
beam = new Beam(beamX, beamY, beamTexture); break;
beam->setRotation(270); case DirectionW::Right:
break; beamHeight = 50.f;
} beamWidth = 800 - position.x;
default: beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
break; break;
} }
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
beam.setVisible(true);
shooting = true; shooting = true;
shootingClock.restart(); shootingClock.restart();
} }
@@ -63,35 +59,15 @@ void Wiazkowiec::spawnBeam() {
void Wiazkowiec::shoot() { void Wiazkowiec::shoot() {
if (!shooting) { if (!shooting) {
spawnBeam(); spawnBeam();
} std::cout << "Wiazkowiec shot a beam!" << std::endl;
switch (direction) {
case DirectionW::Up: {
std::cout << "Direction is up" << std::endl;
break;
}
case DirectionW::Down: {
std::cout << "Direction is down" << std::endl;
break;
}
case DirectionW::Left: {
std::cout << "Direction is left" << std::endl;
break;
}
case DirectionW::Right: {
std::cout << "Direction is right" << std::endl;
break;
}
default:
break;
} }
} }
void Wiazkowiec::setRandomDirection() { void Wiazkowiec::setRandomDirection() {
// Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right // Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right
int directionIndex = std::rand() % 4;
int whatNumber = rand() % 4; switch (directionIndex) {
switch (whatNumber) {
case 0: case 0:
direction = DirectionW::Up; direction = DirectionW::Up;
break; break;
@@ -104,12 +80,10 @@ void Wiazkowiec::setRandomDirection() {
case 3: case 3:
direction = DirectionW::Right; direction = DirectionW::Right;
break; break;
default:
break;
} }
} }
void Wiazkowiec::checkIfBeamShootOutOfBounds() { void Wiazkowiec::updateDirection() {
auto spriteBounds = actorSprite.getGlobalBounds(); // Pobierz rozmiar i pozycję sprite'a auto spriteBounds = actorSprite.getGlobalBounds(); // Pobierz rozmiar i pozycję sprite'a
// Kontrola górnej i dolnej krawędzi // Kontrola górnej i dolnej krawędzi
@@ -132,17 +106,17 @@ void Wiazkowiec::move(float deltaX, float deltaY) {
auto spriteBounds = actorSprite.getGlobalBounds(); // Rozmiar i pozycja sprite'a auto spriteBounds = actorSprite.getGlobalBounds(); // Rozmiar i pozycja sprite'a
// Zapobiegaj wyjściu poza poziome granice // Zapobiegaj wyjściu poza poziome granice
if (static_cast<float>(position.x) + deltaX < 0) { if (position.x + deltaX < 0) {
deltaX = static_cast<float>(-position.x); deltaX = -position.x;
} else if (static_cast<float>(position.x) + spriteBounds.width + deltaX > static_cast<float>(planszaWidth)) { } else if (position.x + spriteBounds.width + deltaX > planszaWidth) {
deltaX = static_cast<float>(planszaWidth) - (static_cast<float>(position.x) + spriteBounds.width); deltaX = planszaWidth - (position.x + spriteBounds.width);
} }
// Zapobiegaj wyjściu poza pionowe granice // Zapobiegaj wyjściu poza pionowe granice
if (static_cast<float>(position.y) + deltaY < 0) { if (position.y + deltaY < 0) {
deltaY = static_cast<float>(-position.y); deltaY = -position.y;
} else if (static_cast<float>(position.y) + spriteBounds.height + deltaY > static_cast<float>(planszaHeight)) { } else if (position.y + spriteBounds.height + deltaY > planszaHeight) {
deltaY = static_cast<float>(planszaHeight) - (static_cast<float>(position.y) + spriteBounds.height); deltaY = planszaHeight - (position.y + spriteBounds.height);
} }
actorSprite.move(deltaX, deltaY); actorSprite.move(deltaX, deltaY);
@@ -159,15 +133,12 @@ void Wiazkowiec::update() {
if (shooting) { if (shooting) {
// Kontrola zakończenia strzału // Kontrola zakończenia strzału
if (shootingClock.getElapsedTime().asSeconds() >= beamDuration) { if (shootingClock.getElapsedTime().asSeconds() >= beamDuration) {
beam.setVisible(false);
shooting = false; shooting = false;
delete beam;
beam = nullptr;
setRandomDirection(); // Zmień kierunek po strzale setRandomDirection(); // Zmień kierunek po strzale
} else {
window_ptr->draw(beam->getSprite());
} }
} else { } else {
checkIfBeamShootOutOfBounds(); updateDirection();
switch (direction) { switch (direction) {
case DirectionW::Up: case DirectionW::Up:
@@ -184,14 +155,20 @@ void Wiazkowiec::update() {
break; break;
} }
if (shootClock.getElapsedTime().asSeconds() >= 3.0f) { if (shootClock.getElapsedTime().asSeconds() >= 3.0f) { // Co 3 sekundy
// Co 3 sekundy
shoot(); shoot();
shootClock.restart(); shootClock.restart();
} }
} }
} }
// Ustawianie widoczności wiązki podczas renderowania
void Wiazkowiec::render(sf::RenderWindow& window) {
if (beam.isVisible()) {
beam.render(window);
}
}
bool Wiazkowiec::isAlive() const { bool Wiazkowiec::isAlive() const {
return alive; return alive;
} }
@@ -207,6 +184,6 @@ bool Wiazkowiec::isShooting() const {
return shooting; return shooting;
} }
Beam* Wiazkowiec::getBeam() const { const Beam& Wiazkowiec::getBeam() const {
return beam; return beam;
} }