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