Bomber i wiazkowiec nie wylaza poza ekran

This commit is contained in:
2024-12-12 00:11:24 +01:00
parent 168ba2e477
commit f1dc19e795
6 changed files with 77 additions and 38 deletions

View File

@@ -9,12 +9,12 @@ Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture& texture) : Actor(x, y, t
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");
moving_speed = 5.0f; // Prędkość
}
void Wiazkowiec::setPlanszaHeight(float height) {
void Wiazkowiec::setPlanszaHeight(float height, float width) {
planszaHeight = height;
planszaWidth = width;
}
void Wiazkowiec::spawnBeam() {
@@ -30,18 +30,18 @@ void Wiazkowiec::spawnBeam() {
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
break;
case DirectionW::Down:
beamHeight = planszaHeight - position.y; // Dynamiczna wysokość okna
beamHeight = planszaHeight - position.y;
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
break;
case DirectionW::Left:
beamHeight = 50.f; // Stała wysokość
beamHeight = 50.f;
beamWidth = position.x;
beamX -= beamWidth; // Wiązka w lewo od pozycji statku
beamX -= beamWidth;
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ść
beamHeight = 50.f;
beamWidth = 800 - position.x;
beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
@@ -52,7 +52,7 @@ void Wiazkowiec::spawnBeam() {
beam.setVisible(true);
shooting = true;
shootingClock.restart(); // Reset zegara
shootingClock.restart();
}
// Strzał wiązki
@@ -84,23 +84,41 @@ void Wiazkowiec::setRandomDirection() {
}
void Wiazkowiec::updateDirection() {
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
auto spriteBounds = actorSprite.getGlobalBounds(); // Pobierz rozmiar i pozycję sprite'a
// Kontrola górnej i dolnej krawędzi
if (position.y <= 0) {
direction = DirectionW::Down;
} else if (position.y >= 800) {
} else if (position.y + spriteBounds.height >= planszaHeight) {
direction = DirectionW::Up;
}
// logika dla kierunku lewo/prawo
// Kontrola lewej i prawej krawędzi
if (position.x <= 0) {
direction = DirectionW::Right;
} else if (position.x >= 1200) {
} else if (position.x + spriteBounds.width >= planszaWidth) {
direction = DirectionW::Left;
}
}
void Wiazkowiec::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<int>(deltaX);
position.y += static_cast<int>(deltaY);