Meteory są w tym samym stanie co w poprzednim commit, tylko, że teraz są zdefiniowane w osobnej klasie "Plansza"
This commit is contained in:
@@ -13,7 +13,8 @@ add_executable(LotoStatek main.cpp
|
||||
headers/Bullet.h
|
||||
sources/Bullet.cpp
|
||||
headers/Meteor.h
|
||||
sources/Meteor.cpp)
|
||||
sources/Meteor.cpp
|
||||
headers/RandomNumberGenerator.h)
|
||||
|
||||
if(WIN32)
|
||||
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML")
|
||||
|
||||
@@ -12,15 +12,15 @@ class Bullet {
|
||||
|
||||
public:
|
||||
Bullet(float x, float y, sf::Texture &texture);
|
||||
void update();
|
||||
sf::Sprite& getSprite();
|
||||
sf::Sprite &getSprite();
|
||||
void setSpeed(float speed);
|
||||
bool getStatus() const;
|
||||
void update();
|
||||
private:
|
||||
sf::Sprite bulletSprite;
|
||||
sf::Texture bulletTexture;
|
||||
float bulletSpeed;
|
||||
Position bulletPosition;
|
||||
float bulletSpeed;
|
||||
bool outOfBounds;
|
||||
};
|
||||
|
||||
|
||||
@@ -9,15 +9,16 @@ class Meteor {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
public:
|
||||
Meteor(int x, int y, sf::Texture &texture);
|
||||
sf::Sprite & getSprite();
|
||||
Meteor(float x, float y, sf::Texture &texture);
|
||||
sf::Sprite &getSprite();
|
||||
bool getStatus();
|
||||
void update();
|
||||
private:
|
||||
sf::Texture meteorTexture;
|
||||
sf::Sprite meteorSprite;
|
||||
Position position;
|
||||
Position meteorPosition;
|
||||
float meteorSpeed;
|
||||
bool outOfBounds;
|
||||
};
|
||||
|
||||
@@ -2,11 +2,25 @@
|
||||
#define PLANSZA_H
|
||||
|
||||
|
||||
#include "Meteor.h"
|
||||
#include "RandomNumberGenerator.h"
|
||||
|
||||
class Plansza {
|
||||
|
||||
struct Size {
|
||||
int height;
|
||||
int width;
|
||||
};
|
||||
public:
|
||||
Plansza(int windowHeight, int windowWidth);
|
||||
void spawn_meteor();
|
||||
Size getSize();
|
||||
std::vector<Meteor> &getMeteors();
|
||||
void update_meteors();
|
||||
private:
|
||||
std::vector<Meteor> meteors;
|
||||
Size size;
|
||||
sf::Texture meteorTexture;
|
||||
RandomNumberGenerator random;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //PLANSZA_H
|
||||
|
||||
20
headers/RandomNumberGenerator.h
Normal file
20
headers/RandomNumberGenerator.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef LOTOSTATEK_RANDOMNUMBERGENERATOR_H
|
||||
#define LOTOSTATEK_RANDOMNUMBERGENERATOR_H
|
||||
|
||||
#include <random>
|
||||
|
||||
class RandomNumberGenerator {
|
||||
private:
|
||||
std::random_device rd;
|
||||
std::mt19937 gen;
|
||||
std::uniform_int_distribution<> dis;
|
||||
|
||||
public:
|
||||
RandomNumberGenerator() : gen(rd()), dis(0, 599) {}
|
||||
|
||||
int getRandomNumber() {
|
||||
return dis(gen);
|
||||
}
|
||||
};
|
||||
|
||||
#endif //LOTOSTATEK_RANDOMNUMBERGENERATOR_H
|
||||
26
main.cpp
26
main.cpp
@@ -4,6 +4,7 @@
|
||||
#include "SFML/Graphics.hpp"
|
||||
#include "headers/Player.h"
|
||||
#include "headers/Meteor.h"
|
||||
#include "headers/Plansza.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -11,15 +12,7 @@ int main()
|
||||
sf::RenderWindow window(sf::VideoMode(600, 800), "My window");
|
||||
window.setVerticalSyncEnabled(true);
|
||||
window.setFramerateLimit(60);
|
||||
|
||||
// Ustawienia randomizera
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<> dis(0, 599);
|
||||
// Koniec ustawień randomizera
|
||||
|
||||
sf::Texture meteorTexture;
|
||||
meteorTexture.loadFromFile("../assets/img/meteor.png");
|
||||
Plansza plansza(window.getSize().y, window.getSize().x);
|
||||
|
||||
sf::Texture backgroundTexture;
|
||||
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
|
||||
@@ -29,9 +22,6 @@ int main()
|
||||
ship.setMovingSpeed(8);
|
||||
ship.setFirerate(200);
|
||||
|
||||
std::vector<Meteor> meteors;
|
||||
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
||||
|
||||
while (window.isOpen()) {
|
||||
window.clear();
|
||||
|
||||
@@ -80,23 +70,15 @@ int main()
|
||||
// TODO: Kolizje
|
||||
// Generate a new meteor at a random position at the top of the screen
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M)) {
|
||||
int randomX = dis(gen);
|
||||
meteors.emplace_back(randomX, -100, meteorTexture);
|
||||
plansza.spawn_meteor();
|
||||
}
|
||||
|
||||
// Update and draw meteors
|
||||
for (auto& meteor : meteors) {
|
||||
for (auto& meteor : plansza.getMeteors()) {
|
||||
meteor.update();
|
||||
window.draw(meteor.getSprite());
|
||||
}
|
||||
|
||||
// Remove meteors that are out of bounds
|
||||
for (auto& meteor : meteors) {
|
||||
if(meteor.getStatus()) {
|
||||
meteors.erase(meteors.begin());
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& bullet : ship.getBullets()) {
|
||||
bullet.update();
|
||||
window.draw(bullet.getSprite());
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
#include <iostream>
|
||||
#include "../headers/Meteor.h"
|
||||
|
||||
Meteor::Meteor(int x, int y, sf::Texture &texture) {
|
||||
position.x = x;
|
||||
position.y = y;
|
||||
Meteor::Meteor(float x, float y, sf::Texture &texture) {
|
||||
outOfBounds = false;
|
||||
meteorTexture = texture;
|
||||
meteorSprite.setTexture(texture);
|
||||
meteorSpeed = 10.0f;
|
||||
meteorSprite.setPosition(x, y);
|
||||
meteorSpeed = 10.0f;
|
||||
meteorSprite.scale(0.05f, 0.05f);
|
||||
meteorPosition.x = x;
|
||||
meteorPosition.y = y;
|
||||
}
|
||||
|
||||
sf::Sprite &Meteor::getSprite() {
|
||||
@@ -18,8 +17,8 @@ sf::Sprite &Meteor::getSprite() {
|
||||
|
||||
void Meteor::update() {
|
||||
meteorSprite.move(0.0f, meteorSpeed);
|
||||
position.y += int(meteorSpeed);
|
||||
if(position.y > 900) {
|
||||
meteorPosition.y += int(meteorSpeed);
|
||||
if(meteorPosition.y > 900) {
|
||||
outOfBounds = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,29 @@
|
||||
#include <random>
|
||||
#include "../headers/Plansza.h"
|
||||
|
||||
Plansza::Plansza(int windowHeight, int windowWidth) {
|
||||
size.height = windowHeight;
|
||||
size.width = windowWidth;
|
||||
meteorTexture.loadFromFile("../assets/img/meteor.png");
|
||||
}
|
||||
|
||||
void Plansza::spawn_meteor() {
|
||||
meteors.emplace_back(random.getRandomNumber(), 100, meteorTexture);
|
||||
}
|
||||
|
||||
Plansza::Size Plansza::getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
std::vector<Meteor> &Plansza::getMeteors() {
|
||||
return meteors;
|
||||
}
|
||||
|
||||
void Plansza::update_meteors() {
|
||||
// Remove meteors that are out of bounds
|
||||
for (auto& meteor : meteors) {
|
||||
if(meteor.getStatus()) {
|
||||
meteors.erase(meteors.begin());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user