Merge remote-tracking branch 'origin/serduszka'
# Conflicts: # CMakeLists.txt # headers/Actor.h # headers/Plansza.h # headers/Player.h # sources/Plansza.cpp # sources/Player.cpp
This commit is contained in:
@@ -21,6 +21,25 @@ Position Actor::getPosition() {
|
||||
return {position.x, position.y};
|
||||
}
|
||||
|
||||
unsigned int Actor::getHP() {
|
||||
return hp;
|
||||
}
|
||||
|
||||
void Actor::dealDamage() {
|
||||
if(damageDealClock.getElapsedTime().asSeconds() > 1.5) {
|
||||
if(hp > 0) {
|
||||
hp--;
|
||||
}
|
||||
damageDealClock.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void Actor::healUP() {
|
||||
if(hp < 3){
|
||||
hp++;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Bullet> &Actor::getBullets() {
|
||||
return bullets;
|
||||
}
|
||||
@@ -39,4 +58,4 @@ void Actor::updateBullets() {
|
||||
|
||||
void Actor::setMovingSpeed(float speed) {
|
||||
moving_speed = speed;
|
||||
}
|
||||
}
|
||||
23
sources/Heart.cpp
Normal file
23
sources/Heart.cpp
Normal 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ą
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,20 @@ Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::Render
|
||||
audioManager.loadSoundEffect("shoot_alt", "../assets/sounds/shoot_alt.ogg");
|
||||
audioManager.loadSoundEffect("fail", "../assets/sounds/fail.mp3");
|
||||
|
||||
meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png");
|
||||
meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png");
|
||||
|
||||
heartTexture.loadFromFile("../assets/img/hearts/heart.png");
|
||||
heartTextureGray.loadFromFile("../assets/img/hearts/heart_gray.png");
|
||||
|
||||
heartStats.emplace_back(sf::Sprite(heartTexture));
|
||||
heartStats.emplace_back(sf::Sprite(heartTexture));
|
||||
heartStats.emplace_back(sf::Sprite(heartTexture));
|
||||
heartStats[0].setPosition(565, 10);
|
||||
heartStats[1].setPosition(530, 10);
|
||||
heartStats[2].setPosition(495, 10);
|
||||
|
||||
meteorSpawnClock.restart();
|
||||
spawn_player();
|
||||
|
||||
spawnClock.restart();
|
||||
@@ -88,19 +102,26 @@ void Plansza::update() {
|
||||
update_score(); // naliczanie punktów
|
||||
// Sprawnowanie wszystkich rodzajów wrogów
|
||||
spawn_meteor();
|
||||
spawn_hearts();
|
||||
spawn_enemy();
|
||||
spawn_advanced_enemy();
|
||||
spawn_wiazkowiec();
|
||||
spawn_bomber();
|
||||
spawn_kamikadze();
|
||||
|
||||
|
||||
|
||||
|
||||
// utrzymanie meteorów i pocisków w ruchu
|
||||
for (auto &meteor: getMeteors()) {
|
||||
for (auto &meteor: meteors) {
|
||||
meteor.update();
|
||||
window->draw(meteor.getSprite());
|
||||
}
|
||||
|
||||
for (auto &bullet: ship->getBullets()) {
|
||||
for (auto &heart: hearts) {
|
||||
heart.update();
|
||||
window->draw(heart.getSprite());
|
||||
}for (auto &bullet: ship->getBullets()) {
|
||||
bullet.update();
|
||||
window->draw(bullet.getSprite());
|
||||
}
|
||||
@@ -112,12 +133,64 @@ void Plansza::update() {
|
||||
|
||||
// Sprawdzenie czy meteory i pociski są poza granicami ekranu
|
||||
update_meteors();
|
||||
update_hearts();
|
||||
ship->updateBullets();
|
||||
|
||||
ship.update();
|
||||
window->draw(ship->getSprite());
|
||||
|
||||
for (auto &meteor: getMeteors()) {
|
||||
for (auto &meteor: meteors) {
|
||||
if (ship->getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
|
||||
ship.onHit();
|
||||
}
|
||||
}
|
||||
|
||||
for (auto heartIt = hearts.begin(); heartIt != hearts.end();) {
|
||||
if (ship.getSprite().getGlobalBounds().intersects(heartIt->getSprite().getGlobalBounds())) {
|
||||
ship.healUP();
|
||||
heartIt = hearts.erase(heartIt);
|
||||
} else {
|
||||
++heartIt;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end();) {
|
||||
bool meteorHit = false;
|
||||
for (auto rocketIt = ship.getBullets().begin(); rocketIt != ship.getBullets().end();) {
|
||||
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
||||
ship.getBullets().erase(rocketIt);
|
||||
meteorIt = getMeteors().erase(meteorIt);
|
||||
meteorHit = true;
|
||||
break;
|
||||
} else {
|
||||
++rocketIt;
|
||||
}
|
||||
}
|
||||
if (!meteorHit) {
|
||||
++meteorIt;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end();) {
|
||||
bool meteorHit = false;
|
||||
for (auto rocketIt = ship.getRockets().begin(); rocketIt != ship.getRockets().end();) {
|
||||
if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) {
|
||||
ship.getRockets().erase(rocketIt);
|
||||
meteorIt = getMeteors().erase(meteorIt);
|
||||
meteorHit = true;
|
||||
break;
|
||||
} else {
|
||||
++rocketIt;
|
||||
}
|
||||
}
|
||||
if (!meteorHit) {
|
||||
++meteorIt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Warunek przeganej
|
||||
if (gameOver) {
|
||||
ship->takeDamage();
|
||||
}
|
||||
}
|
||||
@@ -588,8 +661,36 @@ void Plansza::update() {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
// Statystyka życia gracza (wyświetlanie)
|
||||
switch (ship.getHP()) {
|
||||
case 0:
|
||||
heartStats[0].setTexture(heartTextureGray);
|
||||
heartStats[1].setTexture(heartTextureGray);
|
||||
heartStats[2].setTexture(heartTextureGray);
|
||||
gameOver = true;
|
||||
break;
|
||||
case 1:
|
||||
heartStats[0].setTexture(heartTexture);
|
||||
heartStats[1].setTexture(heartTextureGray);
|
||||
heartStats[2].setTexture(heartTextureGray);
|
||||
break;
|
||||
case 2:
|
||||
heartStats[0].setTexture(heartTexture);
|
||||
heartStats[1].setTexture(heartTexture);
|
||||
heartStats[2].setTexture(heartTextureGray);
|
||||
break;
|
||||
case 3:
|
||||
heartStats[0].setTexture(heartTexture);
|
||||
heartStats[1].setTexture(heartTexture);
|
||||
heartStats[2].setTexture(heartTexture);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
for (const auto& heart: heartStats) {
|
||||
window->draw(heart);
|
||||
}
|
||||
}
|
||||
// Meteor-related niżej
|
||||
|
||||
|
||||
@@ -603,7 +704,7 @@ void Plansza::update_meteors() {
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -611,7 +712,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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -673,6 +783,15 @@ void Plansza::spawn_wiazkowiec() {
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <utility>
|
||||
|
||||
#include "../headers/Player.h"
|
||||
|
||||
#include <iostream>
|
||||
@@ -19,6 +21,9 @@ Player* Player::getInstance(int x, int y, const sf::Texture& texture) {
|
||||
void Player::loadTexture() {
|
||||
bulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png");
|
||||
rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png");
|
||||
hp = 3;
|
||||
damageDealClock.restart();
|
||||
originalColor = actorSprite.getColor();
|
||||
}
|
||||
|
||||
void Player::shoot() {
|
||||
@@ -79,6 +84,27 @@ bool Player::isAlive() const {
|
||||
return health > 0;
|
||||
}
|
||||
|
||||
void Player::onHit() {
|
||||
dealDamage();
|
||||
isBlinking = true;
|
||||
}
|
||||
|
||||
void Player::update() {
|
||||
if (isBlinking) {
|
||||
auto elapsed = damageDealClock.getElapsedTime().asMilliseconds();
|
||||
if (elapsed < 1000) { // miganie przez 1 sekundę
|
||||
if ((elapsed / 100) % 2 == 0) {
|
||||
actorSprite.setColor(sf::Color(255, 255, 255, 128)); // półprzeźroczysty
|
||||
} else {
|
||||
actorSprite.setColor(originalColor); // oryginalny kolor
|
||||
}
|
||||
} else {
|
||||
isBlinking = false;
|
||||
actorSprite.setColor(originalColor); // przywróć oryginalny kolor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Player::setFirerate(unsigned int firerate) {
|
||||
this->firerate = firerate;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user