Statek miga

przy uderzeniu w meteoryt
This commit is contained in:
2024-12-06 15:16:57 +01:00
parent 2f9e0ba236
commit 71f8ebe285
4 changed files with 30 additions and 2 deletions

View File

@@ -17,12 +17,17 @@ public:
void moveRight() override;
void moveUp() override;
void moveDown() override;
void onHit();
void update();
std::vector<Rocket>& getRockets();
private:
std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now();
std::vector<Rocket> rockets;
sf::Texture rocketTexture;
sf::Texture bulletTexture;
bool isBlinking = false;
sf::Color originalColor;
};

View File

@@ -29,7 +29,7 @@ unsigned int Actor::getHP() {
}
void Actor::dealDamage() {
if(damageDealClock.getElapsedTime().asSeconds() > 1) {
if(damageDealClock.getElapsedTime().asSeconds() > 1.5) {
if(hp > 0) {
hp--;
}

View File

@@ -105,13 +105,14 @@ void Plansza::update() {
update_hearts();
ship.updateBullets();
ship.update();
window->draw(ship.getSprite());
// trochę dziwny sposób ale jednak działa
for (auto &meteor: meteors) {
if (ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
ship.dealDamage();
ship.onHit();
}
}

View File

@@ -7,6 +7,7 @@ Player::Player(int x, int y, std::string path) : Actor(x, y, std::move(path)) {
rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png");
hp = 3;
damageDealClock.restart();
originalColor = actorSprite.getColor();
};
void Player::shoot() {
@@ -25,6 +26,27 @@ void Player::alternate_shoot() {
}
}
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;
}