This repository has been archived on 2025-06-06. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LotoStatek/sources/Wiazkowiec.cpp

189 lines
5.2 KiB
C++

#include "../headers/Wiazkowiec.h"
#include <iostream>
#include <random>
#include "../headers/Plansza.h"
Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture& texture) : Actor(x, y, texture), beam(new Beam(0, 0, 50.f, 50.f)) {
actorSprite.setTexture(texture);
hp = 2; // 2 punkty życia
firerate = 5000; // Strzela co 10
moving_speed = 5.0f; // Prędkość
}
void Wiazkowiec::setPlanszaHeight(float height, float width) {
planszaHeight = height;
planszaWidth = width;
}
void Wiazkowiec::spawnBeam() {
float beamX = position.x;
float beamY = position.y;
float beamWidth = 50.f;
float beamHeight = 0.f;
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);
break;
}
// beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
beam->setVisible(true);
shooting = true;
shootingClock.restart();
}
// Strzał wiązki
void Wiazkowiec::shoot() {
if (!shooting) {
spawnBeam();
std::cout << "Wiazkowiec shot a beam!" << std::endl;
}
}
void Wiazkowiec::setRandomDirection() {
// Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right
int directionIndex = std::rand() % 4;
switch (directionIndex) {
case 0:
direction = DirectionW::Up;
break;
case 1:
direction = DirectionW::Down;
break;
case 2:
direction = DirectionW::Left;
break;
case 3:
direction = DirectionW::Right;
break;
}
}
void Wiazkowiec::updateDirection() {
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 (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);
}
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) {
beam->setVisible(false);
shooting = false;
setRandomDirection(); // Zmień kierunek po strzale
}
} else {
updateDirection();
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();
}
}
}
// 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;
}
void Wiazkowiec::takeDamage() {
if (--hp <= 0) {
alive = false;
Plansza::score += 10;
}
}
bool Wiazkowiec::isShooting() const {
return shooting;
}
const Beam* Wiazkowiec::getBeam() const {
return beam;
}