Kamikadze wybucha nawet bez gracza
This commit is contained in:
@@ -26,6 +26,7 @@ public:
|
|||||||
void update(const sf::Vector2f& playerPosition);
|
void update(const sf::Vector2f& playerPosition);
|
||||||
|
|
||||||
bool isAlive() const;
|
bool isAlive() const;
|
||||||
|
bool isExploding() const;
|
||||||
void takeDamage();
|
void takeDamage();
|
||||||
void updateDirection();
|
void updateDirection();
|
||||||
|
|
||||||
|
|||||||
@@ -74,24 +74,25 @@ void Kamikadze::followPlayer(const sf::Vector2f& playerPosition) {
|
|||||||
|
|
||||||
void Kamikadze::explode(const sf::Vector2f& playerPosition, bool& playerHit) {
|
void Kamikadze::explode(const sf::Vector2f& playerPosition, bool& playerHit) {
|
||||||
if (!exploding) {
|
if (!exploding) {
|
||||||
// Kamikadze rozpoczyna eksplozję
|
// Rozpocznij eksplozję
|
||||||
exploding = true;
|
exploding = true;
|
||||||
explosionClock.restart();
|
explosionClock.restart();
|
||||||
movementSpeed = 0.0f; // Zatrzymaj ruch
|
movementSpeed = 0.0f; // Zatrzymaj Kamikadze
|
||||||
|
|
||||||
// Sprawdź, czy gracz jest w obszarze eksplozji
|
// Zakres, w jakim gracz może zostać trafiony
|
||||||
const float explosionRange = 50.0f; // Zakładając promień wybuchu
|
const float explosionRange = 50.0f;
|
||||||
sf::Vector2f diff = playerPosition - sf::Vector2f(static_cast<float>(position.x), static_cast<float>(position.y));
|
sf::Vector2f diff = playerPosition - sf::Vector2f(static_cast<float>(position.x), static_cast<float>(position.y));
|
||||||
float distance = std::sqrt(diff.x * diff.x + diff.y * diff.y);
|
float distance = std::sqrt(diff.x * diff.x + diff.y * diff.y);
|
||||||
|
|
||||||
|
// Trafienie gracza, jeśli jest w obszarze eksplozji
|
||||||
if (distance <= explosionRange) {
|
if (distance <= explosionRange) {
|
||||||
playerHit = true; // Gracz został trafiony
|
playerHit = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Po zakończeniu eksplozji, Kamikadze umiera
|
// Sprawdzanie czasu wybuchu
|
||||||
if (exploding && explosionClock.getElapsedTime().asSeconds() >= 0.5f) { // 0.5 sekundy
|
if (exploding && explosionClock.getElapsedTime().asSeconds() >= 0.5f) {
|
||||||
alive = false;
|
alive = false; // Kamikadze ulega zniszczeniu po eksplozji
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,6 +115,9 @@ void Kamikadze::update(const sf::Vector2f& playerPosition) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Kamikadze::isExploding() const {
|
||||||
|
return exploding;
|
||||||
|
}
|
||||||
|
|
||||||
bool Kamikadze::isAlive() const {
|
bool Kamikadze::isAlive() const {
|
||||||
return alive;
|
return alive;
|
||||||
|
|||||||
@@ -264,29 +264,31 @@ void Plansza::update() {
|
|||||||
|
|
||||||
|
|
||||||
for (auto it = KEnemies.begin(); it != KEnemies.end();) {
|
for (auto it = KEnemies.begin(); it != KEnemies.end();) {
|
||||||
sf::Vector2f playerPosition = ship.getSprite().getPosition(); // Pozycja gracza
|
sf::Vector2f playerPosition = ship.getSprite().getPosition(); // Aktualna pozycja gracza
|
||||||
bool playerHit = false;
|
bool playerHit = false;
|
||||||
|
|
||||||
// Aktualizacja ruchu Kamikadze
|
// Aktualizacja pozycji Kamikadze
|
||||||
|
|
||||||
it->update(playerPosition);
|
it->update(playerPosition);
|
||||||
|
|
||||||
// Sprawdź kolizję z graczem
|
// Wybuch, gdy Kamikadze dotknie gracza
|
||||||
if (ship.getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) {
|
if (ship.getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) {
|
||||||
it->explode(playerPosition, playerHit);
|
it->explode(playerPosition, playerHit);
|
||||||
|
|
||||||
if (playerHit) {
|
if (playerHit) {
|
||||||
ship.takeDamage(); // Gracz traci punkt życia
|
ship.takeDamage(); // Gracz otrzymuje obrażenia
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rysuj Kamikadze na ekranie
|
// Kamikadze eksploduje po aktywacji niezależnie od obecności gracza
|
||||||
|
if (it->isExploding()) {
|
||||||
|
it->explode(playerPosition, playerHit); // Eksplozja trwa
|
||||||
|
}
|
||||||
|
|
||||||
|
// Usunięcie martwego Kamikadze z listy
|
||||||
if (it->isAlive()) {
|
if (it->isAlive()) {
|
||||||
window->draw(it->getSprite());
|
window->draw(it->getSprite());
|
||||||
++it;
|
++it;
|
||||||
} else {
|
} else {
|
||||||
// Usuń, jeśli Kamikadze już nie żyje
|
std::cout << "Kamikadze exploded!" << std::endl;
|
||||||
std::cout << "Kamikadze has exploded!" << std::endl;
|
|
||||||
it = KEnemies.erase(it);
|
it = KEnemies.erase(it);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user