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
|
headers/Bullet.h
|
||||||
sources/Bullet.cpp
|
sources/Bullet.cpp
|
||||||
headers/Meteor.h
|
headers/Meteor.h
|
||||||
sources/Meteor.cpp)
|
sources/Meteor.cpp
|
||||||
|
headers/RandomNumberGenerator.h)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML")
|
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML")
|
||||||
|
|||||||
@@ -12,15 +12,15 @@ class Bullet {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
Bullet(float x, float y, sf::Texture &texture);
|
Bullet(float x, float y, sf::Texture &texture);
|
||||||
void update();
|
sf::Sprite &getSprite();
|
||||||
sf::Sprite& getSprite();
|
|
||||||
void setSpeed(float speed);
|
void setSpeed(float speed);
|
||||||
bool getStatus() const;
|
bool getStatus() const;
|
||||||
|
void update();
|
||||||
private:
|
private:
|
||||||
sf::Sprite bulletSprite;
|
sf::Sprite bulletSprite;
|
||||||
sf::Texture bulletTexture;
|
sf::Texture bulletTexture;
|
||||||
float bulletSpeed;
|
|
||||||
Position bulletPosition;
|
Position bulletPosition;
|
||||||
|
float bulletSpeed;
|
||||||
bool outOfBounds;
|
bool outOfBounds;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,15 +9,16 @@ class Meteor {
|
|||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Meteor(int x, int y, sf::Texture &texture);
|
Meteor(float x, float y, sf::Texture &texture);
|
||||||
sf::Sprite & getSprite();
|
sf::Sprite &getSprite();
|
||||||
bool getStatus();
|
bool getStatus();
|
||||||
void update();
|
void update();
|
||||||
private:
|
private:
|
||||||
sf::Texture meteorTexture;
|
sf::Texture meteorTexture;
|
||||||
sf::Sprite meteorSprite;
|
sf::Sprite meteorSprite;
|
||||||
Position position;
|
Position meteorPosition;
|
||||||
float meteorSpeed;
|
float meteorSpeed;
|
||||||
bool outOfBounds;
|
bool outOfBounds;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -2,11 +2,25 @@
|
|||||||
#define PLANSZA_H
|
#define PLANSZA_H
|
||||||
|
|
||||||
|
|
||||||
|
#include "Meteor.h"
|
||||||
|
#include "RandomNumberGenerator.h"
|
||||||
|
|
||||||
class Plansza {
|
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
|
#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 "SFML/Graphics.hpp"
|
||||||
#include "headers/Player.h"
|
#include "headers/Player.h"
|
||||||
#include "headers/Meteor.h"
|
#include "headers/Meteor.h"
|
||||||
|
#include "headers/Plansza.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -11,15 +12,7 @@ int main()
|
|||||||
sf::RenderWindow window(sf::VideoMode(600, 800), "My window");
|
sf::RenderWindow window(sf::VideoMode(600, 800), "My window");
|
||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
window.setFramerateLimit(60);
|
window.setFramerateLimit(60);
|
||||||
|
Plansza plansza(window.getSize().y, window.getSize().x);
|
||||||
// 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");
|
|
||||||
|
|
||||||
sf::Texture backgroundTexture;
|
sf::Texture backgroundTexture;
|
||||||
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
|
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
|
||||||
@@ -29,9 +22,6 @@ int main()
|
|||||||
ship.setMovingSpeed(8);
|
ship.setMovingSpeed(8);
|
||||||
ship.setFirerate(200);
|
ship.setFirerate(200);
|
||||||
|
|
||||||
std::vector<Meteor> meteors;
|
|
||||||
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
|
||||||
|
|
||||||
while (window.isOpen()) {
|
while (window.isOpen()) {
|
||||||
window.clear();
|
window.clear();
|
||||||
|
|
||||||
@@ -80,23 +70,15 @@ int main()
|
|||||||
// TODO: Kolizje
|
// TODO: Kolizje
|
||||||
// Generate a new meteor at a random position at the top of the screen
|
// Generate a new meteor at a random position at the top of the screen
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M)) {
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M)) {
|
||||||
int randomX = dis(gen);
|
plansza.spawn_meteor();
|
||||||
meteors.emplace_back(randomX, -100, meteorTexture);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update and draw meteors
|
// Update and draw meteors
|
||||||
for (auto& meteor : meteors) {
|
for (auto& meteor : plansza.getMeteors()) {
|
||||||
meteor.update();
|
meteor.update();
|
||||||
window.draw(meteor.getSprite());
|
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()) {
|
for (auto& bullet : ship.getBullets()) {
|
||||||
bullet.update();
|
bullet.update();
|
||||||
window.draw(bullet.getSprite());
|
window.draw(bullet.getSprite());
|
||||||
|
|||||||
@@ -1,15 +1,14 @@
|
|||||||
#include <iostream>
|
|
||||||
#include "../headers/Meteor.h"
|
#include "../headers/Meteor.h"
|
||||||
|
|
||||||
Meteor::Meteor(int x, int y, sf::Texture &texture) {
|
Meteor::Meteor(float x, float y, sf::Texture &texture) {
|
||||||
position.x = x;
|
|
||||||
position.y = y;
|
|
||||||
outOfBounds = false;
|
outOfBounds = false;
|
||||||
meteorTexture = texture;
|
meteorTexture = texture;
|
||||||
meteorSprite.setTexture(texture);
|
meteorSprite.setTexture(texture);
|
||||||
meteorSpeed = 10.0f;
|
|
||||||
meteorSprite.setPosition(x, y);
|
meteorSprite.setPosition(x, y);
|
||||||
|
meteorSpeed = 10.0f;
|
||||||
meteorSprite.scale(0.05f, 0.05f);
|
meteorSprite.scale(0.05f, 0.05f);
|
||||||
|
meteorPosition.x = x;
|
||||||
|
meteorPosition.y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Sprite &Meteor::getSprite() {
|
sf::Sprite &Meteor::getSprite() {
|
||||||
@@ -18,8 +17,8 @@ sf::Sprite &Meteor::getSprite() {
|
|||||||
|
|
||||||
void Meteor::update() {
|
void Meteor::update() {
|
||||||
meteorSprite.move(0.0f, meteorSpeed);
|
meteorSprite.move(0.0f, meteorSpeed);
|
||||||
position.y += int(meteorSpeed);
|
meteorPosition.y += int(meteorSpeed);
|
||||||
if(position.y > 900) {
|
if(meteorPosition.y > 900) {
|
||||||
outOfBounds = true;
|
outOfBounds = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1,29 @@
|
|||||||
|
#include <random>
|
||||||
#include "../headers/Plansza.h"
|
#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