Do poprawy centrowanie lasera i strzelanie w lewo i w prawo
This commit is contained in:
@@ -11,8 +11,7 @@
|
|||||||
class Actor {
|
class Actor {
|
||||||
public:
|
public:
|
||||||
Actor(int x, int y, const sf::Texture& texture);
|
Actor(int x, int y, const sf::Texture& texture);
|
||||||
|
virtual ~Actor() = default;
|
||||||
|
|
||||||
|
|
||||||
sf::Sprite& getSprite();
|
sf::Sprite& getSprite();
|
||||||
unsigned int getHP();
|
unsigned int getHP();
|
||||||
|
|||||||
@@ -5,23 +5,11 @@
|
|||||||
|
|
||||||
class Beam {
|
class Beam {
|
||||||
public:
|
public:
|
||||||
Beam(float x, float y, float width, float height);
|
Beam() = default;
|
||||||
|
Beam(int x, int y, const sf::Texture &texture);
|
||||||
void draw(sf::RenderWindow &window);
|
|
||||||
|
|
||||||
void update();
|
|
||||||
void render(sf::RenderWindow& window);
|
|
||||||
|
|
||||||
sf::FloatRect getBounds() const;
|
|
||||||
|
|
||||||
bool isVisible() const;
|
|
||||||
void setVisible(bool visible);
|
|
||||||
|
|
||||||
|
|
||||||
|
sf::Sprite getSprite();
|
||||||
private:
|
private:
|
||||||
sf::RectangleShape beamShape;
|
|
||||||
bool visible;
|
|
||||||
sf::Texture beamTexture;
|
|
||||||
sf::Sprite beamSprite;
|
sf::Sprite beamSprite;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -6,4 +6,14 @@ struct Position {
|
|||||||
int y;
|
int y;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct PositionU {
|
||||||
|
unsigned int x;
|
||||||
|
unsigned int y;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct PositionF {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
};
|
||||||
|
|
||||||
#endif //LOTOSTATEK_POSITION_H
|
#endif //LOTOSTATEK_POSITION_H
|
||||||
|
|||||||
@@ -13,40 +13,47 @@ enum class DirectionW {
|
|||||||
|
|
||||||
class Wiazkowiec : public Actor {
|
class Wiazkowiec : public Actor {
|
||||||
public:
|
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 move(float deltaX, float deltaY) override;
|
||||||
void moveLeft() override;
|
void moveLeft() override;
|
||||||
void moveRight() override;
|
void moveRight() override;
|
||||||
void moveUp() override;
|
void moveUp() override;
|
||||||
void moveDown() override;
|
void moveDown() override;
|
||||||
|
|
||||||
|
void takeDamage();
|
||||||
|
|
||||||
void setRandomDirection();
|
void setRandomDirection();
|
||||||
|
void checkIfBeamShootOutOfBounds();
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
void shoot() override;
|
||||||
void render(sf::RenderWindow &window);
|
void render(sf::RenderWindow &window);
|
||||||
|
|
||||||
bool isAlive() const;
|
bool isAlive() const;
|
||||||
void takeDamage();
|
|
||||||
void checkIfBeamShootOutOfBounds();
|
|
||||||
bool isShooting() const;
|
bool isShooting() const;
|
||||||
const Beam* getBeam() const;
|
|
||||||
void setPlanszaHeight(float height, float width);
|
void setPlanszaHeight(int height, int width);
|
||||||
|
Beam* getBeam() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float planszaHeight = 800.f;
|
|
||||||
float planszaWidth = 600.f;
|
|
||||||
sf::Clock shootClock;
|
|
||||||
sf::Texture WiazkaTexture;
|
sf::Texture WiazkaTexture;
|
||||||
float movementSpeed = 2.0f;
|
sf::Texture beamTexture;
|
||||||
bool alive = true;
|
sf::Clock shootClock;
|
||||||
DirectionW direction = DirectionW::Down;
|
|
||||||
Beam *beam; // Wiązka
|
|
||||||
bool shooting = false;
|
|
||||||
sf::Clock shootingClock;
|
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ę
|
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
|
#endif //WIAZKOWIEC_H
|
||||||
|
|||||||
@@ -2,50 +2,16 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
Beam::Beam(float x, float y, float width, float height)
|
Beam::Beam(int x, int y, const sf::Texture& texture) {
|
||||||
: visible(false) {
|
if (texture.getSize().x > 0 && texture.getSize().y > 0) {
|
||||||
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);
|
|
||||||
beamSprite.setPosition(x, y);
|
beamSprite.setPosition(x, y);
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "Błąd: Tekstura wiązki nie została poprawnie załadowana." << std::endl;
|
std::cerr << "Błąd: Tekstura wiązki nie została poprawnie załadowana." << std::endl;
|
||||||
}
|
}
|
||||||
|
beamSprite.setTexture(texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Beam::draw(sf::RenderWindow& window) {
|
sf::Sprite Beam::getSprite() {
|
||||||
|
return beamSprite;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -445,14 +445,13 @@ void Plansza::update() {
|
|||||||
for (auto &wiazkowiec: WEnemies) {
|
for (auto &wiazkowiec: WEnemies) {
|
||||||
wiazkowiec.update();
|
wiazkowiec.update();
|
||||||
|
|
||||||
if (wiazkowiec.isShooting() && wiazkowiec.getBeam()->isVisible()) {
|
if (wiazkowiec.isShooting()) {
|
||||||
if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam()->getBounds())) {
|
if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam()->getSprite().getGlobalBounds())) {
|
||||||
ship->takeDamage(); // Gracz otrzymuje obrażenia
|
ship->takeDamage(); // Gracz otrzymuje obrażenia
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window->draw(wiazkowiec.getSprite());
|
window->draw(wiazkowiec.getSprite());
|
||||||
wiazkowiec.render(*window);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: naprawić to co średnio działa
|
// TODO: naprawić to co średnio działa
|
||||||
@@ -778,12 +777,14 @@ void Plansza::spawn_kamikadze() {
|
|||||||
|
|
||||||
void Plansza::spawn_wiazkowiec() {
|
void Plansza::spawn_wiazkowiec() {
|
||||||
if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 5) { // Spawn co 10 sekund
|
if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 5) { // Spawn co 10 sekund
|
||||||
int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50);
|
if (WEnemies.size() < 1) {
|
||||||
Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture);
|
int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50);
|
||||||
wiazkowiec.setPlanszaHeight(size.height, size.width); // Przekazanie wysokości i szerokosci okna
|
Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture, window);
|
||||||
WEnemies.push_back(wiazkowiec);
|
wiazkowiec.setPlanszaHeight(size.height, size.width); // Przekazanie wysokości i szerokosci okna
|
||||||
std::cout << "Spawned Wiazkowiec Enemy at X: " << spawnX << std::endl;
|
WEnemies.push_back(wiazkowiec);
|
||||||
WiazkowiecSpawnClock.restart();
|
std::cout << "Spawned Wiazkowiec Enemy at X: " << spawnX << std::endl;
|
||||||
|
WiazkowiecSpawnClock.restart();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,14 @@ Player* Player::getInstance(int x, int y, const sf::Texture& texture) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Player::loadTexture() {
|
void Player::loadTexture() {
|
||||||
bulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png");
|
try {
|
||||||
rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png");
|
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();
|
damageDealClock.restart();
|
||||||
originalColor = actorSprite.getColor();
|
originalColor = actorSprite.getColor();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,52 +5,54 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture) : Actor(x, y, texture),
|
Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture, sf::RenderWindow *window) : Actor(x, y, texture),
|
||||||
beam(new Beam(0, 0, 50.f, 50.f)) {
|
beam(nullptr)
|
||||||
|
{
|
||||||
|
window_ptr = window;
|
||||||
actorSprite.setTexture(texture);
|
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
|
hp = 2; // 2 punkty życia
|
||||||
firerate = 5000; // Strzela co 10
|
firerate = 5000; // Strzela co 10
|
||||||
moving_speed = 5.0f; // Prędkość
|
moving_speed = 5.0f; // Prędkość
|
||||||
}
|
}
|
||||||
|
|
||||||
void Wiazkowiec::setPlanszaHeight(float height, float width) {
|
void Wiazkowiec::setPlanszaHeight(int height, int width) {
|
||||||
planszaHeight = height;
|
planszaHeight = height;
|
||||||
planszaWidth = width;
|
planszaWidth = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Wiazkowiec::spawnBeam() {
|
void Wiazkowiec::spawnBeam() {
|
||||||
float beamX = position.x;
|
int beamX = actorSprite.getPosition().x;
|
||||||
float beamY = position.y;
|
int beamY = actorSprite.getPosition().y;
|
||||||
float beamWidth = 50.f;
|
|
||||||
float beamHeight = 0.f;
|
|
||||||
|
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case DirectionW::Up:
|
case DirectionW::Up: {
|
||||||
beamHeight = position.y;
|
beam = new Beam(beamX, beamY - beamTexture.getSize().y, beamTexture);
|
||||||
beamY -= beamHeight;
|
|
||||||
beam = new Beam(beamX, beamY, beamWidth, beamHeight);
|
|
||||||
break;
|
break;
|
||||||
case DirectionW::Down:
|
}
|
||||||
beamHeight = planszaHeight - position.y;
|
case DirectionW::Down: {
|
||||||
beam = new Beam(beamX, beamY, beamWidth, beamHeight);
|
beam = new Beam(beamX, beamY + actorSprite.getGlobalBounds().height, beamTexture);
|
||||||
break;
|
break;
|
||||||
case DirectionW::Left:
|
}
|
||||||
beamHeight = 50.f;
|
case DirectionW::Left: {
|
||||||
beamWidth = position.x;
|
beam = new Beam(beamX - beamTexture.getSize().x, beamY, beamTexture);
|
||||||
beamX -= beamWidth;
|
|
||||||
beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
|
|
||||||
beam = new Beam(beamX, beamY, beamWidth, beamHeight);
|
|
||||||
break;
|
break;
|
||||||
case DirectionW::Right:
|
}
|
||||||
beamHeight = 50.f;
|
case DirectionW::Right: {
|
||||||
beamWidth = 800 - position.x;
|
beam = new Beam(beamX + actorSprite.getGlobalBounds().width, beamY, beamTexture);
|
||||||
beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f;
|
break;
|
||||||
beam = new Beam(beamX, beamY, beamWidth, beamHeight);
|
}
|
||||||
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
beam->setVisible(true);
|
|
||||||
|
|
||||||
shooting = true;
|
shooting = true;
|
||||||
shootingClock.restart();
|
shootingClock.restart();
|
||||||
}
|
}
|
||||||
@@ -60,13 +62,35 @@ void Wiazkowiec::shoot() {
|
|||||||
if (!shooting) {
|
if (!shooting) {
|
||||||
spawnBeam();
|
spawnBeam();
|
||||||
std::cout << "Wiazkowiec shot a beam!" << std::endl;
|
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() {
|
void Wiazkowiec::setRandomDirection() {
|
||||||
// Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right
|
// Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right
|
||||||
|
|
||||||
switch (RandomNumberGenerator::getRandomNumber(0, 3)) {
|
int whatNumber = rand() % 4;
|
||||||
|
|
||||||
|
switch (whatNumber) {
|
||||||
case 0:
|
case 0:
|
||||||
direction = DirectionW::Up;
|
direction = DirectionW::Up;
|
||||||
break;
|
break;
|
||||||
@@ -107,17 +131,17 @@ void Wiazkowiec::move(float deltaX, float deltaY) {
|
|||||||
auto spriteBounds = actorSprite.getGlobalBounds(); // Rozmiar i pozycja sprite'a
|
auto spriteBounds = actorSprite.getGlobalBounds(); // Rozmiar i pozycja sprite'a
|
||||||
|
|
||||||
// Zapobiegaj wyjściu poza poziome granice
|
// Zapobiegaj wyjściu poza poziome granice
|
||||||
if (position.x + deltaX < 0) {
|
if (static_cast<float>(position.x) + deltaX < 0) {
|
||||||
deltaX = -position.x;
|
deltaX = static_cast<float>(-position.x);
|
||||||
} else if (position.x + spriteBounds.width + deltaX > planszaWidth) {
|
} else if (static_cast<float>(position.x) + spriteBounds.width + deltaX > static_cast<float>(planszaWidth)) {
|
||||||
deltaX = planszaWidth - (position.x + spriteBounds.width);
|
deltaX = static_cast<float>(planszaWidth) - (static_cast<float>(position.x) + spriteBounds.width);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zapobiegaj wyjściu poza pionowe granice
|
// Zapobiegaj wyjściu poza pionowe granice
|
||||||
if (position.y + deltaY < 0) {
|
if (static_cast<float>(position.y) + deltaY < 0) {
|
||||||
deltaY = -position.y;
|
deltaY = static_cast<float>(-position.y);
|
||||||
} else if (position.y + spriteBounds.height + deltaY > planszaHeight) {
|
} else if (static_cast<float>(position.y) + spriteBounds.height + deltaY > static_cast<float>(planszaHeight)) {
|
||||||
deltaY = planszaHeight - (position.y + spriteBounds.height);
|
deltaY = static_cast<float>(planszaHeight) - (static_cast<float>(position.y) + spriteBounds.height);
|
||||||
}
|
}
|
||||||
|
|
||||||
actorSprite.move(deltaX, deltaY);
|
actorSprite.move(deltaX, deltaY);
|
||||||
@@ -134,9 +158,12 @@ void Wiazkowiec::update() {
|
|||||||
if (shooting) {
|
if (shooting) {
|
||||||
// Kontrola zakończenia strzału
|
// Kontrola zakończenia strzału
|
||||||
if (shootingClock.getElapsedTime().asSeconds() >= beamDuration) {
|
if (shootingClock.getElapsedTime().asSeconds() >= beamDuration) {
|
||||||
beam->setVisible(false);
|
|
||||||
shooting = false;
|
shooting = false;
|
||||||
|
delete beam;
|
||||||
|
beam = nullptr;
|
||||||
setRandomDirection(); // Zmień kierunek po strzale
|
setRandomDirection(); // Zmień kierunek po strzale
|
||||||
|
} else {
|
||||||
|
window_ptr->draw(beam->getSprite());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
checkIfBeamShootOutOfBounds();
|
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 {
|
bool Wiazkowiec::isAlive() const {
|
||||||
return alive;
|
return alive;
|
||||||
}
|
}
|
||||||
@@ -186,6 +206,6 @@ bool Wiazkowiec::isShooting() const {
|
|||||||
return shooting;
|
return shooting;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Beam *Wiazkowiec::getBeam() const {
|
Beam* Wiazkowiec::getBeam() const {
|
||||||
return beam;
|
return beam;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user