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();
|
||||
bool getStatus();
|
||||
void update();
|
||||
~Meteor();
|
||||
private:
|
||||
sf::Texture meteorTexture;
|
||||
sf::Sprite meteorSprite;
|
||||
Position meteorPosition;
|
||||
float meteorSpeed;
|
||||
bool outOfBounds;
|
||||
static unsigned int counter;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "Meteor.h"
|
||||
#include "RandomNumberGenerator.h"
|
||||
#include "SFML/System/Clock.hpp"
|
||||
|
||||
class Plansza {
|
||||
struct Size {
|
||||
@@ -21,6 +22,8 @@ private:
|
||||
Size size;
|
||||
sf::Texture meteorTexture;
|
||||
RandomNumberGenerator random;
|
||||
unsigned int meteorsCounter;
|
||||
sf::Clock spawnClock;
|
||||
};
|
||||
|
||||
#endif //PLANSZA_H
|
||||
|
||||
@@ -10,7 +10,7 @@ private:
|
||||
std::uniform_int_distribution<> dis;
|
||||
|
||||
public:
|
||||
RandomNumberGenerator() : gen(rd()), dis(0, 599) {}
|
||||
RandomNumberGenerator() : gen(rd()), dis(0, 499) {}
|
||||
|
||||
int getRandomNumber() {
|
||||
return dis(gen);
|
||||
|
||||
10
main.cpp
10
main.cpp
@@ -1,5 +1,4 @@
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
#include "SFML/Graphics.hpp"
|
||||
#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::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
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M)) {
|
||||
plansza.spawn_meteor();
|
||||
}
|
||||
plansza.spawn_meteor();
|
||||
|
||||
|
||||
// Update and draw meteors
|
||||
for (auto& meteor : plansza.getMeteors()) {
|
||||
@@ -84,6 +79,7 @@ int main()
|
||||
window.draw(bullet.getSprite());
|
||||
}
|
||||
|
||||
plansza.update_meteors();
|
||||
ship.updateBullets();
|
||||
window.draw(ship.getSprite());
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <iostream>
|
||||
#include "../headers/Meteor.h"
|
||||
|
||||
Meteor::Meteor(float x, float y, sf::Texture &texture) {
|
||||
@@ -5,7 +6,7 @@ Meteor::Meteor(float x, float y, sf::Texture &texture) {
|
||||
meteorTexture = texture;
|
||||
meteorSprite.setTexture(texture);
|
||||
meteorSprite.setPosition(x, y);
|
||||
meteorSpeed = 10.0f;
|
||||
meteorSpeed = 5.0f;
|
||||
meteorSprite.scale(0.05f, 0.05f);
|
||||
meteorPosition.x = x;
|
||||
meteorPosition.y = y;
|
||||
@@ -18,7 +19,7 @@ sf::Sprite &Meteor::getSprite() {
|
||||
void Meteor::update() {
|
||||
meteorSprite.move(0.0f, meteorSpeed);
|
||||
meteorPosition.y += int(meteorSpeed);
|
||||
if(meteorPosition.y > 900) {
|
||||
if(meteorPosition.y > 800) {
|
||||
outOfBounds = true;
|
||||
}
|
||||
}
|
||||
@@ -27,3 +28,11 @@ bool Meteor::getStatus() {
|
||||
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 <iostream>
|
||||
#include "../headers/Plansza.h"
|
||||
|
||||
Plansza::Plansza(int windowHeight, int windowWidth) {
|
||||
size.height = windowHeight;
|
||||
size.width = windowWidth;
|
||||
meteorsCounter = 0;
|
||||
meteorTexture.loadFromFile("../assets/img/meteor.png");
|
||||
spawnClock.restart();
|
||||
}
|
||||
|
||||
// TODO: Meteory na jednym poziomie ze statkiem
|
||||
// TODO: Kolizje
|
||||
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() {
|
||||
@@ -20,7 +30,7 @@ std::vector<Meteor> &Plansza::getMeteors() {
|
||||
}
|
||||
|
||||
void Plansza::update_meteors() {
|
||||
// Remove meteors that are out of bounds
|
||||
// usuwanie meteorów które wyleciały poza ekran
|
||||
for (auto& meteor : meteors) {
|
||||
if(meteor.getStatus()) {
|
||||
meteors.erase(meteors.begin());
|
||||
|
||||
Reference in New Issue
Block a user