Strzela z kolizja

This commit is contained in:
2024-12-09 23:09:44 +01:00
parent 13066709a7
commit a81cf284d0
8 changed files with 91 additions and 51 deletions

View File

@@ -30,8 +30,6 @@ add_executable(LotoStatek main.cpp
sources/ObjectItem.cpp
sources/Enemy.cpp
headers/Enemy.h
headers/EnemyBullet.h
sources/EnemyBullet.cpp
)
if(WIN32)

View File

@@ -1,14 +0,0 @@
#ifndef ENEMY_BULLET_H
#define ENEMY_BULLET_H
#include "Projectile.h"
class EnemyBullet : public Projectile {
public:
EnemyBullet(float x, float y, sf::Texture &texture);
void update() override;
void setOutOfBounds(bool status);
};
#endif // ENEMY_BULLET_H

View File

@@ -17,11 +17,14 @@ public:
void moveRight() override;
void moveUp() override;
void moveDown() override;
void takeDamage();
bool isAlive() const;
std::vector<Rocket>& getRockets();
private:
std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now();
std::vector<Rocket> rockets;
sf::Texture rocketTexture;
int health = 3; // Liczba punktów życia gracza
sf::Texture bulletTexture;
};

View File

@@ -38,7 +38,7 @@ void Actor::updateBullets() {
++it; // Przechodzi do następnego elementu
}
}
std::cout << "Liczba pociskow: " << bullets.size() << std::endl;
//std::cout << "Liczba pociskow: " << bullets.size() << std::endl;
}

View File

@@ -3,8 +3,8 @@
Enemy::Enemy(int x, int y, std::string path) : Actor(x, y, path) {
hp = 1; // Przeciwnik ma 1 punkt życia
firerate = 2000; // Strzela co 1 sekundę
moving_speed = 2.0f; // Prędkość ruchu przeciwnika
firerate = 2000; // Strzela co 2
moving_speed = 2.0f; // Prędkość
enemyBulletTexture.loadFromFile("../assets/img/bullets/enemy_bullet.png");
}
@@ -19,17 +19,17 @@ void Enemy::shoot() {
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ę
if (position.y <= 0) {
direction = Direction::Down;
} else if (position.y >= 800) {
direction = Direction::Up;
}
// 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
// logika dla kierunku lewo/prawo
if (position.x <= 0) {
direction = Direction::Right;
} else if (position.x >= 1200) {
direction = Direction::Left;
}
}
@@ -50,7 +50,6 @@ 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();

View File

@@ -1,18 +0,0 @@
#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;
}

View File

@@ -22,6 +22,7 @@ ship(static_cast<int>(mainWindow->getSize().x) / 2, static_cast<int>(mainWindow-
audioManager.loadSoundEffect("shoot_alt", "../assets/sounds/shoot_alt.ogg");
audioManager.loadSoundEffect("fail", "../assets/sounds/fail.mp3");
meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png");
meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png");
spawnClock.restart();
@@ -124,6 +125,38 @@ void Plansza::update() {
}
}
if (!ship.isAlive()) {
std::cout << "Game Over! Player is dead." << std::endl;
std::cout << "You lost the game!\n";
sf::RenderWindow errorWindow(sf::VideoMode(350, 200), "The end");
sf::Font font;
if (!font.loadFromFile("../assets/fonts/arial.ttf")) {
std::cerr << "Error loading font\n";
exit(-500);
}
sf::Text text("Your ship is destroyed!", font, 24);
text.setFillColor(sf::Color::Red);
text.setPosition(50, 80);
// zatrzymanie muzyki i odtworzenie dźwięku przegranej
audioManager.playSoundEffect("fail", 70.f);
audioManager.stopBackgroundMusic();
sf::Event event{};
while (errorWindow.isOpen()) {
while (errorWindow.pollEvent(event)) {
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
errorWindow.close();
window->close();
exit(-2);
}
}
errorWindow.clear();
errorWindow.draw(text);
errorWindow.display();
}
}
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end(); ) {
bool meteorHit = false;
for (auto rocketIt = ship.getBullets().begin(); rocketIt != ship.getBullets().end(); ) {
@@ -157,12 +190,11 @@ void Plansza::update() {
++meteorIt;
}
}
// Ruch i renderowanie przeciwników
// Ruch i render przeciwnika
for (auto it = enemies.begin(); it != enemies.end();) {
it->update(); // Aktualizacja kierunku i ruchu przeciwnika
it->update(); // Aktualizacja kierunku i ruchu
it->shoot();
// Rysowanie przeciwników
window->draw(it->getSprite());
// Usunięcie martwych przeciwników
@@ -173,7 +205,23 @@ void Plansza::update() {
}
}
for (auto& enemy : enemies) { // Lista przeciwników w grze
for (auto& enemy : enemies) {
for (auto it = enemy.getBullets().begin(); it != enemy.getBullets().end();) {
if (ship.getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) {
// Kolizja wykryta
std::cout << "Player hit by enemy bullet!\n";
ship.takeDamage();
// Usuwanie pocisku
it = enemy.getBullets().erase(it);
} else {
++it; // Brak kolizji, przechodzimy do kolejnego pocisku
}
}
}
for (auto& enemy : enemies) {
enemy.shoot();
enemy.updateBullets();
for (auto& bullet : enemy.getBullets()) {

View File

@@ -1,4 +1,10 @@
#include "../headers/Player.h"
#include <iostream>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp>
#include "../headers/Bullet.h"
Player::Player(int x, int y, std::string path) : Actor(x, y, path) {
@@ -22,6 +28,24 @@ void Player::alternate_shoot() {
}
}
void Player::takeDamage() {
if (health > 0) {
health--;
std::cout << "Player hit! Remaining health: " << health << "\n";
if (health <= 0) {
std::cout << "Player has been destroyed!\n";
std::cout << "You lost the game!\n";
}
}
}
bool Player::isAlive() const {
return health > 0;
}
void Player::setFirerate(unsigned int firerate) {
this->firerate = firerate;
}