#include #include #include "../headers/Kamikadze.h" #include "../headers/RandomNumberGenerator.h" #include "../headers/Plansza.h" Kamikadze::Kamikadze(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) { hp = 3; // 3 punkty życia moving_speed = 5.0f; // Prędkość } void Kamikadze::shoot(){} void Kamikadze::setRandomDirection() { int randomDirection = RandomNumberGenerator::getRandomNumber(0,3); // Zapobieganie wyjscia poza ekran switch (randomDirection) { case 0: if (position.y > 0) direction = DirectionK::Up; break; case 1: if (position.y < 800) direction = DirectionK::Down; break; case 2: if (position.x > 0) direction = DirectionK::Left; break; case 3: if (position.x < 1200) direction = DirectionK::Right; break; } } void Kamikadze::updateDirection() { // Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi if (position.y <= 0) { direction = DirectionK::Down; } else if (position.y >= 800) { direction = DirectionK::Up; } // logika dla kierunku lewo/prawo if (position.x <= 0) { direction = DirectionK::Right; } else if (position.x >= 1200) { direction = DirectionK::Left; } } void Kamikadze::followPlayer(const sf::Vector2f& playerPosition) { float diffX = playerPosition.x - position.x; float diffY = playerPosition.y - position.y; float magnitude = std::sqrt(diffX * diffX + diffY * diffY); if (magnitude != 0) { diffX /= magnitude; diffY /= magnitude; // Aktualizacja pozycji Kamikadze w kierunku gracza position.x += diffX * movementSpeed; position.y += diffY * movementSpeed; } else { //zatrzymanie kamikadze movementSpeed = 0.0f; } } void Kamikadze::explode(const sf::Vector2f& playerPosition, bool& playerHit) { if (!exploding) { // Rozpocznij kamikadze exploding = true; explosionClock.restart(); movementSpeed = 0.0f; std::cout << "Kamikadze exploding!" << std::endl; std::cout << "Kamikadze position: (" << position.x << ", " << position.y << ")" << std::endl; std::cout << "Player position: (" << playerPosition.x << ", " << playerPosition.y << ")" << std::endl; // Zakres, w jakim gracz może zostać trafiony sf::Vector2f diff = playerPosition - sf::Vector2f(static_cast(position.x), static_cast(position.y)); float distance = std::sqrt(diff.x * diff.x + diff.y * diff.y); std::cout << "Distance to player: " << distance << std::endl; // Trafienie gracza, jeśli jest w obszarze eksplozji const float explosionRange = 100.0f; if (distance <= explosionRange) { playerHit = true; std::cout << "Player is within explosion range! Player hit!" << std::endl; } } // Sprawdzanie czasu wybuchu if (exploding && explosionClock.getElapsedTime().asSeconds() >= 0.5f) { alive = false; // Kamikadze ulega zniszczeniu po eksplozji std::cout << "Kamikadze destroyed after explosion!" << std::endl; } } void Kamikadze::move(float deltaX, float deltaY) { actorSprite.move(deltaX, deltaY); position.x += static_cast(deltaX); position.y += static_cast(deltaY); } void Kamikadze::moveLeft() { move(-moving_speed, 0.0f); } void Kamikadze::moveRight() { move(moving_speed, 0.0f); } void Kamikadze::moveUp() { move(0.0f, -moving_speed); } void Kamikadze::moveDown() { move(0.0f, moving_speed); } void Kamikadze::update(const sf::Vector2f& playerPosition) { if (alive && !exploding) { // Podążanie za graczem, dopóki Kamikadze jest żywy followPlayer(playerPosition); actorSprite.setPosition(position.x, position.y); } } bool Kamikadze::isExploding() const { return exploding; } bool Kamikadze::isAlive() const { return alive; } void Kamikadze::takeDamage() { if (--hp <= 0) { alive = false; Plansza::score += 20; Plansza::ultimateCounter += 20; } }