Meteoryty są spawnowane automatycznie co 1 sekunde, max 5 meteorytów na plansze

This commit is contained in:
2024-11-20 14:57:40 +01:00
parent 1cd9f9b950
commit f1a3449d47
6 changed files with 32 additions and 12 deletions

View File

@@ -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";
//}

View File

@@ -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());