80 lines
2.1 KiB
C++
80 lines
2.1 KiB
C++
#include "../headers/Enemy.h"
|
|
#include "../headers/Bullet.h"
|
|
|
|
Enemy::Enemy(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) {
|
|
actorSprite.setTexture(texture);
|
|
hp = 1; // Przeciwnik ma 1 punkt życia
|
|
firerate = 2000; // Strzela co 2
|
|
moving_speed = 2.0f; // Prędkość
|
|
enemyBulletTexture.loadFromFile("../assets/img/bullets/enemy_bullet.png");
|
|
}
|
|
|
|
void Enemy::shoot() {
|
|
if (shootClock.getElapsedTime().asMilliseconds() >= firerate) {
|
|
Bullet bullet(position.x, position.y + actorSprite.getGlobalBounds().height / 2, enemyBulletTexture);
|
|
bullet.setSpeed(10.0f); // Prędkość w dół
|
|
bullets.emplace_back(std::move(bullet));
|
|
shootClock.restart();
|
|
}
|
|
}
|
|
|
|
void Enemy::updateDirection() {
|
|
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
|
|
if (position.y <= 0) {
|
|
direction = Direction::Down;
|
|
} else if (position.y >= 800) {
|
|
direction = Direction::Up;
|
|
}
|
|
|
|
// logika dla kierunku lewo/prawo
|
|
if (position.x <= 0) {
|
|
direction = Direction::Right;
|
|
} else if (position.x >= 1200) {
|
|
direction = Direction::Left;
|
|
}
|
|
}
|
|
|
|
void Enemy::move(float deltaX, float deltaY) {
|
|
actorSprite.move(deltaX, deltaY);
|
|
position.x += static_cast<int>(deltaX);
|
|
position.y += static_cast<int>(deltaY);
|
|
}
|
|
|
|
|
|
|
|
void Enemy::moveLeft() { move(-moving_speed, 0.0f); }
|
|
void Enemy::moveRight() { move(moving_speed, 0.0f); }
|
|
void Enemy::moveUp() { move(0.0f, -moving_speed); }
|
|
void Enemy::moveDown() { move(0.0f, moving_speed); }
|
|
|
|
void Enemy::update() {
|
|
// Sprawdzamy, czy przeciwnik dotarł do krawędzi i zmieniamy kierunek
|
|
updateDirection();
|
|
|
|
switch (direction) {
|
|
case Direction::Up:
|
|
moveUp();
|
|
break;
|
|
case Direction::Down:
|
|
moveDown();
|
|
break;
|
|
case Direction::Left:
|
|
moveLeft();
|
|
break;
|
|
case Direction::Right:
|
|
moveRight();
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
bool Enemy::isAlive() const {
|
|
return alive;
|
|
}
|
|
|
|
void Enemy::takeDamage() {
|
|
if (--hp <= 0) {
|
|
alive = false;
|
|
}
|
|
}
|