Added heart and spawn of them

This commit is contained in:
2024-12-05 14:52:00 +01:00
parent b6830c305b
commit 3b637508e1
5 changed files with 81 additions and 6 deletions

View File

@@ -27,7 +27,9 @@ add_executable(LotoStatek main.cpp
headers/Size.h
headers/Position.h
headers/ObjectItem.hpp
sources/ObjectItem.cpp)
sources/ObjectItem.cpp
headers/Heart.hpp
sources/Heart.cpp)
if(WIN32)
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/lib/SFML")

14
headers/Heart.hpp Normal file
View File

@@ -0,0 +1,14 @@
#ifndef LOTOSTATEK_HEART_HPP
#define LOTOSTATEK_HEART_HPP
#include "SFML/Graphics/Texture.hpp"
#include "SFML/Graphics/Sprite.hpp"
#include "ObjectItem.hpp"
class Heart : public ObjectItem {
public:
Heart(float x, float y, sf::Texture &texture);
void update();
};
#endif //LOTOSTATEK_HEART_HPP

View File

@@ -12,6 +12,7 @@
#include "AudioManager.h"
#include "Meteor.h"
#include "Plansza.h"
#include "Heart.hpp"
class Plansza {
public:
@@ -19,7 +20,9 @@ public:
Size getSize();
std::vector<Meteor> &getMeteors();
void spawn_meteor();
void spawn_hearts();
void update_meteors();
void update_hearts();
void update();
private:
Size size;
@@ -28,9 +31,13 @@ private:
AudioManager audioManager;
sf::Texture meteorTexture1;
sf::Texture meteorTexture2;
sf::Clock spawnClock;
sf::Texture heartTexture;
sf::Clock meteorSpawnClock;
sf::Clock heartSpawnClock;
std::vector<Meteor> meteors;
std::vector<Heart> hearts;
sf::RenderWindow *window;
};
#endif //PLANSZA_H

23
sources/Heart.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "../headers/Heart.hpp"
Heart::Heart(float x, float y, sf::Texture &texture) : ObjectItem(x,y,texture) {
outOfBounds = false;
texture = texture;
sprite.setTexture(texture);
sprite.setOrigin(sprite.getLocalBounds().width / 2, sprite.getLocalBounds().height / 2); // wycentrowanie sprite
sprite.setPosition(x, y);
movingSpeed = 3.0f;
// sprite.scale(0.05f, 0.05f);
position.x = x;
position.y = y;
rotationSpeed = 0;
}
void Heart::update() {
sprite.move(0.0f, movingSpeed); // przesunięcie sprajta
position.y += int(movingSpeed); // przesunięcie pozycji
// sprite.rotate(rotationSpeed); // obracanie tym meteorkiem pięknym
if(position.y > 900) {
outOfBounds = true; // jeżeli wyszedł poza granice ekranu ustaw tą zmienną
}
}

View File

@@ -24,7 +24,10 @@ ship(static_cast<int>(mainWindow->getSize().x) / 2, static_cast<int>(mainWindow-
meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png");
meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png");
spawnClock.restart();
heartTexture.loadFromFile("../assets/img/hearts/heart.png");
meteorSpawnClock.restart();
}
@@ -67,14 +70,20 @@ void Plansza::update() {
// generowanie nowego meteoru
spawn_meteor();
spawn_hearts();
// utrzymanie meteorów i pocisków w ruchu
for (auto& meteor : getMeteors()) {
for (auto& meteor : meteors) {
meteor.update();
window->draw(meteor.getSprite());
}
for (auto& heart : hearts) {
heart.update();
window->draw(heart.getSprite());
}
for (auto& bullet : ship.getBullets()) {
bullet.update();
window->draw(bullet.getSprite());
@@ -87,10 +96,12 @@ void Plansza::update() {
// Sprawdzenie czy meteory i pociski są poza granicami ekranu
update_meteors();
update_hearts();
ship.updateBullets();
window->draw(ship.getSprite());
// trochę dziwny sposób ale jednak działa
for (auto& meteor : getMeteors()) {
if(ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
@@ -162,7 +173,7 @@ void Plansza::update() {
// Meteor-related niżej
void Plansza::spawn_meteor() {
if (spawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy
if (meteorSpawnClock.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
if(rand() % 2 == 1) {
meteors.emplace_back(RandomNumberGenerator::getRandomNumber(50,499), -100, meteorTexture2);
@@ -170,7 +181,16 @@ void Plansza::spawn_meteor() {
meteors.emplace_back(RandomNumberGenerator::getRandomNumber(50,499), -100, meteorTexture1);
}
}
spawnClock.restart();
meteorSpawnClock.restart();
}
}
void Plansza::spawn_hearts() {
if (heartSpawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy
if (hearts.size() < 5) { // jeśli jest mniej niż 5 meteorów na planszy
hearts.emplace_back(RandomNumberGenerator::getRandomNumber(50, 499), -100, heartTexture);
}
heartSpawnClock.restart();
}
}
@@ -183,6 +203,15 @@ void Plansza::update_meteors() {
}
}
void Plansza::update_hearts() {
// usuwanie serduszek które wyleciały poza ekran
for (auto& heart : hearts) {
if(heart.getStatus()) {
hearts.erase(hearts.begin());
}
}
}
Size Plansza::getSize() {
return size;
}