110 lines
3.7 KiB
C++
110 lines
3.7 KiB
C++
#include <iostream>
|
|
|
|
#include "SFML/Graphics.hpp"
|
|
#include "headers/Player.h"
|
|
#include "headers/Meteor.h"
|
|
#include "headers/Plansza.h"
|
|
|
|
int main()
|
|
{
|
|
std::clog << "Game started\n";
|
|
sf::RenderWindow window(sf::VideoMode(600, 800), "My window");
|
|
window.setVerticalSyncEnabled(true);
|
|
window.setFramerateLimit(60);
|
|
Plansza plansza(window.getSize().y, window.getSize().x);
|
|
|
|
sf::Texture backgroundTexture;
|
|
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
|
|
sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła
|
|
|
|
// TODO: Przenieść tworzenie statku wewnątrz klasy Plansza
|
|
Player ship(window.getSize().x/2,window.getSize().y - 100, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
|
|
ship.setMovingSpeed(8);
|
|
ship.setFirerate(200);
|
|
|
|
while (window.isOpen()) {
|
|
// std::cout << "Liczba: " << RandomNumberGenerator::getRandomNumber(0,499) << std::endl;
|
|
window.clear();
|
|
|
|
window.draw(backgroundSprite); // narysuj tło
|
|
|
|
// Tu są handlowane eventy
|
|
sf::Event event{};
|
|
while (window.pollEvent(event)) {
|
|
if(event.type == sf::Event::Closed)
|
|
window.close();
|
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
|
window.close();
|
|
}
|
|
}
|
|
|
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
|
if(ship.getPosition().x > -10) {
|
|
ship.moveLeft();
|
|
}
|
|
}
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
|
|
if(ship.getPosition().y > 0) {
|
|
ship.moveUp();
|
|
}
|
|
}
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
|
|
if(ship.getPosition().y < 700) {
|
|
ship.moveDown();
|
|
}
|
|
}
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
|
|
if(ship.getPosition().x < 480) {
|
|
ship.moveRight();
|
|
}
|
|
}
|
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
|
|
ship.shoot();
|
|
}
|
|
|
|
|
|
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) ship.shoot();
|
|
if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) ship.alternate_shoot();
|
|
|
|
// Generate a new meteor at a random position at the top of the screen
|
|
plansza.spawn_meteor();
|
|
|
|
|
|
// Update and draw meteors
|
|
for (auto& meteor : plansza.getMeteors()) {
|
|
meteor.update();
|
|
window.draw(meteor.getSprite());
|
|
}
|
|
|
|
for (auto& bullet : ship.getBullets()) {
|
|
bullet.update();
|
|
window.draw(bullet.getSprite());
|
|
}
|
|
|
|
plansza.update_meteors();
|
|
ship.updateBullets();
|
|
window.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";
|
|
// window.close();
|
|
// exit(-2); // Kod -2 oznacza uderzenie się w meteoryt
|
|
}
|
|
}
|
|
|
|
// TODO: Poprawić kolizję pocisku z meteorem, tak aby był kasowany tylko ten konkretny meteoryt
|
|
// for (auto meteorIt = plansza.getMeteors().begin(); meteorIt != plansza.getMeteors().end(); ++meteorIt) {
|
|
// for (auto& bullet : ship.getBullets()) {
|
|
// if (meteorIt->getSprite().getGlobalBounds().intersects(bullet.getSprite().getGlobalBounds())) {
|
|
// meteorIt = plansza.getMeteors().erase(meteorIt);
|
|
// break; // Exit the inner loop to avoid invalidating the iterator
|
|
// }
|
|
// }
|
|
// }
|
|
window.display();
|
|
}
|
|
|
|
return 0;
|
|
} |