Bomber i wiazkowiec nie wylaza poza ekran
This commit is contained in:
@@ -31,8 +31,11 @@ public:
|
|||||||
bool isAlive() const;
|
bool isAlive() const;
|
||||||
void takeDamage();
|
void takeDamage();
|
||||||
void updateDirection();
|
void updateDirection();
|
||||||
|
void setPlanszaHeight(float height, float width);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
float planszaHeight = 800.f;
|
||||||
|
float planszaWidth = 600.f;
|
||||||
sf::Clock shootClock;
|
sf::Clock shootClock;
|
||||||
sf::Texture BombaTexture;
|
sf::Texture BombaTexture;
|
||||||
float movementSpeed = 2.0f;
|
float movementSpeed = 2.0f;
|
||||||
|
|||||||
@@ -32,12 +32,13 @@ public:
|
|||||||
void updateDirection();
|
void updateDirection();
|
||||||
bool isShooting() const;
|
bool isShooting() const;
|
||||||
const Beam& getBeam() const;
|
const Beam& getBeam() const;
|
||||||
void setPlanszaHeight(float height);
|
void setPlanszaHeight(float height, float width);
|
||||||
void setMapBounds(float width, float height);
|
void setMapBounds(float width, float height);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float planszaHeight = 800.f;
|
float planszaHeight = 800.f;
|
||||||
|
float planszaWidth = 600.f;
|
||||||
sf::Clock shootClock;
|
sf::Clock shootClock;
|
||||||
sf::Texture WiazkaTexture;
|
sf::Texture WiazkaTexture;
|
||||||
float movementSpeed = 2.0f;
|
float movementSpeed = 2.0f;
|
||||||
|
|||||||
@@ -7,11 +7,14 @@ Bomber::Bomber(int x, int y, const sf::Texture& texture, const sf::Texture& bull
|
|||||||
BombaTexture = bulletTexture;
|
BombaTexture = bulletTexture;
|
||||||
hp = 2; // 2 punkty życia
|
hp = 2; // 2 punkty życia
|
||||||
firerate = 10000; // Strzela co 10
|
firerate = 10000; // Strzela co 10
|
||||||
moving_speed = 1.0f; // Prędkość
|
moving_speed = 10.0f; // Prędkość
|
||||||
// BombaTexture.loadFromFile("../assets/img/bullets/bomba.png");
|
}
|
||||||
|
|
||||||
|
void Bomber::setPlanszaHeight(float height, float width) {
|
||||||
|
planszaHeight = height;
|
||||||
|
planszaWidth = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Losuje losowy kierunek dla Bombera
|
|
||||||
void Bomber::setRandomDirection() {
|
void Bomber::setRandomDirection() {
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
std::mt19937 gen(rd());
|
std::mt19937 gen(rd());
|
||||||
@@ -47,22 +50,39 @@ void Bomber::shoot() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Bomber::updateDirection() {
|
void Bomber::updateDirection() {
|
||||||
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
|
auto spriteBounds = actorSprite.getGlobalBounds(); // Pobierz rozmiar i pozycję sprite'a
|
||||||
|
|
||||||
|
// Kontrola górnej i dolnej krawędzi (wysokości planszy)
|
||||||
if (position.y <= 0) {
|
if (position.y <= 0) {
|
||||||
direction = DirectionB::Down;
|
direction = DirectionB::Down;
|
||||||
} else if (position.y >= 800) {
|
} else if (position.y + spriteBounds.height >= planszaHeight) {
|
||||||
direction = DirectionB::Up;
|
direction = DirectionB::Up;
|
||||||
}
|
}
|
||||||
|
|
||||||
// logika dla kierunku lewo/prawo
|
// Kontrola lewej i prawej krawędzi (szerokości planszy)
|
||||||
if (position.x <= 0) {
|
if (position.x <= 0) {
|
||||||
direction = DirectionB::Right;
|
direction = DirectionB::Right;
|
||||||
} else if (position.x >= 1200) {
|
} else if (position.x + spriteBounds.width >= planszaWidth) {
|
||||||
direction = DirectionB::Left;
|
direction = DirectionB::Left;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bomber::move(float deltaX, float deltaY) {
|
void Bomber::move(float deltaX, float deltaY) {
|
||||||
|
auto spriteBounds = actorSprite.getGlobalBounds(); // Rozmiar i pozycja sprite'a
|
||||||
|
|
||||||
|
// Zapobiegaj wyjściu poza poziome granice
|
||||||
|
if (position.x + deltaX < 0) {
|
||||||
|
deltaX = -position.x;
|
||||||
|
} else if (position.x + spriteBounds.width + deltaX > planszaWidth) {
|
||||||
|
deltaX = planszaWidth - (position.x + spriteBounds.width);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zapobiegaj wyjściu poza pionowe granice
|
||||||
|
if (position.y + deltaY < 0) {
|
||||||
|
deltaY = -position.y;
|
||||||
|
} else if (position.y + spriteBounds.height + deltaY > planszaHeight) {
|
||||||
|
deltaY = planszaHeight - (position.y + spriteBounds.height);
|
||||||
|
}
|
||||||
actorSprite.move(deltaX, deltaY);
|
actorSprite.move(deltaX, deltaY);
|
||||||
position.x += static_cast<int>(deltaX);
|
position.x += static_cast<int>(deltaX);
|
||||||
position.y += static_cast<int>(deltaY);
|
position.y += static_cast<int>(deltaY);
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ Kamikadze::Kamikadze(int x, int y, const sf::Texture& texture) : Actor(x, y, tex
|
|||||||
|
|
||||||
void Kamikadze::shoot(){}
|
void Kamikadze::shoot(){}
|
||||||
|
|
||||||
// Losuje losowy kierunek dla Bombera
|
|
||||||
void Kamikadze::setRandomDirection() {
|
void Kamikadze::setRandomDirection() {
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
std::mt19937 gen(rd());
|
std::mt19937 gen(rd());
|
||||||
@@ -56,31 +55,28 @@ void Kamikadze::updateDirection() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Kamikadze::followPlayer(const sf::Vector2f& playerPosition) {
|
void Kamikadze::followPlayer(const sf::Vector2f& playerPosition) {
|
||||||
// Oblicz różnicę w pozycjach
|
|
||||||
float diffX = playerPosition.x - position.x;
|
float diffX = playerPosition.x - position.x;
|
||||||
float diffY = playerPosition.y - position.y;
|
float diffY = playerPosition.y - position.y;
|
||||||
|
|
||||||
// Normalizacja wektora (skrócenie kierunku do jednostkowego)
|
|
||||||
float magnitude = std::sqrt(diffX * diffX + diffY * diffY);
|
float magnitude = std::sqrt(diffX * diffX + diffY * diffY);
|
||||||
if (magnitude != 0) {
|
if (magnitude != 0) {
|
||||||
diffX /= magnitude;
|
diffX /= magnitude;
|
||||||
diffY /= magnitude;
|
diffY /= magnitude;
|
||||||
|
|
||||||
// Aktualizacja pozycji Kamikadze w kierunku gracza z uwzględnieniem prędkości
|
// Aktualizacja pozycji Kamikadze w kierunku gracza
|
||||||
position.x += diffX * movementSpeed;
|
position.x += diffX * movementSpeed;
|
||||||
position.y += diffY * movementSpeed;
|
position.y += diffY * movementSpeed;
|
||||||
} else {
|
} else { //zatrzymanie kamikadze
|
||||||
// Jeśli Kamikadze jest dokładnie na pozycji gracza, zatrzymaj jego ruch
|
|
||||||
movementSpeed = 0.0f;
|
movementSpeed = 0.0f;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Kamikadze::explode(const sf::Vector2f& playerPosition, bool& playerHit) {
|
void Kamikadze::explode(const sf::Vector2f& playerPosition, bool& playerHit) {
|
||||||
if (!exploding) {
|
if (!exploding) {
|
||||||
// Rozpocznij eksplozję
|
// Rozpocznij kamikadze
|
||||||
exploding = true;
|
exploding = true;
|
||||||
explosionClock.restart();
|
explosionClock.restart();
|
||||||
movementSpeed = 0.0f; // Zatrzymaj Kamikadze
|
movementSpeed = 0.0f;
|
||||||
|
|
||||||
std::cout << "Kamikadze exploding!" << std::endl;
|
std::cout << "Kamikadze exploding!" << std::endl;
|
||||||
std::cout << "Kamikadze position: (" << position.x << ", " << position.y << ")" << std::endl;
|
std::cout << "Kamikadze position: (" << position.x << ", " << position.y << ")" << std::endl;
|
||||||
@@ -121,9 +117,9 @@ void Kamikadze::moveDown() { move(0.0f, moving_speed); }
|
|||||||
|
|
||||||
void Kamikadze::update(const sf::Vector2f& playerPosition) {
|
void Kamikadze::update(const sf::Vector2f& playerPosition) {
|
||||||
if (alive && !exploding) {
|
if (alive && !exploding) {
|
||||||
// Podążaj za graczem, dopóki Kamikadze jest żywy i nie eksploduje
|
// Podążanie za graczem, dopóki Kamikadze jest żywy
|
||||||
followPlayer(playerPosition);
|
followPlayer(playerPosition);
|
||||||
actorSprite.setPosition(position.x, position.y); // Aktualizuj sprite
|
actorSprite.setPosition(position.x, position.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -96,8 +96,8 @@ void Plansza::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// generowanie nowego meteoru
|
// generowanie nowego meteoru
|
||||||
//spawn_meteor();
|
|
||||||
ship.update();
|
ship.update();
|
||||||
|
spawn_meteor();
|
||||||
spawn_enemy();
|
spawn_enemy();
|
||||||
spawn_advanced_enemy();
|
spawn_advanced_enemy();
|
||||||
spawn_wiazkowiec();
|
spawn_wiazkowiec();
|
||||||
@@ -254,7 +254,7 @@ void Plansza::update() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto it = BEnemies.begin(); it != BEnemies.end();) {
|
for (auto it = BEnemies.begin(); it != BEnemies.end();) {
|
||||||
it->update(); // Ruch b
|
it->update(); // Ruch bombera
|
||||||
it->shoot(); // Strzał przeciwnika
|
it->shoot(); // Strzał przeciwnika
|
||||||
|
|
||||||
window->draw(it->getSprite()); // Rysowanie na ekranie
|
window->draw(it->getSprite()); // Rysowanie na ekranie
|
||||||
@@ -272,7 +272,6 @@ void Plansza::update() {
|
|||||||
sf::Vector2f playerPosition = ship.getSprite().getPosition(); // Aktualna pozycja gracza
|
sf::Vector2f playerPosition = ship.getSprite().getPosition(); // Aktualna pozycja gracza
|
||||||
bool playerHit = false;
|
bool playerHit = false;
|
||||||
|
|
||||||
// Aktualizacja pozycji Kamikadze
|
|
||||||
it->update(playerPosition);
|
it->update(playerPosition);
|
||||||
|
|
||||||
// Wybuch, gdy Kamikadze dotknie gracza
|
// Wybuch, gdy Kamikadze dotknie gracza
|
||||||
@@ -289,7 +288,7 @@ void Plansza::update() {
|
|||||||
it->explode(playerPosition, playerHit); // Eksplozja trwa
|
it->explode(playerPosition, playerHit); // Eksplozja trwa
|
||||||
}
|
}
|
||||||
|
|
||||||
// Usunięcie martwego Kamikadze z listy
|
// Usunięcie martwego Kamikadze
|
||||||
if (it->isAlive()) {
|
if (it->isAlive()) {
|
||||||
window->draw(it->getSprite());
|
window->draw(it->getSprite());
|
||||||
++it;
|
++it;
|
||||||
@@ -678,9 +677,11 @@ void Plansza::spawn_advanced_enemy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Plansza::spawn_bomber() {
|
void Plansza::spawn_bomber() {
|
||||||
if (BomberSpawnClock.getElapsedTime().asSeconds() >= 120) { // Spawn co 10 sekund
|
if (BomberSpawnClock.getElapsedTime().asSeconds() >= 10) { // Spawn co 10 sekund
|
||||||
int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50);
|
int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50);
|
||||||
BEnemies.emplace_back(spawnX, -50, BomberEnemyTexture, BombaTexture);
|
Bomber bomber(spawnX, -50, BomberEnemyTexture, BombaTexture);
|
||||||
|
bomber.setPlanszaHeight(size.height, size.width); // Przekazanie wysokości i szerokości okna
|
||||||
|
BEnemies.push_back(bomber);
|
||||||
std::cout << "Spawned Bomber Enemy at X: " << spawnX << std::endl;
|
std::cout << "Spawned Bomber Enemy at X: " << spawnX << std::endl;
|
||||||
BomberSpawnClock.restart();
|
BomberSpawnClock.restart();
|
||||||
}
|
}
|
||||||
@@ -696,10 +697,10 @@ void Plansza::spawn_kamikadze() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Plansza::spawn_wiazkowiec() {
|
void Plansza::spawn_wiazkowiec() {
|
||||||
if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 10) { // Spawn co 10 sekund
|
if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 110) { // Spawn co 10 sekund
|
||||||
int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50);
|
int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50);
|
||||||
Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture);
|
Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture);
|
||||||
wiazkowiec.setPlanszaHeight(size.height); // Przekazanie wysokości 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();
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture& texture) : Actor(x, y, t
|
|||||||
actorSprite.setTexture(texture);
|
actorSprite.setTexture(texture);
|
||||||
hp = 2; // 2 punkty życia
|
hp = 2; // 2 punkty życia
|
||||||
firerate = 5000; // Strzela co 10
|
firerate = 5000; // Strzela co 10
|
||||||
moving_speed = 1.0f; // Prędkość
|
moving_speed = 5.0f; // Prędkość
|
||||||
// BombaTexture.loadFromFile("../assets/img/bullets/bomba.png");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Wiazkowiec::setPlanszaHeight(float height) {
|
void Wiazkowiec::setPlanszaHeight(float height, float width) {
|
||||||
planszaHeight = height;
|
planszaHeight = height;
|
||||||
|
planszaWidth = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Wiazkowiec::spawnBeam() {
|
void Wiazkowiec::spawnBeam() {
|
||||||
@@ -30,18 +30,18 @@ void Wiazkowiec::spawnBeam() {
|
|||||||
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
|
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
|
||||||
break;
|
break;
|
||||||
case DirectionW::Down:
|
case DirectionW::Down:
|
||||||
beamHeight = planszaHeight - position.y; // Dynamiczna wysokość okna
|
beamHeight = planszaHeight - position.y;
|
||||||
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
|
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
|
||||||
break;
|
break;
|
||||||
case DirectionW::Left:
|
case DirectionW::Left:
|
||||||
beamHeight = 50.f; // Stała wysokość
|
beamHeight = 50.f;
|
||||||
beamWidth = position.x;
|
beamWidth = position.x;
|
||||||
beamX -= beamWidth; // Wiązka w lewo od pozycji statku
|
beamX -= beamWidth;
|
||||||
beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
|
beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
|
||||||
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
|
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
|
||||||
break;
|
break;
|
||||||
case DirectionW::Right:
|
case DirectionW::Right:
|
||||||
beamHeight = 50.f; // Stała wysokość
|
beamHeight = 50.f;
|
||||||
beamWidth = 800 - position.x;
|
beamWidth = 800 - position.x;
|
||||||
beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
|
beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
|
||||||
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
|
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
|
||||||
@@ -52,7 +52,7 @@ void Wiazkowiec::spawnBeam() {
|
|||||||
beam.setVisible(true);
|
beam.setVisible(true);
|
||||||
|
|
||||||
shooting = true;
|
shooting = true;
|
||||||
shootingClock.restart(); // Reset zegara
|
shootingClock.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Strzał wiązki
|
// Strzał wiązki
|
||||||
@@ -84,23 +84,41 @@ void Wiazkowiec::setRandomDirection() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Wiazkowiec::updateDirection() {
|
void Wiazkowiec::updateDirection() {
|
||||||
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
|
auto spriteBounds = actorSprite.getGlobalBounds(); // Pobierz rozmiar i pozycję sprite'a
|
||||||
|
|
||||||
|
// Kontrola górnej i dolnej krawędzi
|
||||||
if (position.y <= 0) {
|
if (position.y <= 0) {
|
||||||
direction = DirectionW::Down;
|
direction = DirectionW::Down;
|
||||||
} else if (position.y >= 800) {
|
} else if (position.y + spriteBounds.height >= planszaHeight) {
|
||||||
direction = DirectionW::Up;
|
direction = DirectionW::Up;
|
||||||
}
|
}
|
||||||
|
|
||||||
// logika dla kierunku lewo/prawo
|
// Kontrola lewej i prawej krawędzi
|
||||||
if (position.x <= 0) {
|
if (position.x <= 0) {
|
||||||
direction = DirectionW::Right;
|
direction = DirectionW::Right;
|
||||||
} else if (position.x >= 1200) {
|
} else if (position.x + spriteBounds.width >= planszaWidth) {
|
||||||
direction = DirectionW::Left;
|
direction = DirectionW::Left;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Wiazkowiec::move(float deltaX, float deltaY) {
|
void Wiazkowiec::move(float deltaX, float deltaY) {
|
||||||
|
auto spriteBounds = actorSprite.getGlobalBounds(); // Rozmiar i pozycja sprite'a
|
||||||
|
|
||||||
|
// Zapobiegaj wyjściu poza poziome granice
|
||||||
|
if (position.x + deltaX < 0) {
|
||||||
|
deltaX = -position.x;
|
||||||
|
} else if (position.x + spriteBounds.width + deltaX > planszaWidth) {
|
||||||
|
deltaX = planszaWidth - (position.x + spriteBounds.width);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Zapobiegaj wyjściu poza pionowe granice
|
||||||
|
if (position.y + deltaY < 0) {
|
||||||
|
deltaY = -position.y;
|
||||||
|
} else if (position.y + spriteBounds.height + deltaY > planszaHeight) {
|
||||||
|
deltaY = planszaHeight - (position.y + spriteBounds.height);
|
||||||
|
}
|
||||||
|
|
||||||
actorSprite.move(deltaX, deltaY);
|
actorSprite.move(deltaX, deltaY);
|
||||||
position.x += static_cast<int>(deltaX);
|
position.x += static_cast<int>(deltaX);
|
||||||
position.y += static_cast<int>(deltaY);
|
position.y += static_cast<int>(deltaY);
|
||||||
|
|||||||
Reference in New Issue
Block a user