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

@@ -7,11 +7,14 @@ Bomber::Bomber(int x, int y, const sf::Texture& texture, const sf::Texture& bull
BombaTexture = bulletTexture;
hp = 2; // 2 punkty życia
firerate = 10000; // Strzela co 10
moving_speed = 1.0f; // Prędkość
// BombaTexture.loadFromFile("../assets/img/bullets/bomba.png");
moving_speed = 10.0f; // Prędkość
}
void Bomber::setPlanszaHeight(float height, float width) {
planszaHeight = height;
planszaWidth = width;
}
// Losuje losowy kierunek dla Bombera
void Bomber::setRandomDirection() {
std::random_device rd;
std::mt19937 gen(rd());
@@ -47,22 +50,39 @@ void Bomber::shoot() {
}
void Bomber::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 (wysokości planszy)
if (position.y <= 0) {
direction = DirectionB::Down;
} else if (position.y >= 800) {
} else if (position.y + spriteBounds.height >= planszaHeight) {
direction = DirectionB::Up;
}
// logika dla kierunku lewo/prawo
// Kontrola lewej i prawej krawędzi (szerokości planszy)
if (position.x <= 0) {
direction = DirectionB::Right;
} else if (position.x >= 1200) {
} 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<int>(deltaX);
position.y += static_cast<int>(deltaY);