#include #include #include "../headers/Player.h" #include "../headers/Plansza.h" Player::Player(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) { hp = 3; if(Plansza::selectedShip != none) { actorSprite.setScale(0.20f, 0.20f); } } 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() { try { bulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png"); rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png"); } catch (std::exception &e) { std::cerr << "Failed to load textures: " << e.what() << std::endl; exit(-500); } damageDealClock.restart(); originalColor = actorSprite.getColor(); } void Player::shoot() { auto now = std::chrono::steady_clock::now(); if (std::chrono::duration_cast(now - lastShotTime).count() >= firerate) { if (tripleShot == true) { bullets.emplace_back(position.x - 40, position.y, bulletTexture, bulletSpeed); bullets.emplace_back(position.x, position.y, bulletTexture, bulletSpeed); bullets.emplace_back(position.x + 40, position.y, bulletTexture, bulletSpeed); lastShotTime = now; } if (tripleShot == false) { bullets.emplace_back(position.x, position.y, bulletTexture, bulletSpeed); lastShotTime = now; } } } void Player::alternate_shoot() { auto now = std::chrono::steady_clock::now(); if (std::chrono::duration_cast(now - lastShotTime).count() >= firerate) { rockets.emplace_back(position.x, position.y, rocketTexture).getSprite().scale(1.5f, 1.5f); lastShotTime = now; } } void Player::ultimate_shoot() { ultimateShootActive = true; } 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(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 (hp > 0) { hp--; isImmortal = true; // Aktywuj chwilową nieśmiertelność immortalityClock.restart(); } } } void Player::setTripleShot(bool toogle) { tripleShot = toogle; } 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(moving_speed); } void Player::moveRight() { move(moving_speed, 0.0f); position.x += static_cast(moving_speed); } void Player::moveUp() { move(0.0f, -moving_speed); position.y -= static_cast(moving_speed); } void Player::moveDown() { move(0.0f, moving_speed); position.y += static_cast(moving_speed); } std::vector &Player::getRockets() { return rockets; } void Player::setBulletSpeed(float speed) { this->bulletSpeed = speed; } Player* Player::player_ = nullptr; bool Player::getUltimateStatus() { return ultimateShootActive; } void Player::setUltimateStatus(bool status) { ultimateShootActive = status; }