This repository has been archived on 2025-06-06. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LotoStatek/sources/Player.cpp
2024-12-10 20:20:05 +01:00

86 lines
2.3 KiB
C++

#include "../headers/Player.h"
#include <iostream>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Text.hpp>
#include "../headers/Bullet.h"
Player::Player(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture, const sf::Texture& rocketTexture) : Actor(x, y, texture), bulletTexture(bulletTexture), rocketTexture(rocketTexture) {
};
void Player::setTextures(const sf::Texture& shipTexture, const sf::Texture& bulletTexture, const sf::Texture& rocketTexture) {
this->actorSprite.setTexture(shipTexture); // Poprawiona nazwa - actorSprite zamiast shipSprite
this->bulletTexture = bulletTexture;
this->rocketTexture = rocketTexture;
}
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::takeDamage() {
if (health > 0) {
health--;
std::cout << "Player hit! Remaining health: " << health << "\n";
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::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;
}