Kolizja wiazki dodana

This commit is contained in:
2025-01-14 13:52:05 +01:00
parent 629b1279a9
commit baced5f971
4 changed files with 83 additions and 23 deletions

View File

@@ -13,9 +13,15 @@ Boss::Boss(int x, int y, const sf::Texture& bossTexture, const sf::Texture& bull
}
hp = 100;
firerate = 2000;
movementSpeed = 2.0f;
}
void Boss::setPlanszaHeight(int height, int width) {
planszaHeight = height;
planszaWidth = width;
}
void Boss::shoot() {
if (shootClock.getElapsedTime().asMilliseconds() >= firerate) {
Bullet leftBullet(position.x - 20, position.y, bulletTexture);
@@ -35,7 +41,7 @@ void Boss::shoot() {
void Boss::dropBomb() {
if (bombClock.getElapsedTime().asMilliseconds() >= 5000) {
Bullet Bomb(position.x, position.y, BombaTexture);
Bomb.setSpeed(0.1f);
Bomb.setSpeed(0.5f);
bullets.emplace_back(std::move(Bomb)); // Można zmienić na bombę
std::cout << "Bombka lezy" << std::endl;
bombClock.restart();
@@ -47,6 +53,7 @@ void Boss::shootLaser() {
laserBeam = new Beam(position.x, position.y, beamTexture);
beamDurationClock.restart();
laserClock.restart();
isStationary = true;
}
if (laserBeam) {
@@ -56,12 +63,16 @@ void Boss::shootLaser() {
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);
@@ -69,16 +80,21 @@ void Boss::move(float deltaX, float deltaY) {
}
void Boss::update() {
if (directionClock.getElapsedTime().asSeconds() >= 3.0f) {
setRandomDirection();
directionClock.restart();
}
std::cout << "Current movementSpeed: " << movementSpeed << std::endl;
std::cout << "Boss position: (" << position.x << ", " << position.y << "), Direction: " << static_cast<int>(direction) << std::endl;
switch (direction) {
case BossDirection::Up: move(0, -movementSpeed); break;
case BossDirection::Down: move(0, movementSpeed); break;
case BossDirection::Left: move(-movementSpeed, 0); break;
case BossDirection::Right: move(movementSpeed, 0); break;
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();
@@ -125,9 +141,11 @@ void Boss::moveDown() {
}
void Boss::takeDamage() {
if (--hp <= 0) {
hp = 0;
std::cout << "HP bossa" << hp << std::endl;
}
}
@@ -136,14 +154,38 @@ bool Boss::isAlive() const {
}
void Boss::setRandomDirection() {
int randomValue = RandomNumberGenerator::getRandomNumber(0, 3);
direction = static_cast<BossDirection>(randomValue);
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 >= planszaWidth) ||
(direction == BossDirection::Up && position.y <= 0) ||
(direction == BossDirection::Down && position.y >= planszaHeight)
);
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;
if (position.x >= 300) direction = BossDirection::Left;
if (position.y <= 0) direction = BossDirection::Down;
if (position.y >= 300) direction = BossDirection::Up;
if (position.x < 0) {
position.x = 0;
direction = BossDirection::Right;
} else if (position.x > planszaWidth - actorSprite.getGlobalBounds().width) {
position.x = planszaWidth - actorSprite.getGlobalBounds().width;
direction = BossDirection::Left;
}
if (position.y < 0) {
position.y = 0;
direction = BossDirection::Down;
} else if (position.y > planszaHeight - actorSprite.getGlobalBounds().height) {
position.y = planszaHeight - actorSprite.getGlobalBounds().height;
direction = BossDirection::Up;
}
}