wiazkowiec strzela, gora dol
This commit is contained in:
157
sources/Wiazkowiec.cpp
Normal file
157
sources/Wiazkowiec.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
#include "../headers/Wiazkowiec.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "../headers/Bullet.h"
|
||||
#include <random>
|
||||
|
||||
Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture& texture) : Actor(x, y, texture), beam(0, 0, 50.f, 50.f, sf::Color::Red) {
|
||||
actorSprite.setTexture(texture);
|
||||
hp = 2; // 2 punkty życia
|
||||
firerate = 5000; // Strzela co 10
|
||||
moving_speed = 1.0f; // Prędkość
|
||||
// BombaTexture.loadFromFile("../assets/img/bullets/bomba.png");
|
||||
}
|
||||
|
||||
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;
|
||||
break;
|
||||
case DirectionW::Down:
|
||||
beamHeight = 600 - position.y;
|
||||
break;
|
||||
case DirectionW::Left:
|
||||
beamWidth = position.x;
|
||||
beamX -= beamWidth;
|
||||
break;
|
||||
case DirectionW::Right:
|
||||
beamWidth = 800 - position.x;
|
||||
break;
|
||||
}
|
||||
|
||||
beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red);
|
||||
beam.setVisible(true);
|
||||
|
||||
shooting = true;
|
||||
shootingClock.restart(); // Reset zegara
|
||||
}
|
||||
|
||||
// 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() {
|
||||
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
|
||||
if (position.y <= 0) {
|
||||
direction = DirectionW::Down;
|
||||
} else if (position.y >= 800) {
|
||||
direction = DirectionW::Up;
|
||||
}
|
||||
|
||||
// logika dla kierunku lewo/prawo
|
||||
if (position.x <= 0) {
|
||||
direction = DirectionW::Right;
|
||||
} else if (position.x >= 1200) {
|
||||
direction = DirectionW::Left;
|
||||
}
|
||||
}
|
||||
|
||||
void Wiazkowiec::move(float deltaX, float deltaY) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
bool Wiazkowiec::isShooting() const {
|
||||
return shooting;
|
||||
}
|
||||
|
||||
const Beam& Wiazkowiec::getBeam() const {
|
||||
return beam;
|
||||
}
|
||||
Reference in New Issue
Block a user