Meteoryty są spawnowane automatycznie co 1 sekunde, max 5 meteorytów na plansze
This commit is contained in:
@@ -15,12 +15,14 @@ public:
|
|||||||
sf::Sprite &getSprite();
|
sf::Sprite &getSprite();
|
||||||
bool getStatus();
|
bool getStatus();
|
||||||
void update();
|
void update();
|
||||||
|
~Meteor();
|
||||||
private:
|
private:
|
||||||
sf::Texture meteorTexture;
|
sf::Texture meteorTexture;
|
||||||
sf::Sprite meteorSprite;
|
sf::Sprite meteorSprite;
|
||||||
Position meteorPosition;
|
Position meteorPosition;
|
||||||
float meteorSpeed;
|
float meteorSpeed;
|
||||||
bool outOfBounds;
|
bool outOfBounds;
|
||||||
|
static unsigned int counter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "Meteor.h"
|
#include "Meteor.h"
|
||||||
#include "RandomNumberGenerator.h"
|
#include "RandomNumberGenerator.h"
|
||||||
|
#include "SFML/System/Clock.hpp"
|
||||||
|
|
||||||
class Plansza {
|
class Plansza {
|
||||||
struct Size {
|
struct Size {
|
||||||
@@ -21,6 +22,8 @@ private:
|
|||||||
Size size;
|
Size size;
|
||||||
sf::Texture meteorTexture;
|
sf::Texture meteorTexture;
|
||||||
RandomNumberGenerator random;
|
RandomNumberGenerator random;
|
||||||
|
unsigned int meteorsCounter;
|
||||||
|
sf::Clock spawnClock;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //PLANSZA_H
|
#endif //PLANSZA_H
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ private:
|
|||||||
std::uniform_int_distribution<> dis;
|
std::uniform_int_distribution<> dis;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RandomNumberGenerator() : gen(rd()), dis(0, 599) {}
|
RandomNumberGenerator() : gen(rd()), dis(0, 499) {}
|
||||||
|
|
||||||
int getRandomNumber() {
|
int getRandomNumber() {
|
||||||
return dis(gen);
|
return dis(gen);
|
||||||
|
|||||||
10
main.cpp
10
main.cpp
@@ -1,5 +1,4 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <random>
|
|
||||||
|
|
||||||
#include "SFML/Graphics.hpp"
|
#include "SFML/Graphics.hpp"
|
||||||
#include "headers/Player.h"
|
#include "headers/Player.h"
|
||||||
@@ -65,13 +64,9 @@ int main()
|
|||||||
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) ship.shoot();
|
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) ship.shoot();
|
||||||
if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) ship.alternate_shoot();
|
if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) ship.alternate_shoot();
|
||||||
|
|
||||||
|
|
||||||
// TODO: Meteory na jednym poziomie ze statkiem
|
|
||||||
// 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)) {
|
plansza.spawn_meteor();
|
||||||
plansza.spawn_meteor();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update and draw meteors
|
// Update and draw meteors
|
||||||
for (auto& meteor : plansza.getMeteors()) {
|
for (auto& meteor : plansza.getMeteors()) {
|
||||||
@@ -84,6 +79,7 @@ int main()
|
|||||||
window.draw(bullet.getSprite());
|
window.draw(bullet.getSprite());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
plansza.update_meteors();
|
||||||
ship.updateBullets();
|
ship.updateBullets();
|
||||||
window.draw(ship.getSprite());
|
window.draw(ship.getSprite());
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <iostream>
|
||||||
#include "../headers/Meteor.h"
|
#include "../headers/Meteor.h"
|
||||||
|
|
||||||
Meteor::Meteor(float x, float y, sf::Texture &texture) {
|
Meteor::Meteor(float x, float y, sf::Texture &texture) {
|
||||||
@@ -5,7 +6,7 @@ Meteor::Meteor(float x, float y, sf::Texture &texture) {
|
|||||||
meteorTexture = texture;
|
meteorTexture = texture;
|
||||||
meteorSprite.setTexture(texture);
|
meteorSprite.setTexture(texture);
|
||||||
meteorSprite.setPosition(x, y);
|
meteorSprite.setPosition(x, y);
|
||||||
meteorSpeed = 10.0f;
|
meteorSpeed = 5.0f;
|
||||||
meteorSprite.scale(0.05f, 0.05f);
|
meteorSprite.scale(0.05f, 0.05f);
|
||||||
meteorPosition.x = x;
|
meteorPosition.x = x;
|
||||||
meteorPosition.y = y;
|
meteorPosition.y = y;
|
||||||
@@ -18,7 +19,7 @@ sf::Sprite &Meteor::getSprite() {
|
|||||||
void Meteor::update() {
|
void Meteor::update() {
|
||||||
meteorSprite.move(0.0f, meteorSpeed);
|
meteorSprite.move(0.0f, meteorSpeed);
|
||||||
meteorPosition.y += int(meteorSpeed);
|
meteorPosition.y += int(meteorSpeed);
|
||||||
if(meteorPosition.y > 900) {
|
if(meteorPosition.y > 800) {
|
||||||
outOfBounds = true;
|
outOfBounds = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,3 +28,11 @@ bool Meteor::getStatus() {
|
|||||||
return outOfBounds;
|
return outOfBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int Meteor::counter = 0;
|
||||||
|
|
||||||
|
// było użyte do testowania czy meteoryt jest kasowany
|
||||||
|
//Meteor::~Meteor() {
|
||||||
|
// Meteor::counter++;
|
||||||
|
// std::clog << Meteor::counter << " Meteor destroyed\n";
|
||||||
|
//}
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,24 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
|
#include <iostream>
|
||||||
#include "../headers/Plansza.h"
|
#include "../headers/Plansza.h"
|
||||||
|
|
||||||
Plansza::Plansza(int windowHeight, int windowWidth) {
|
Plansza::Plansza(int windowHeight, int windowWidth) {
|
||||||
size.height = windowHeight;
|
size.height = windowHeight;
|
||||||
size.width = windowWidth;
|
size.width = windowWidth;
|
||||||
|
meteorsCounter = 0;
|
||||||
meteorTexture.loadFromFile("../assets/img/meteor.png");
|
meteorTexture.loadFromFile("../assets/img/meteor.png");
|
||||||
|
spawnClock.restart();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Meteory na jednym poziomie ze statkiem
|
||||||
|
// TODO: Kolizje
|
||||||
void Plansza::spawn_meteor() {
|
void Plansza::spawn_meteor() {
|
||||||
meteors.emplace_back(random.getRandomNumber(), 100, meteorTexture);
|
if (spawnClock.getElapsedTime().asSeconds() > 1.0f) { // spawn co 1 sekunde
|
||||||
|
if (meteors.size() < 5) { // jeśli jest mniej niż 5 meteorów na planszy
|
||||||
|
meteors.emplace_back(random.getRandomNumber(), -100, meteorTexture);
|
||||||
|
}
|
||||||
|
spawnClock.restart();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Plansza::Size Plansza::getSize() {
|
Plansza::Size Plansza::getSize() {
|
||||||
@@ -20,7 +30,7 @@ std::vector<Meteor> &Plansza::getMeteors() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Plansza::update_meteors() {
|
void Plansza::update_meteors() {
|
||||||
// Remove meteors that are out of bounds
|
// usuwanie meteorów które wyleciały poza ekran
|
||||||
for (auto& meteor : meteors) {
|
for (auto& meteor : meteors) {
|
||||||
if(meteor.getStatus()) {
|
if(meteor.getStatus()) {
|
||||||
meteors.erase(meteors.begin());
|
meteors.erase(meteors.begin());
|
||||||
|
|||||||
Reference in New Issue
Block a user