Strzela z kolizja
This commit is contained in:
@@ -30,8 +30,6 @@ add_executable(LotoStatek main.cpp
|
|||||||
sources/ObjectItem.cpp
|
sources/ObjectItem.cpp
|
||||||
sources/Enemy.cpp
|
sources/Enemy.cpp
|
||||||
headers/Enemy.h
|
headers/Enemy.h
|
||||||
headers/EnemyBullet.h
|
|
||||||
sources/EnemyBullet.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -17,11 +17,14 @@ public:
|
|||||||
void moveRight() override;
|
void moveRight() override;
|
||||||
void moveUp() override;
|
void moveUp() override;
|
||||||
void moveDown() override;
|
void moveDown() override;
|
||||||
|
void takeDamage();
|
||||||
|
bool isAlive() const;
|
||||||
std::vector<Rocket>& getRockets();
|
std::vector<Rocket>& getRockets();
|
||||||
private:
|
private:
|
||||||
std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now();
|
std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now();
|
||||||
std::vector<Rocket> rockets;
|
std::vector<Rocket> rockets;
|
||||||
sf::Texture rocketTexture;
|
sf::Texture rocketTexture;
|
||||||
|
int health = 3; // Liczba punktów życia gracza
|
||||||
sf::Texture bulletTexture;
|
sf::Texture bulletTexture;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ void Actor::updateBullets() {
|
|||||||
++it; // Przechodzi do następnego elementu
|
++it; // Przechodzi do następnego elementu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << "Liczba pociskow: " << bullets.size() << std::endl;
|
//std::cout << "Liczba pociskow: " << bullets.size() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
Enemy::Enemy(int x, int y, std::string path) : Actor(x, y, path) {
|
Enemy::Enemy(int x, int y, std::string path) : Actor(x, y, path) {
|
||||||
hp = 1; // Przeciwnik ma 1 punkt życia
|
hp = 1; // Przeciwnik ma 1 punkt życia
|
||||||
firerate = 2000; // Strzela co 1 sekundę
|
firerate = 2000; // Strzela co 2
|
||||||
moving_speed = 2.0f; // Prędkość ruchu przeciwnika
|
moving_speed = 2.0f; // Prędkość
|
||||||
enemyBulletTexture.loadFromFile("../assets/img/bullets/enemy_bullet.png");
|
enemyBulletTexture.loadFromFile("../assets/img/bullets/enemy_bullet.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,17 +19,17 @@ void Enemy::shoot() {
|
|||||||
|
|
||||||
void Enemy::updateDirection() {
|
void Enemy::updateDirection() {
|
||||||
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
|
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
|
||||||
if (position.y <= 0) { // Górna krawędź ekranu
|
if (position.y <= 0) {
|
||||||
direction = Direction::Down; // Zmieniamy na ruch w dół
|
direction = Direction::Down;
|
||||||
} else if (position.y >= 800) { // Dolna krawędź ekranu
|
} else if (position.y >= 800) {
|
||||||
direction = Direction::Up; // Zmieniamy na ruch w górę
|
direction = Direction::Up;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Podobna logika dla kierunku lewo/prawo, jeśli przeciwnik będzie się poruszał w poziomie
|
// logika dla kierunku lewo/prawo
|
||||||
if (position.x <= 0) { // Lewa krawędź
|
if (position.x <= 0) {
|
||||||
direction = Direction::Right; // Zmieniamy na ruch w prawo
|
direction = Direction::Right;
|
||||||
} else if (position.x >= 1200) { // Prawa krawędź
|
} else if (position.x >= 1200) {
|
||||||
direction = Direction::Left; // Zmieniamy na ruch w lewo
|
direction = Direction::Left;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,7 +50,6 @@ void Enemy::update() {
|
|||||||
// Sprawdzamy, czy przeciwnik dotarł do krawędzi i zmieniamy kierunek
|
// Sprawdzamy, czy przeciwnik dotarł do krawędzi i zmieniamy kierunek
|
||||||
updateDirection();
|
updateDirection();
|
||||||
|
|
||||||
// Zgodnie z kierunkiem, poruszamy przeciwnikiem
|
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case Direction::Up:
|
case Direction::Up:
|
||||||
moveUp();
|
moveUp();
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -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("shoot_alt", "../assets/sounds/shoot_alt.ogg");
|
||||||
audioManager.loadSoundEffect("fail", "../assets/sounds/fail.mp3");
|
audioManager.loadSoundEffect("fail", "../assets/sounds/fail.mp3");
|
||||||
|
|
||||||
|
|
||||||
meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png");
|
meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png");
|
||||||
meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png");
|
meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png");
|
||||||
spawnClock.restart();
|
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(); ) {
|
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end(); ) {
|
||||||
bool meteorHit = false;
|
bool meteorHit = false;
|
||||||
for (auto rocketIt = ship.getBullets().begin(); rocketIt != ship.getBullets().end(); ) {
|
for (auto rocketIt = ship.getBullets().begin(); rocketIt != ship.getBullets().end(); ) {
|
||||||
@@ -157,12 +190,11 @@ void Plansza::update() {
|
|||||||
++meteorIt;
|
++meteorIt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Ruch i renderowanie przeciwników
|
// Ruch i render przeciwnika
|
||||||
for (auto it = enemies.begin(); it != enemies.end();) {
|
for (auto it = enemies.begin(); it != enemies.end();) {
|
||||||
it->update(); // Aktualizacja kierunku i ruchu przeciwnika
|
it->update(); // Aktualizacja kierunku i ruchu
|
||||||
it->shoot();
|
it->shoot();
|
||||||
|
|
||||||
// Rysowanie przeciwników
|
|
||||||
window->draw(it->getSprite());
|
window->draw(it->getSprite());
|
||||||
|
|
||||||
// Usunięcie martwych przeciwników
|
// 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.shoot();
|
||||||
enemy.updateBullets();
|
enemy.updateBullets();
|
||||||
for (auto& bullet : enemy.getBullets()) {
|
for (auto& bullet : enemy.getBullets()) {
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
#include "../headers/Player.h"
|
#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"
|
#include "../headers/Bullet.h"
|
||||||
|
|
||||||
Player::Player(int x, int y, std::string path) : Actor(x, y, path) {
|
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) {
|
void Player::setFirerate(unsigned int firerate) {
|
||||||
this->firerate = firerate;
|
this->firerate = firerate;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user