# Conflicts: # CMakeLists.txt # headers/Actor.h # headers/Plansza.h # headers/Player.h # sources/Plansza.cpp # sources/Player.cpp
141 lines
3.8 KiB
C++
141 lines
3.8 KiB
C++
#include <utility>
|
|
|
|
#include "../headers/Player.h"
|
|
|
|
#include <iostream>
|
|
#include <SFML/Graphics/Font.hpp>
|
|
#include <SFML/Graphics/RenderWindow.hpp>
|
|
#include <SFML/Graphics/Text.hpp>
|
|
|
|
Player::Player(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) {
|
|
|
|
}
|
|
|
|
Player* Player::getInstance(int x, int y, const sf::Texture& texture) {
|
|
if (player_ == nullptr) {
|
|
player_ = new Player(x, y, texture);
|
|
}
|
|
return player_;
|
|
}
|
|
|
|
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() {
|
|
auto now = std::chrono::steady_clock::now();
|
|
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastShotTime).count() >= firerate) {
|
|
bullets.emplace_back(position.x, position.y, bulletTexture);
|
|
lastShotTime = now;
|
|
}
|
|
}
|
|
|
|
|
|
void Player::alternate_shoot() {
|
|
auto now = std::chrono::steady_clock::now();
|
|
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastShotTime).count() >= firerate) {
|
|
rockets.emplace_back(position.x, position.y, rocketTexture).getSprite().scale(1.5f, 1.5f);
|
|
lastShotTime = now;
|
|
}
|
|
}
|
|
|
|
void Player::update() {
|
|
// Wyłącz nieśmiertelność po określonym czasie
|
|
if (isImmortal && immortalityClock.getElapsedTime().asSeconds() >= immortalityDuration) {
|
|
isImmortal = false;
|
|
std::cout << "Immortality ended.\n";
|
|
}
|
|
|
|
// Efekt migania podczas nieśmiertelności
|
|
if (isImmortal) {
|
|
if (static_cast<int>(immortalityClock.getElapsedTime().asMilliseconds() / 200) % 2 == 0) {
|
|
actorSprite.setColor(sf::Color(255, 255, 255, 128)); // Półprzezroczysty
|
|
} else {
|
|
actorSprite.setColor(sf::Color(255, 255, 255, 255)); // Normalny
|
|
}
|
|
} else {
|
|
actorSprite.setColor(sf::Color(255, 255, 255, 255)); // Normalny
|
|
}
|
|
}
|
|
|
|
void Player::takeDamage() {
|
|
if (!isImmortal) {
|
|
if (health > 0) {
|
|
health--;
|
|
std::cout << "Player hit! Remaining health: " << health << "\n";
|
|
isImmortal = true; // Aktywuj chwilową nieśmiertelność
|
|
immortalityClock.restart();
|
|
|
|
|
|
if (health <= 0) {
|
|
std::cout << "Player has been destroyed!\n";
|
|
std::cout << "You lost the game!\n";
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
void Player::move(float deltaX, float deltaY) {
|
|
actorSprite.move(deltaX, deltaY);
|
|
}
|
|
|
|
void Player::moveLeft() {
|
|
move(-moving_speed, 0.0f);
|
|
position.x -= static_cast<int>(moving_speed);
|
|
}
|
|
|
|
void Player::moveRight() {
|
|
move(moving_speed, 0.0f);
|
|
position.x += static_cast<int>(moving_speed);
|
|
}
|
|
|
|
void Player::moveUp() {
|
|
move(0.0f, -moving_speed);
|
|
position.y -= static_cast<int>(moving_speed);
|
|
}
|
|
|
|
void Player::moveDown() {
|
|
move(0.0f, moving_speed);
|
|
position.y += static_cast<int>(moving_speed);
|
|
}
|
|
|
|
std::vector<Rocket> &Player::getRockets() {
|
|
return rockets;
|
|
}
|
|
|
|
Player* Player::player_ = nullptr;
|