Do poprawy centrowanie lasera i strzelanie w lewo i w prawo

This commit is contained in:
2024-12-20 23:38:37 +01:00
parent 0a5a26208a
commit c4c83382c3
8 changed files with 125 additions and 128 deletions

View File

@@ -11,8 +11,7 @@
class Actor {
public:
Actor(int x, int y, const sf::Texture& texture);
virtual ~Actor() = default;
sf::Sprite& getSprite();
unsigned int getHP();

View File

@@ -5,23 +5,11 @@
class Beam {
public:
Beam(float x, float y, float width, float height);
void draw(sf::RenderWindow &window);
void update();
void render(sf::RenderWindow& window);
sf::FloatRect getBounds() const;
bool isVisible() const;
void setVisible(bool visible);
Beam() = default;
Beam(int x, int y, const sf::Texture &texture);
sf::Sprite getSprite();
private:
sf::RectangleShape beamShape;
bool visible;
sf::Texture beamTexture;
sf::Sprite beamSprite;
};

View File

@@ -6,4 +6,14 @@ struct Position {
int y;
};
struct PositionU {
unsigned int x;
unsigned int y;
};
struct PositionF {
float x;
float y;
};
#endif //LOTOSTATEK_POSITION_H

View File

@@ -13,40 +13,47 @@ enum class DirectionW {
class Wiazkowiec : public Actor {
public:
Wiazkowiec(int x, int y, const sf::Texture& texture);
Wiazkowiec(int x, int y, const sf::Texture& texture, sf::RenderWindow *window);
void shoot() override;
void move(float deltaX, float deltaY) override;
void moveLeft() override;
void moveRight() override;
void moveUp() override;
void moveDown() override;
void takeDamage();
void setRandomDirection();
void checkIfBeamShootOutOfBounds();
void update();
void shoot() override;
void render(sf::RenderWindow &window);
bool isAlive() const;
void takeDamage();
void checkIfBeamShootOutOfBounds();
bool isShooting() const;
const Beam* getBeam() const;
void setPlanszaHeight(float height, float width);
void setPlanszaHeight(int height, int width);
Beam* getBeam() const;
private:
float planszaHeight = 800.f;
float planszaWidth = 600.f;
sf::Clock shootClock;
sf::Texture WiazkaTexture;
float movementSpeed = 2.0f;
bool alive = true;
DirectionW direction = DirectionW::Down;
Beam *beam; // Wiązka
bool shooting = false;
sf::Texture beamTexture;
sf::Clock shootClock;
sf::Clock shootingClock;
float beamDuration = 1.0f;
DirectionW direction = DirectionW::Down;
Beam *beam; // wskaźnik na wiązkę
sf::RenderWindow *window_ptr;
void spawnBeam(); // Tworzy wiązkę
int planszaHeight = 800;
int planszaWidth = 600;
float beamDuration = 1.0f;
float movementSpeed = 2.0f;
bool shooting = false;
bool alive = true;
};
#endif //WIAZKOWIEC_H

View File

@@ -2,50 +2,16 @@
#include <iostream>
Beam::Beam(float x, float y, float width, float height)
: visible(false) {
beamShape.setPosition(x, y);
beamShape.setSize({width, height});
// beamShape.setFillColor(color);
if (!beamTexture.loadFromFile("../assets/img/wiazka/wiazka.png")) {
std::cerr << "Błąd! Nie można załadować tekstury wiazka.png" << std::endl;
}
beamSprite.setTexture(beamTexture);
if (beamTexture.getSize().x > 0 && beamTexture.getSize().y > 0) {
float scaleX = width / beamTexture.getSize().x;
float scaleY = height / beamTexture.getSize().y;
beamSprite.setScale(scaleX, scaleY);
Beam::Beam(int x, int y, const sf::Texture& texture) {
if (texture.getSize().x > 0 && texture.getSize().y > 0) {
beamSprite.setPosition(x, y);
} else {
std::cerr << "Błąd: Tekstura wiązki nie została poprawnie załadowana." << std::endl;
}
beamSprite.setTexture(texture);
}
void Beam::draw(sf::RenderWindow& window) {
window.draw(beamSprite);
}
void Beam::update() {
}
void Beam::render(sf::RenderWindow& window) {
window.draw(beamSprite);
}
sf::FloatRect Beam::getBounds() const {
return beamShape.getGlobalBounds();
}
bool Beam::isVisible() const {
return visible;
}
void Beam::setVisible(bool visible) {
this->visible = visible;
sf::Sprite Beam::getSprite() {
return beamSprite;
}

View File

@@ -445,14 +445,13 @@ void Plansza::update() {
for (auto &wiazkowiec: WEnemies) {
wiazkowiec.update();
if (wiazkowiec.isShooting() && wiazkowiec.getBeam()->isVisible()) {
if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam()->getBounds())) {
if (wiazkowiec.isShooting()) {
if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam()->getSprite().getGlobalBounds())) {
ship->takeDamage(); // Gracz otrzymuje obrażenia
}
}
window->draw(wiazkowiec.getSprite());
wiazkowiec.render(*window);
}
// TODO: naprawić to co średnio działa
@@ -778,13 +777,15 @@ void Plansza::spawn_kamikadze() {
void Plansza::spawn_wiazkowiec() {
if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 5) { // Spawn co 10 sekund
if (WEnemies.size() < 1) {
int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50);
Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture);
Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture, window);
wiazkowiec.setPlanszaHeight(size.height, size.width); // Przekazanie wysokości i szerokosci okna
WEnemies.push_back(wiazkowiec);
std::cout << "Spawned Wiazkowiec Enemy at X: " << spawnX << std::endl;
WiazkowiecSpawnClock.restart();
}
}
}

View File

@@ -21,8 +21,14 @@ Player* Player::getInstance(int x, int y, const sf::Texture& texture) {
}
void Player::loadTexture() {
try {
bulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png");
rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png");
} catch (std::exception &e) {
std::cerr << "Failed to load textures: " << e.what() << std::endl;
exit(-500);
}
damageDealClock.restart();
originalColor = actorSprite.getColor();
}

View File

@@ -5,51 +5,53 @@
#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);
break;
case DirectionW::Down:
beamHeight = planszaHeight - position.y;
beam = new Beam(beamX, beamY, beamWidth, beamHeight);
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);
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::Up: {
beam = new Beam(beamX, beamY - beamTexture.getSize().y, beamTexture);
break;
}
case DirectionW::Down: {
beam = new Beam(beamX, beamY + actorSprite.getGlobalBounds().height, beamTexture);
break;
}
case DirectionW::Left: {
beam = new Beam(beamX - beamTexture.getSize().x, beamY, beamTexture);
break;
}
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;
}