Migrated the whole main loop
The main loop of the game is now contained in Plansza class
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
class AudioManager {
|
||||
public:
|
||||
AudioManager() = default;
|
||||
bool loadBackgroundMusic(const std::string& filePath);
|
||||
void playBackgroundMusic(float volume = 50.f, bool loop = true);
|
||||
void stopBackgroundMusic();
|
||||
|
||||
@@ -15,8 +15,7 @@
|
||||
|
||||
class Plansza {
|
||||
public:
|
||||
Plansza(unsigned int windowHeight, unsigned int windowWidth);
|
||||
Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow);
|
||||
Plansza(unsigned int windowHeight,unsigned int windowWidth, sf::RenderWindow *mainWindow);
|
||||
Size getSize();
|
||||
std::vector<Meteor> &getMeteors();
|
||||
void spawn_meteor();
|
||||
|
||||
154
main.cpp
154
main.cpp
@@ -1,10 +1,6 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "SFML/Graphics.hpp"
|
||||
#include "headers/Player.h"
|
||||
#include "headers/Background.h"
|
||||
#include "headers/AudioManager.h"
|
||||
#include "headers/Meteor.h"
|
||||
|
||||
#include "headers/Plansza.h"
|
||||
|
||||
int main()
|
||||
@@ -18,21 +14,7 @@ int main()
|
||||
icon.loadFromFile("../assets/img/icon/ikonka.png");
|
||||
mainWindow.setIcon(128, 128, icon.getPixelsPtr());
|
||||
|
||||
Plansza plansza(mainWindow.getSize().y, mainWindow.getSize().x);
|
||||
Background background("../assets/img/background/background.png", 2.0f); //tutaj predkosc tla, mozna zwiekszyc jak za wolno
|
||||
|
||||
AudioManager audioManager;
|
||||
audioManager.loadBackgroundMusic("../assets/music/background.ogg");
|
||||
audioManager.playBackgroundMusic();
|
||||
|
||||
audioManager.loadSoundEffect("shoot", "../assets/sounds/shoot.ogg");
|
||||
audioManager.loadSoundEffect("shoot_alt", "../assets/sounds/shoot_alt.ogg");
|
||||
audioManager.loadSoundEffect("fail", "../assets/sounds/fail.mp3");
|
||||
|
||||
// TODO: Przenieść tworzenie statku wewnątrz klasy Plansza
|
||||
Player ship(mainWindow.getSize().x / 2, mainWindow.getSize().y - 100, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
|
||||
ship.setMovingSpeed(8);
|
||||
ship.setFirerate(200);
|
||||
Plansza plansza(mainWindow.getSize().y, mainWindow.getSize().x, &mainWindow);
|
||||
|
||||
while (mainWindow.isOpen()) {
|
||||
mainWindow.clear();
|
||||
@@ -44,137 +26,7 @@ int main()
|
||||
mainWindow.close();
|
||||
}
|
||||
|
||||
// tło
|
||||
background.update();
|
||||
background.draw(mainWindow);
|
||||
|
||||
// poruszanie się statkiem
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
||||
if(ship.getPosition().x > 50) {
|
||||
ship.moveLeft();
|
||||
}
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
|
||||
if(ship.getPosition().y > 80) {
|
||||
ship.moveUp();
|
||||
}
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
|
||||
if(ship.getPosition().y < 720) {
|
||||
ship.moveDown();
|
||||
}
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
|
||||
if(ship.getPosition().x < 550) {
|
||||
ship.moveRight();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Przerobić tak aby strzelanie odbywało się bez użycia warunku zewnętrzenego
|
||||
// Ale w tym przypadku trzeba będzie przenieść obiekt dźwięku wewnątrz klasy Bullet
|
||||
// strzelanie ze statku
|
||||
if(event.type == sf::Event::MouseButtonPressed) {
|
||||
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
|
||||
ship.shoot();
|
||||
audioManager.playSoundEffect("shoot", 70.f); // Odtworzenie dźwięku wystrzału
|
||||
}
|
||||
if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
|
||||
ship.alternate_shoot();
|
||||
audioManager.playSoundEffect("shoot_alt", 70.f); // Odtworzenie dźwięku dla alternatywnego strzału
|
||||
}
|
||||
}
|
||||
|
||||
// generowanie nowego meteoru
|
||||
plansza.spawn_meteor();
|
||||
|
||||
|
||||
// utrzymanie meteorów i pocisków w ruchu
|
||||
for (auto& meteor : plansza.getMeteors()) {
|
||||
meteor.update();
|
||||
mainWindow.draw(meteor.getSprite());
|
||||
}
|
||||
|
||||
for (auto& bullet : ship.getBullets()) {
|
||||
bullet.update();
|
||||
mainWindow.draw(bullet.getSprite());
|
||||
}
|
||||
|
||||
for (auto& rocket : ship.getRockets()) {
|
||||
rocket.update();
|
||||
mainWindow.draw(rocket.getSprite());
|
||||
}
|
||||
|
||||
// Sprawdzenie czy meteory i pociski są poza granicami ekranu
|
||||
plansza.update_meteors();
|
||||
ship.updateBullets();
|
||||
|
||||
mainWindow.draw(ship.getSprite());
|
||||
|
||||
// trochę dziwny sposób ale jednak działa
|
||||
for (auto& meteor : plansza.getMeteors()) {
|
||||
if(ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
|
||||
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";
|
||||
return -1;
|
||||
}
|
||||
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();
|
||||
while (errorWindow.isOpen()) {
|
||||
while (errorWindow.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
||||
errorWindow.close();
|
||||
mainWindow.close();
|
||||
exit(-2);
|
||||
}
|
||||
}
|
||||
errorWindow.clear();
|
||||
errorWindow.draw(text);
|
||||
errorWindow.display();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto meteorIt = plansza.getMeteors().begin(); meteorIt != plansza.getMeteors().end(); ) {
|
||||
bool meteorHit = false;
|
||||
for (auto rocketIt = ship.getBullets().begin(); rocketIt != ship.getBullets().end(); ) {
|
||||
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
||||
ship.getBullets().erase(rocketIt);
|
||||
meteorIt = plansza.getMeteors().erase(meteorIt);
|
||||
meteorHit = true;
|
||||
break;
|
||||
} else {
|
||||
++rocketIt;
|
||||
}
|
||||
}
|
||||
if (!meteorHit) {
|
||||
++meteorIt;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto meteorIt = plansza.getMeteors().begin(); meteorIt != plansza.getMeteors().end(); ) {
|
||||
bool meteorHit = false;
|
||||
for (auto rocketIt = ship.getRockets().begin(); rocketIt != ship.getRockets().end(); ) {
|
||||
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
||||
ship.getRockets().erase(rocketIt);
|
||||
meteorIt = plansza.getMeteors().erase(meteorIt);
|
||||
meteorHit = true;
|
||||
break;
|
||||
} else {
|
||||
++rocketIt;
|
||||
}
|
||||
}
|
||||
if (!meteorHit) {
|
||||
++meteorIt;
|
||||
}
|
||||
}
|
||||
plansza.update();
|
||||
|
||||
mainWindow.display();
|
||||
}
|
||||
|
||||
@@ -2,18 +2,25 @@
|
||||
#include <iostream>
|
||||
#include "../headers/Plansza.h"
|
||||
|
||||
Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth) {
|
||||
size.height = windowHeight;
|
||||
size.width = windowWidth;
|
||||
meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png");
|
||||
meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png");
|
||||
spawnClock.restart();
|
||||
}
|
||||
Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow)
|
||||
: background("../assets/img/background/background.png", 2.0f),
|
||||
ship(static_cast<int>(mainWindow->getSize().x) / 2, static_cast<int>(mainWindow->getSize().y) - 100, "../assets/ship/Dreadnought-Base.png")
|
||||
{
|
||||
|
||||
Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow) {
|
||||
size.height = windowHeight;
|
||||
size.width = windowWidth;
|
||||
window = mainWindow;
|
||||
size.height = static_cast<int>(windowHeight);
|
||||
size.width = static_cast<int>(windowWidth);
|
||||
|
||||
ship.setMovingSpeed(8);
|
||||
ship.setFirerate(200);
|
||||
|
||||
audioManager.loadBackgroundMusic("../assets/music/background.ogg");
|
||||
audioManager.playBackgroundMusic();
|
||||
|
||||
audioManager.loadSoundEffect("shoot", "../assets/sounds/shoot.ogg");
|
||||
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();
|
||||
@@ -21,7 +28,134 @@ Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::Render
|
||||
|
||||
|
||||
void Plansza::update() {
|
||||
// tło
|
||||
background.update();
|
||||
background.draw(*window);
|
||||
|
||||
// poruszanie się statkiem
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
||||
if(ship.getPosition().x > 50) {
|
||||
ship.moveLeft();
|
||||
}
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
|
||||
if(ship.getPosition().y > 80) {
|
||||
ship.moveUp();
|
||||
}
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
|
||||
if(ship.getPosition().y < 720) {
|
||||
ship.moveDown();
|
||||
}
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
|
||||
if(ship.getPosition().x < 550) {
|
||||
ship.moveRight();
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Przenieść obiekt dźwięku wewnątrz klasy Bullet
|
||||
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
|
||||
ship.shoot();
|
||||
audioManager.playSoundEffect("shoot", 70.f); // Odtworzenie dźwięku wystrzału
|
||||
}
|
||||
if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
|
||||
ship.alternate_shoot();
|
||||
audioManager.playSoundEffect("shoot_alt", 70.f); // Odtworzenie dźwięku dla alternatywnego strzału
|
||||
}
|
||||
|
||||
// generowanie nowego meteoru
|
||||
spawn_meteor();
|
||||
|
||||
|
||||
// utrzymanie meteorów i pocisków w ruchu
|
||||
for (auto& meteor : getMeteors()) {
|
||||
meteor.update();
|
||||
window->draw(meteor.getSprite());
|
||||
}
|
||||
|
||||
for (auto& bullet : ship.getBullets()) {
|
||||
bullet.update();
|
||||
window->draw(bullet.getSprite());
|
||||
}
|
||||
|
||||
for (auto& rocket : ship.getRockets()) {
|
||||
rocket.update();
|
||||
window->draw(rocket.getSprite());
|
||||
}
|
||||
|
||||
// Sprawdzenie czy meteory i pociski są poza granicami ekranu
|
||||
update_meteors();
|
||||
ship.updateBullets();
|
||||
|
||||
window->draw(ship.getSprite());
|
||||
|
||||
// trochę dziwny sposób ale jednak działa
|
||||
for (auto& meteor : getMeteors()) {
|
||||
if(ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
|
||||
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(); ) {
|
||||
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
||||
ship.getBullets().erase(rocketIt);
|
||||
meteorIt = getMeteors().erase(meteorIt);
|
||||
meteorHit = true;
|
||||
break;
|
||||
} else {
|
||||
++rocketIt;
|
||||
}
|
||||
}
|
||||
if (!meteorHit) {
|
||||
++meteorIt;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end(); ) {
|
||||
bool meteorHit = false;
|
||||
for (auto rocketIt = ship.getRockets().begin(); rocketIt != ship.getRockets().end(); ) {
|
||||
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
||||
ship.getRockets().erase(rocketIt);
|
||||
meteorIt = getMeteors().erase(meteorIt);
|
||||
meteorHit = true;
|
||||
break;
|
||||
} else {
|
||||
++rocketIt;
|
||||
}
|
||||
}
|
||||
if (!meteorHit) {
|
||||
++meteorIt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Meteor-related niżej
|
||||
|
||||
Reference in New Issue
Block a user