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

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