Dodanie bombera
This commit is contained in:
78
sources/Bomber.cpp
Normal file
78
sources/Bomber.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "../headers/Bomber.h"
|
||||
#include "../headers/Bullet.h"
|
||||
|
||||
Bomber::Bomber(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture) : Actor(x, y, texture) {
|
||||
actorSprite.setTexture(texture);
|
||||
BombaTexture = bulletTexture;
|
||||
hp = 2; // 2 punkty życia
|
||||
firerate = 2000; // Strzela co 2
|
||||
moving_speed = 2.0f; // Prędkość
|
||||
BombaTexture.loadFromFile("../assets/img/bullets/bomba.png");
|
||||
}
|
||||
|
||||
void Bomber::shoot() {
|
||||
if (shootClock.getElapsedTime().asMilliseconds() >= firerate) {
|
||||
Bullet Bbullet(position.x, position.y + actorSprite.getGlobalBounds().height / 2, BombaTexture);
|
||||
Bbullet.setSpeed(0.5f); // Prędkość w dół
|
||||
bullets.emplace_back(std::move(Bbullet));
|
||||
shootClock.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void Bomber::updateDirection() {
|
||||
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
|
||||
if (position.y <= 0) {
|
||||
direction = DirectionB::Down;
|
||||
} else if (position.y >= 800) {
|
||||
direction = DirectionB::Up;
|
||||
}
|
||||
|
||||
// logika dla kierunku lewo/prawo
|
||||
if (position.x <= 0) {
|
||||
direction = DirectionB::Right;
|
||||
} else if (position.x >= 1200) {
|
||||
direction = DirectionB::Left;
|
||||
}
|
||||
}
|
||||
|
||||
void Bomber::move(float deltaX, float deltaY) {
|
||||
actorSprite.move(deltaX, deltaY);
|
||||
position.x += static_cast<int>(deltaX);
|
||||
position.y += static_cast<int>(deltaY);
|
||||
}
|
||||
|
||||
void Bomber::moveLeft() { move(-moving_speed, 0.0f); }
|
||||
void Bomber::moveRight() { move(moving_speed, 0.0f); }
|
||||
void Bomber::moveUp() { move(0.0f, -moving_speed); }
|
||||
void Bomber::moveDown() { move(0.0f, moving_speed); }
|
||||
|
||||
void Bomber::update() {
|
||||
// Sprawdzamy, czy przeciwnik dotarł do krawędzi i zmieniamy kierunek
|
||||
updateDirection();
|
||||
|
||||
switch (direction) {
|
||||
case DirectionB::Up:
|
||||
moveUp();
|
||||
break;
|
||||
case DirectionB::Down:
|
||||
moveDown();
|
||||
break;
|
||||
case DirectionB::Left:
|
||||
moveLeft();
|
||||
break;
|
||||
case DirectionB::Right:
|
||||
moveRight();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Bomber::isAlive() const {
|
||||
return alive;
|
||||
}
|
||||
|
||||
void Bomber::takeDamage() {
|
||||
if (--hp <= 0) {
|
||||
alive = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user