Niby strzela, ale bez kolizji
This commit is contained in:
@@ -3,28 +3,71 @@
|
||||
|
||||
Enemy::Enemy(int x, int y, std::string path) : Actor(x, y, path) {
|
||||
hp = 1; // Przeciwnik ma 1 punkt życia
|
||||
firerate = 1000; // Strzela co 1 sekundę
|
||||
firerate = 2000; // Strzela co 1 sekundę
|
||||
moving_speed = 2.0f; // Prędkość ruchu przeciwnika
|
||||
enemyBulletTexture.loadFromFile("../assets/img/bullets/enemy_bullet.png");
|
||||
}
|
||||
|
||||
void Enemy::shoot() {
|
||||
if (shootClock.getElapsedTime().asMilliseconds() >= firerate) {
|
||||
bullets.emplace_back(position.x, position.y + 20, bulletTextureLeft); // Strzał w dół
|
||||
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) { // Górna krawędź ekranu
|
||||
direction = Direction::Down; // Zmieniamy na ruch w dół
|
||||
} else if (position.y >= 800) { // Dolna krawędź ekranu
|
||||
direction = Direction::Up; // Zmieniamy na ruch w górę
|
||||
}
|
||||
|
||||
// Podobna logika dla kierunku lewo/prawo, jeśli przeciwnik będzie się poruszał w poziomie
|
||||
if (position.x <= 0) { // Lewa krawędź
|
||||
direction = Direction::Right; // Zmieniamy na ruch w prawo
|
||||
} else if (position.x >= 1200) { // Prawa krawędź
|
||||
direction = Direction::Left; // Zmieniamy na ruch w lewo
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
// Zgodnie z kierunkiem, poruszamy przeciwnikiem
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user