Do poprawy centrowanie lasera i strzelanie w lewo i w prawo
This commit is contained in:
@@ -5,52 +5,54 @@
|
||||
#include <iostream>
|
||||
|
||||
|
||||
Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture) : Actor(x, y, texture),
|
||||
beam(new Beam(0, 0, 50.f, 50.f)) {
|
||||
Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture, sf::RenderWindow *window) : Actor(x, y, texture),
|
||||
beam(nullptr)
|
||||
{
|
||||
window_ptr = window;
|
||||
actorSprite.setTexture(texture);
|
||||
|
||||
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 = 2; // 2 punkty życia
|
||||
firerate = 5000; // Strzela co 10
|
||||
moving_speed = 5.0f; // Prędkość
|
||||
}
|
||||
|
||||
void Wiazkowiec::setPlanszaHeight(float height, float width) {
|
||||
void Wiazkowiec::setPlanszaHeight(int height, int width) {
|
||||
planszaHeight = height;
|
||||
planszaWidth = width;
|
||||
}
|
||||
|
||||
void Wiazkowiec::spawnBeam() {
|
||||
float beamX = position.x;
|
||||
float beamY = position.y;
|
||||
float beamWidth = 50.f;
|
||||
float beamHeight = 0.f;
|
||||
int beamX = actorSprite.getPosition().x;
|
||||
int beamY = actorSprite.getPosition().y;
|
||||
|
||||
switch (direction) {
|
||||
case DirectionW::Up:
|
||||
beamHeight = position.y;
|
||||
beamY -= beamHeight;
|
||||
beam = new Beam(beamX, beamY, beamWidth, beamHeight);
|
||||
case DirectionW::Up: {
|
||||
beam = new Beam(beamX, beamY - beamTexture.getSize().y, beamTexture);
|
||||
break;
|
||||
case DirectionW::Down:
|
||||
beamHeight = planszaHeight - position.y;
|
||||
beam = new Beam(beamX, beamY, beamWidth, beamHeight);
|
||||
}
|
||||
case DirectionW::Down: {
|
||||
beam = new Beam(beamX, beamY + actorSprite.getGlobalBounds().height, beamTexture);
|
||||
break;
|
||||
case DirectionW::Left:
|
||||
beamHeight = 50.f;
|
||||
beamWidth = position.x;
|
||||
beamX -= beamWidth;
|
||||
beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
|
||||
beam = new Beam(beamX, beamY, beamWidth, beamHeight);
|
||||
}
|
||||
case DirectionW::Left: {
|
||||
beam = new Beam(beamX - beamTexture.getSize().x, beamY, beamTexture);
|
||||
break;
|
||||
case DirectionW::Right:
|
||||
beamHeight = 50.f;
|
||||
beamWidth = 800 - position.x;
|
||||
beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
|
||||
beam = new Beam(beamX, beamY, beamWidth, beamHeight);
|
||||
}
|
||||
case DirectionW::Right: {
|
||||
beam = new Beam(beamX + actorSprite.getGlobalBounds().width, beamY, beamTexture);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
beam->setVisible(true);
|
||||
|
||||
shooting = true;
|
||||
shootingClock.restart();
|
||||
}
|
||||
@@ -60,13 +62,35 @@ void Wiazkowiec::shoot() {
|
||||
if (!shooting) {
|
||||
spawnBeam();
|
||||
std::cout << "Wiazkowiec shot a beam!" << std::endl;
|
||||
switch (direction) {
|
||||
case DirectionW::Up: {
|
||||
std::cout << "Direction is up" << std::endl;
|
||||
break;
|
||||
}
|
||||
case DirectionW::Down: {
|
||||
std::cout << "Direction is down" << std::endl;
|
||||
break;
|
||||
}
|
||||
case DirectionW::Left: {
|
||||
std::cout << "Direction is left" << std::endl;
|
||||
break;
|
||||
}
|
||||
case DirectionW::Right: {
|
||||
std::cout << "Direction is right" << std::endl;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Wiazkowiec::setRandomDirection() {
|
||||
// Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right
|
||||
|
||||
switch (RandomNumberGenerator::getRandomNumber(0, 3)) {
|
||||
int whatNumber = rand() % 4;
|
||||
|
||||
switch (whatNumber) {
|
||||
case 0:
|
||||
direction = DirectionW::Up;
|
||||
break;
|
||||
@@ -107,17 +131,17 @@ 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);
|
||||
if (static_cast<float>(position.x) + deltaX < 0) {
|
||||
deltaX = static_cast<float>(-position.x);
|
||||
} else if (static_cast<float>(position.x) + spriteBounds.width + deltaX > static_cast<float>(planszaWidth)) {
|
||||
deltaX = static_cast<float>(planszaWidth) - (static_cast<float>(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);
|
||||
if (static_cast<float>(position.y) + deltaY < 0) {
|
||||
deltaY = static_cast<float>(-position.y);
|
||||
} else if (static_cast<float>(position.y) + spriteBounds.height + deltaY > static_cast<float>(planszaHeight)) {
|
||||
deltaY = static_cast<float>(planszaHeight) - (static_cast<float>(position.y) + spriteBounds.height);
|
||||
}
|
||||
|
||||
actorSprite.move(deltaX, deltaY);
|
||||
@@ -134,9 +158,12 @@ void Wiazkowiec::update() {
|
||||
if (shooting) {
|
||||
// Kontrola zakończenia strzału
|
||||
if (shootingClock.getElapsedTime().asSeconds() >= beamDuration) {
|
||||
beam->setVisible(false);
|
||||
shooting = false;
|
||||
delete beam;
|
||||
beam = nullptr;
|
||||
setRandomDirection(); // Zmień kierunek po strzale
|
||||
} else {
|
||||
window_ptr->draw(beam->getSprite());
|
||||
}
|
||||
} else {
|
||||
checkIfBeamShootOutOfBounds();
|
||||
@@ -164,13 +191,6 @@ void Wiazkowiec::update() {
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
@@ -186,6 +206,6 @@ bool Wiazkowiec::isShooting() const {
|
||||
return shooting;
|
||||
}
|
||||
|
||||
const Beam *Wiazkowiec::getBeam() const {
|
||||
Beam* Wiazkowiec::getBeam() const {
|
||||
return beam;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user