This repository has been archived on 2025-06-06. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LotoStatek/sources/Boss.cpp

189 lines
5.4 KiB
C++

#include "../headers/Boss.h"
#include <random>
#include <iostream>
#include "../headers/RandomNumberGenerator.h"
Boss::Boss(int x, int y, const sf::Texture& bossTexture, const sf::Texture& bulletTexture, const sf::Texture BombaTexture, sf::RenderWindow* window)
: Actor(x, y, bossTexture), bulletTexture(bulletTexture), BombaTexture(BombaTexture), window(window) {
try {
beamTexture.loadFromFile("../assets/img/wiazka/laser.png");
} catch (std::exception& e) {
std::cerr << "Failed to load textures: " << e.what() << std::endl;
exit(-500);
}
hp = 100;
firerate = 2000;
movementSpeed = 2.0f;
}
void Boss::setPlanszaHeight(int height, int width) {
planszaHeight = height;
planszaWidth = width;
}
// TODO: Po mergowaniu dodać obsługę pocisku, przy pomocy nowego konstruktora Bullet
void Boss::shoot() {
if (shootClock.getElapsedTime().asMilliseconds() >= firerate) {
Bullet leftBullet(position.x - 20, position.y, bulletTexture);
leftBullet.setSpeed(5.0f);
Bullet centerBullet(position.x, position.y, bulletTexture);
centerBullet.setSpeed(5.0f);
Bullet rightBullet(position.x + 20, position.y, bulletTexture);
rightBullet.setSpeed(5.0f);
bullets.push_back(std::move(leftBullet));
bullets.push_back(std::move(centerBullet));
bullets.push_back(std::move(rightBullet));
std::cout << "Strzal lezy" << std::endl;
shootClock.restart();
}
}
void Boss::dropBomb() {
if (bombClock.getElapsedTime().asMilliseconds() >= 5000) {
Bullet Bomb(position.x, position.y, BombaTexture);
Bomb.setSpeed(0.5f);
bombs.emplace_back(std::move(Bomb)); // Można zmienić na bombę
std::cout << "Bombka lezy" << std::endl;
bombClock.restart();
}
}
void Boss::shootLaser() {
if (!laserBeam && laserClock.getElapsedTime().asSeconds() >= 5.0f) {
laserBeam = new Beam(position.x, position.y, beamTexture);
beamDurationClock.restart();
laserClock.restart();
isStationary = true;
}
if (laserBeam) {
window->draw(laserBeam->getSprite());
std::cout << "Laser beam shooted" << std::endl;
if (beamDurationClock.getElapsedTime().asSeconds() >= beamDuration) {
delete laserBeam;
laserBeam = nullptr;
isStationary = false;
}
}
}
void Boss::move(float deltaX, float deltaY) {
if (deltaX == 0 && deltaY == 0) {
std::cerr << "Boss stopped: deltaX and deltaY are both 0" << std::endl;
}
actorSprite.move(deltaX, deltaY);
position.x += static_cast<int>(deltaX);
position.y += static_cast<int>(deltaY);
handleBounds();
}
void Boss::update() {
std::cout << "Current movementSpeed: " << movementSpeed << std::endl;
std::cout << "Boss position: (" << position.x << ", " << position.y << "), Direction: " << static_cast<int>(direction) << std::endl;
if (!isStationary) {
if (directionClock.getElapsedTime().asSeconds() >= 3.0f) {
setRandomDirection();
directionClock.restart();
}
switch (direction) {
case BossDirection::Up: moveUp(); break;
case BossDirection::Down: moveDown(); break;
case BossDirection::Left: moveLeft(); break;
case BossDirection::Right: moveRight(); break;
}
}
shoot();
dropBomb();
shootLaser();
// Aktualizacja pocisków
for (auto it = bullets.begin(); it != bullets.end();) {
it->update();
window->draw(it->getSprite());
if (it->isOutOfBounds()) {
it = bullets.erase(it);
} else {
++it;
}
}
// Aktualizacja bomb
for (auto it = bombs.begin(); it != bombs.end();) {
it->update();
window->draw(it->getSprite());
if (it->isOutOfBounds()) {
it = bombs.erase(it);
} else {
++it;
}
}
}
void Boss::moveLeft() {
move(-movementSpeed, 0.0f);
}
void Boss::moveRight() {
move(movementSpeed, 0.0f);
}
void Boss::moveUp() {
move(0.0f, -movementSpeed);
}
void Boss::moveDown() {
move(0.0f, movementSpeed);
}
void Boss::takeDamage() {
if (--hp <= 0) {
hp = 0;
std::cout << "HP bossa" << hp << std::endl;
}
}
bool Boss::isAlive() const {
return hp > 0;
}
void Boss::setRandomDirection() {
BossDirection previousDirection = direction;
do {
int randomValue = RandomNumberGenerator::getRandomNumber(0, 3);
direction = static_cast<BossDirection>(randomValue);
} while (
(direction == BossDirection::Left && position.x <= 0) ||
(direction == BossDirection::Right && position.x >= 600) ||
(direction == BossDirection::Up && position.y <= 0) ||
(direction == BossDirection::Down && position.y >= 800)
);
if (previousDirection == direction) {
std::cerr << "Boss kept the same direction: " << static_cast<int>(direction) << std::endl;
}
}
void Boss::handleBounds() {
if (position.x < 0) {
direction = BossDirection::Right;
} else if (position.x > 600 - actorSprite.getGlobalBounds().width) {
direction = BossDirection::Left;
}
if (position.y < 0) {
direction = BossDirection::Down;
} else if (position.y > 800 - actorSprite.getGlobalBounds().height) {
direction = BossDirection::Up;
}
}