Niby strzela, ale bez kolizji
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "../headers/Actor.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Actor::Actor(int x, int y, std::string path) {
|
||||
loadTexture(path);
|
||||
position.x = x;
|
||||
@@ -29,13 +31,17 @@ std::vector<Bullet> &Actor::getBullets() {
|
||||
}
|
||||
|
||||
void Actor::updateBullets() {
|
||||
for (auto& bullet : bullets) {
|
||||
if(bullet.isOutOfBounds()) {
|
||||
bullets.erase(bullets.begin());
|
||||
for (auto it = bullets.begin(); it != bullets.end(); ) {
|
||||
if (it->isOutOfBounds()) {
|
||||
it = bullets.erase(it); // Usuwa element i zwraca iterator na następny element
|
||||
} else {
|
||||
++it; // Przechodzi do następnego elementu
|
||||
}
|
||||
}
|
||||
std::cout << "Liczba pociskow: " << bullets.size() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
void Actor::setMovingSpeed(float speed) {
|
||||
moving_speed = speed;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
18
sources/EnemyBullet.cpp
Normal file
18
sources/EnemyBullet.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "../headers/EnemyBullet.h"
|
||||
|
||||
EnemyBullet::EnemyBullet(float x, float y, sf::Texture &texture)
|
||||
: Projectile(x, y, texture) {
|
||||
speed = 5.0f; // Ruch w dół (dodatnia prędkość)
|
||||
}
|
||||
|
||||
void EnemyBullet::update() {
|
||||
sprite.move(0.0f, speed); // Przesuwanie pocisku w dół
|
||||
position.y += speed;
|
||||
|
||||
if (position.y > 800) {
|
||||
outOfBounds = true;
|
||||
}
|
||||
}
|
||||
void EnemyBullet::setOutOfBounds(bool status) {
|
||||
outOfBounds = status;
|
||||
}
|
||||
@@ -159,7 +159,7 @@ void Plansza::update() {
|
||||
}
|
||||
// Ruch i renderowanie przeciwników
|
||||
for (auto it = enemies.begin(); it != enemies.end();) {
|
||||
it->moveDown(); // Przeciwnicy poruszają się w dół
|
||||
it->update(); // Aktualizacja kierunku i ruchu przeciwnika
|
||||
it->shoot();
|
||||
|
||||
// Rysowanie przeciwników
|
||||
@@ -173,6 +173,28 @@ void Plansza::update() {
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& enemy : enemies) { // Lista przeciwników w grze
|
||||
enemy.shoot();
|
||||
enemy.updateBullets();
|
||||
for (auto& bullet : enemy.getBullets()) {
|
||||
bullet.update();
|
||||
window->draw(bullet.getSprite());
|
||||
}
|
||||
}
|
||||
|
||||
// Usuwanie pocisków, które są poza ekranem
|
||||
for (auto enemyIt = enemies.begin(); enemyIt != enemies.end(); ) {
|
||||
for (auto bulletIt = enemyIt->getBullets().begin(); bulletIt != enemyIt->getBullets().end();) {
|
||||
if (bulletIt->isOutOfBounds()) {
|
||||
bulletIt = enemyIt->getBullets().erase(bulletIt); // Usuwamy pocisk, który wyszedł poza ekran
|
||||
} else {
|
||||
++bulletIt;
|
||||
}
|
||||
}
|
||||
++enemyIt;
|
||||
}
|
||||
|
||||
|
||||
// Kolizje między pociskami gracza a przeciwnikami
|
||||
for (auto enemyIt = enemies.begin(); enemyIt != enemies.end();) {
|
||||
bool hit = false;
|
||||
|
||||
Reference in New Issue
Block a user