#include "../headers/Bomber.h" #include "../headers/Bullet.h" #include "../headers/Plansza.h" #include Bomber::Bomber(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture) : Actor(x, y, texture) { BombaTexture = bulletTexture; hp = 2; // 2 punkty życia firerate = 6000; // Strzela co 6 moving_speed = 8.0f; // Prędkość } void Bomber::setPlanszaHeight(float height, float width) { planszaHeight = height; planszaWidth = width; } void Bomber::setRandomDirection() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution dist(0, 3); int randomDirection = dist(gen); // Zapobieganie wyjscia poza ekran switch (randomDirection) { case 0: if (position.y > 0) direction = DirectionB::Up; break; case 1: if (position.y < 600) direction = DirectionB::Down; break; case 2: if (position.x > 0) direction = DirectionB::Left; break; case 3: if (position.x < 800) direction = DirectionB::Right; break; } } void Bomber::shoot() { if (shootClock.getElapsedTime().asMilliseconds() >= firerate) { Bullet Bbullet(position.x, position.y + actorSprite.getGlobalBounds().height / 2, BombaTexture); Bbullet.setSpeed(0.1f); // Prędkość w dół bullets.emplace_back(std::move(Bbullet)); shootClock.restart(); setRandomDirection(); } } void Bomber::updateDirection() { auto spriteBounds = actorSprite.getGlobalBounds(); // Pobierz rozmiar i pozycję sprite'a // Kontrola górnej i dolnej krawędzi (wysokości planszy) if (position.y <= 0) { direction = DirectionB::Down; } else if (position.y + spriteBounds.height >= planszaHeight) { direction = DirectionB::Up; } // Kontrola lewej i prawej krawędzi (szerokości planszy) if (position.x <= 0) { direction = DirectionB::Right; } else if (position.x + spriteBounds.width >= planszaWidth) { direction = DirectionB::Left; } } 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); position.x += static_cast(deltaX); position.y += static_cast(deltaY); } void Bomber::moveLeft() { move(-moving_speed, 0.0f); } void Bomber::moveRight() { move(moving_speed, 0.0f); } void Bomber::moveUp() { move(0.0f, -moving_speed); } void Bomber::moveDown() { move(0.0f, moving_speed); } void Bomber::update() { // Sprawdzamy, czy przeciwnik dotarł do krawędzi i zmieniamy kierunek updateDirection(); switch (direction) { case DirectionB::Up: moveUp(); break; case DirectionB::Down: moveDown(); break; case DirectionB::Left: moveLeft(); break; case DirectionB::Right: moveRight(); break; } } bool Bomber::isAlive() const { return alive; } void Bomber::takeDamage() { if (--hp <= 0) { alive = false; Plansza::score += 15; Plansza::ultimateCounter += 15; } }