#include "../headers/Wiazkowiec.h" #include #include "../headers/Bullet.h" #include Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture& texture) : Actor(x, y, texture), beam(0, 0, 50.f, 50.f, sf::Color::Red) { actorSprite.setTexture(texture); hp = 2; // 2 punkty życia firerate = 5000; // Strzela co 10 moving_speed = 1.0f; // Prędkość // BombaTexture.loadFromFile("../assets/img/bullets/bomba.png"); } void Wiazkowiec::setPlanszaHeight(float height) { planszaHeight = height; } void Wiazkowiec::setMapBounds(float width, float height) { mapWidth = width; mapHeight = height; } void Wiazkowiec::spawnBeam() { float beamX = position.x; float beamY = position.y; float beamWidth = 50.f; float beamHeight = 0.f; switch (direction) { case DirectionW::Up: beamHeight = position.y; beamY -= beamHeight; beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); break; case DirectionW::Down: beamHeight = planszaHeight - position.y; // Dynamiczna wysokość okna beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); break; case DirectionW::Left: beamHeight = 50.f; // Stała wysokość beamWidth = position.x; beamX -= beamWidth; // Wiązka w lewo od pozycji statku beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); break; case DirectionW::Right: beamHeight = 50.f; // Stała wysokość beamWidth = 800 - position.x; beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); break; } beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); beam.setVisible(true); shooting = true; shootingClock.restart(); // Reset zegara } // Strzał wiązki void Wiazkowiec::shoot() { if (!shooting) { spawnBeam(); std::cout << "Wiazkowiec shot a beam!" << std::endl; } } void Wiazkowiec::setRandomDirection() { // Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right int directionIndex = std::rand() % 4; switch (directionIndex) { case 0: direction = DirectionW::Up; break; case 1: direction = DirectionW::Down; break; case 2: direction = DirectionW::Left; break; case 3: direction = DirectionW::Right; break; } } void Wiazkowiec::updateDirection() { auto spriteBounds = actorSprite.getGlobalBounds(); // Kontrola dolnej i górnej krawędzi if (position.y <= 0) { direction = DirectionW::Down; } else if (position.y + spriteBounds.height >= mapHeight) { direction = DirectionW::Up; } // Kontrola lewej i prawej krawędzi if (position.x <= 0) { direction = DirectionW::Right; } else if (position.x + spriteBounds.width >= mapWidth) { direction = DirectionW::Left; } } void Wiazkowiec::move(float deltaX, float deltaY) { actorSprite.move(deltaX, deltaY); position.x += static_cast(deltaX); position.y += static_cast(deltaY); } void Wiazkowiec::moveLeft() { move(-moving_speed, 0.0f); } void Wiazkowiec::moveRight() { move(moving_speed, 0.0f); } void Wiazkowiec::moveUp() { move(0.0f, -moving_speed); } void Wiazkowiec::moveDown() { move(0.0f, moving_speed); } void Wiazkowiec::update() { if (shooting) { // Kontrola zakończenia strzału if (shootingClock.getElapsedTime().asSeconds() >= beamDuration) { beam.setVisible(false); shooting = false; setRandomDirection(); // Zmień kierunek po strzale } } else { updateDirection(); switch (direction) { case DirectionW::Up: if (position.y > 0) moveUp(); break; case DirectionW::Down: if (position.y + actorSprite.getGlobalBounds().height < mapHeight) moveDown(); break; case DirectionW::Left: if (position.x > 0) moveLeft(); break; case DirectionW::Right: if (position.x + actorSprite.getGlobalBounds().width < mapWidth) moveRight(); break; } if (shootClock.getElapsedTime().asSeconds() >= 3.0f) { // Co 3 sekundy shoot(); 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 { return alive; } void Wiazkowiec::takeDamage() { if (--hp <= 0) { alive = false; } } bool Wiazkowiec::isShooting() const { return shooting; } const Beam& Wiazkowiec::getBeam() const { return beam; }