214 lines
5.8 KiB
C++
214 lines
5.8 KiB
C++
#include "../headers/Wiazkowiec.h"
|
|
#include "../headers/Plansza.h"
|
|
#include "../headers/RandomNumberGenerator.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture, sf::RenderWindow *window) : Actor(x, y, texture),
|
|
beam(nullptr)
|
|
{
|
|
window_ptr = window;
|
|
|
|
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(int height, int width) {
|
|
planszaHeight = height;
|
|
planszaWidth = width;
|
|
}
|
|
|
|
void Wiazkowiec::spawnBeam() {
|
|
int beamX = actorSprite.getPosition().x;
|
|
int beamY = actorSprite.getPosition().y;
|
|
|
|
switch (direction) {
|
|
case DirectionW::Up: {
|
|
beam = new Beam(beamX, beamY, beamTexture);
|
|
beam->setRotation(180);
|
|
break;
|
|
}
|
|
case DirectionW::Down: {
|
|
beam = new Beam(beamX, beamY, beamTexture);
|
|
break;
|
|
}
|
|
case DirectionW::Left: {
|
|
beam = new Beam(beamX, beamY, beamTexture);
|
|
beam->setRotation(90);
|
|
break;
|
|
}
|
|
case DirectionW::Right: {
|
|
beam = new Beam(beamX, beamY, beamTexture);
|
|
beam->setRotation(270);
|
|
break;
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
|
|
shooting = true;
|
|
shootingClock.restart();
|
|
}
|
|
|
|
// Strzał wiązki
|
|
void Wiazkowiec::shoot() {
|
|
if (!shooting) {
|
|
spawnBeam();
|
|
}
|
|
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
|
|
|
|
int whatNumber = rand() % 4;
|
|
|
|
switch (whatNumber) {
|
|
case 0:
|
|
direction = DirectionW::Up;
|
|
break;
|
|
case 1:
|
|
direction = DirectionW::Down;
|
|
break;
|
|
case 2:
|
|
direction = DirectionW::Left;
|
|
break;
|
|
case 3:
|
|
direction = DirectionW::Right;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void Wiazkowiec::checkIfBeamShootOutOfBounds() {
|
|
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 + spriteBounds.height >= planszaHeight) {
|
|
direction = DirectionW::Up;
|
|
}
|
|
|
|
// Kontrola lewej i prawej krawędzi
|
|
if (position.x <= 0) {
|
|
direction = DirectionW::Right;
|
|
} 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 (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 (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);
|
|
position.x += static_cast<int>(deltaX);
|
|
position.y += static_cast<int>(deltaY);
|
|
}
|
|
|
|
void Wiazkowiec::moveLeft() { move(-moving_speed, 0.0f); }
|
|
void Wiazkowiec::moveRight() { move(moving_speed, 0.0f); }
|
|
void Wiazkowiec::moveUp() { move(0.0f, -moving_speed); }
|
|
void Wiazkowiec::moveDown() { move(0.0f, moving_speed); }
|
|
|
|
void Wiazkowiec::update() {
|
|
if (shooting) {
|
|
// Kontrola zakończenia strzału
|
|
if (shootingClock.getElapsedTime().asSeconds() >= beamDuration) {
|
|
shooting = false;
|
|
delete beam;
|
|
beam = nullptr;
|
|
setRandomDirection(); // Zmień kierunek po strzale
|
|
} else {
|
|
window_ptr->draw(beam->getSprite());
|
|
}
|
|
} else {
|
|
checkIfBeamShootOutOfBounds();
|
|
|
|
switch (direction) {
|
|
case DirectionW::Up:
|
|
moveUp();
|
|
break;
|
|
case DirectionW::Down:
|
|
moveDown();
|
|
break;
|
|
case DirectionW::Left:
|
|
moveLeft();
|
|
break;
|
|
case DirectionW::Right:
|
|
moveRight();
|
|
break;
|
|
}
|
|
|
|
if (shootClock.getElapsedTime().asSeconds() >= 3.0f) {
|
|
// Co 3 sekundy
|
|
shoot();
|
|
shootClock.restart();
|
|
}
|
|
}
|
|
}
|
|
|
|
bool Wiazkowiec::isAlive() const {
|
|
return alive;
|
|
}
|
|
|
|
void Wiazkowiec::takeDamage() {
|
|
if (--hp <= 0) {
|
|
alive = false;
|
|
Plansza::score += 10;
|
|
Plansza::ultimateCounter += 10;
|
|
}
|
|
}
|
|
|
|
bool Wiazkowiec::isShooting() const {
|
|
return shooting;
|
|
}
|
|
|
|
Beam* Wiazkowiec::getBeam() const {
|
|
return beam;
|
|
}
|