Multiple fixes

Obracające się meteoryty, koniec gry przy natrafieniu na meteoryt.
Poprawione sprajty statku, meteorytu i pocisku
This commit is contained in:
2024-11-21 15:50:25 +01:00
parent b16fd9fad5
commit 2bdfbb193c
9 changed files with 42 additions and 16 deletions

View File

@@ -31,9 +31,7 @@ public:
void updateBullets();
void setMovingSpeed(float speed) {
moving_speed = speed;
}
void setMovingSpeed(float speed);
protected:
sf::Sprite actorSprite;

View File

@@ -20,6 +20,7 @@ private:
sf::Texture meteorTexture;
sf::Sprite meteorSprite;
Position meteorPosition;
float meteorRotationSpeed;
float meteorSpeed;
bool outOfBounds;
static unsigned int counter;

View File

@@ -12,7 +12,7 @@ class Plansza {
int width;
};
public:
Plansza(int windowHeight, int windowWidth);
Plansza(unsigned int windowHeight, unsigned int windowWidth);
void spawn_meteor();
Size getSize();
std::vector<Meteor> &getMeteors();

View File

@@ -17,7 +17,8 @@ int main()
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła
Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
// TODO: Przenieść tworzenie statku wewnątrz klasy Plansza
Player ship(window.getSize().x/2,window.getSize().y - 100, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
ship.setMovingSpeed(8);
ship.setFirerate(200);
@@ -83,6 +84,24 @@ int main()
ship.updateBullets();
window.draw(ship.getSprite());
// trochę dziwny sposób ale jednak działa
for (auto& meteor : plansza.getMeteors()) {
if(ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
std::cout << "You lost the game!\n";
window.close();
exit(-2); // Kod -2 oznacza uderzenie się w meteoryt
}
}
// TODO: Poprawić kolizję pocisku z meteorem, tak aby był kasowany tylko ten konkretny meteoryt
// for (auto meteorIt = plansza.getMeteors().begin(); meteorIt != plansza.getMeteors().end(); ++meteorIt) {
// for (auto& bullet : ship.getBullets()) {
// if (meteorIt->getSprite().getGlobalBounds().intersects(bullet.getSprite().getGlobalBounds())) {
// meteorIt = plansza.getMeteors().erase(meteorIt);
// break; // Exit the inner loop to avoid invalidating the iterator
// }
// }
// }
window.display();
}

View File

@@ -1,10 +1,10 @@
#include "../headers/Actor.h"
// TODO: Naprawić krzywy sprite statku
Actor::Actor(int x, int y, std::string path) {
loadTexture(path);
position.x = x;
position.y = y;
actorSprite.setOrigin(actorSprite.getLocalBounds().width / 2, actorSprite.getLocalBounds().height / 2); // wycentrowanie sprite
actorSprite.setPosition(x, y);
}
@@ -39,3 +39,7 @@ void Actor::updateBullets() {
}
}
}
void Actor::setMovingSpeed(float speed) {
moving_speed = speed;
}

View File

@@ -5,6 +5,7 @@ Bullet::Bullet(float x, float y, sf::Texture &texture) {
outOfBounds = false;
bulletTexture = texture;
bulletSprite.setTexture(texture);
bulletSprite.setOrigin(bulletSprite.getLocalBounds().width/2, bulletSprite.getLocalBounds().height/2);
bulletSprite.setPosition(x, y);
bulletSpeed = -10.0f;
bulletPosition.x = x;

View File

@@ -5,11 +5,13 @@ Meteor::Meteor(float x, float y, sf::Texture &texture) {
outOfBounds = false;
meteorTexture = texture;
meteorSprite.setTexture(texture);
meteorSprite.setOrigin(meteorSprite.getLocalBounds().width / 2, meteorSprite.getLocalBounds().height / 2); // wycentrowanie sprite
meteorSprite.setPosition(x, y);
meteorSpeed = 5.0f;
meteorSprite.scale(0.05f, 0.05f);
meteorPosition.x = x;
meteorPosition.y = y;
meteorRotationSpeed = static_cast<float>(rand() % 2 + 1) * (rand() % 2 == 0 ? 1 : -1);
}
sf::Sprite &Meteor::getSprite() {
@@ -17,11 +19,14 @@ sf::Sprite &Meteor::getSprite() {
}
void Meteor::update() {
meteorSprite.move(0.0f, meteorSpeed);
meteorPosition.y += int(meteorSpeed);
if(meteorPosition.y > 800) {
outOfBounds = true;
meteorSprite.move(0.0f, meteorSpeed); // przesunięcie sprajta
meteorPosition.y += int(meteorSpeed); // przesunięcie pozycji
meteorSprite.rotate(meteorRotationSpeed); // obracanie tym meteorkiem pięknym
if(meteorPosition.y > 900) {
outOfBounds = true; // jeżeli wyszedł poza granice ekranu ustaw tą zmienną
}
// std::cout << "x: " << meteorSprite.getPosition().x << std::endl;
// std::cout << "y: " << meteorSprite.getPosition().y << std::endl;
}
bool Meteor::getStatus() {

View File

@@ -2,17 +2,15 @@
#include <iostream>
#include "../headers/Plansza.h"
Plansza::Plansza(int windowHeight, int windowWidth) {
Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth) {
size.height = windowHeight;
size.width = windowWidth;
meteorTexture.loadFromFile("../assets/img/meteor.png");
spawnClock.restart();
}
// TODO: Meteory na jednym poziomie ze statkiem
// TODO: Kolizje
void Plansza::spawn_meteor() {
if (spawnClock.getElapsedTime().asSeconds() > 1.0f) { // spawn co 1 sekunde
if (spawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy
if (meteors.size() < 5) { // jeśli jest mniej niż 5 meteorów na planszy
meteors.emplace_back(random.getRandomNumber(), -100, meteorTexture);
}

View File

@@ -5,7 +5,7 @@ Player::Player(int x, int y, std::string path) : Actor(x, y, path) {};
void Player::shoot() {
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastShotTime).count() >= firerate) {
bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2-62, position.y, bulletTextureLeft);
bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2, position.y, bulletTextureLeft);
lastShotTime = now;
}
}
@@ -13,7 +13,7 @@ void Player::shoot() {
void Player::alternate_shoot() {
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastShotTime).count() >= firerate) {
bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2-62, position.y, bulletTextureRight);
bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2, position.y, bulletTextureRight);
lastShotTime = now;
}
}