From 2bffc2de0cdb62bf03590121d495e876cf1a0724 Mon Sep 17 00:00:00 2001 From: Kuba Date: Wed, 11 Dec 2024 00:05:53 +0100 Subject: [PATCH] Dziuala 3 strzlowiec --- headers/Actor.h | 4 +++- headers/AdvancedEnemy.h | 2 +- headers/Enemy.h | 2 +- headers/Plansza.h | 5 +---- headers/Player.h | 4 ++-- main.cpp | 11 ++--------- sources/Actor.cpp | 9 ++++----- sources/AdvancedEnemy.cpp | 4 +--- sources/Enemy.cpp | 4 ++-- sources/Plansza.cpp | 21 +++++---------------- sources/Player.cpp | 11 +++-------- 11 files changed, 25 insertions(+), 52 deletions(-) diff --git a/headers/Actor.h b/headers/Actor.h index fe05f3a..3e5e794 100644 --- a/headers/Actor.h +++ b/headers/Actor.h @@ -9,7 +9,7 @@ class Actor { public: - Actor(int x, int y, const sf::Texture& texture); + Actor(int x, int y, std::string path); void loadTexture(std::string path); @@ -35,6 +35,8 @@ protected: sf::Texture actorTexture; sf::Texture bulletTextureLeft; sf::Texture bulletTextureRight; + sf::Texture enemyTexture; + sf::Texture advancedEnemyTexture; Position position; unsigned int hp; unsigned int damage; diff --git a/headers/AdvancedEnemy.h b/headers/AdvancedEnemy.h index 09fe88f..7dbd0e0 100644 --- a/headers/AdvancedEnemy.h +++ b/headers/AdvancedEnemy.h @@ -13,7 +13,7 @@ enum class DirectionA { class AdvancedEnemy : public Actor { public: - AdvancedEnemy(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture); + AdvancedEnemy(int x, int y, std::string path); void shoot() override; void move(float deltaX, float deltaY) override; diff --git a/headers/Enemy.h b/headers/Enemy.h index bb7c81f..4af000d 100644 --- a/headers/Enemy.h +++ b/headers/Enemy.h @@ -13,7 +13,7 @@ enum class Direction { class Enemy : public Actor { public: - Enemy(int x, int y, const sf::Texture& texture) ; + Enemy(int x, int y, std::string path); void shoot() override; void move(float deltaX, float deltaY) override; diff --git a/headers/Plansza.h b/headers/Plansza.h index 4173def..9548a03 100644 --- a/headers/Plansza.h +++ b/headers/Plansza.h @@ -18,8 +18,7 @@ class Plansza { public: - Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow, - const sf::Texture& playerTexture, const sf::Texture& playerBulletTexture, const sf::Texture& playerRocketTexture); + Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow); Size getSize(); std::vector &getMeteors(); @@ -43,8 +42,6 @@ private: sf::Texture playerTexture; sf::Texture playerBulletTexture; sf::Texture playerRocketTexture; - sf::Texture enemyTexture; - sf::Texture advancedEnemyTexture; sf::Clock spawnClock; sf::Clock shooterSpawnClock; std::vector enemies; diff --git a/headers/Player.h b/headers/Player.h index 721d761..a1a2f6e 100644 --- a/headers/Player.h +++ b/headers/Player.h @@ -8,8 +8,7 @@ class Player : public Actor { public: - Player(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture, const sf::Texture& rocketTexture); - void setTextures(const sf::Texture& shipTexture, const sf::Texture& bulletTexture, const sf::Texture& rocketTexture); + Player(int x, int y, std::string path); void shoot() override; void alternate_shoot(); void setFirerate(unsigned int firerate); @@ -25,6 +24,7 @@ private: std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now(); std::vector rockets; sf::Texture rocketTexture; + int health = 3; // Liczba punktów życia gracza sf::Texture bulletTexture; }; diff --git a/main.cpp b/main.cpp index 6c612bc..67fd617 100644 --- a/main.cpp +++ b/main.cpp @@ -5,14 +5,7 @@ int main() { - std::clog << "Game started\n"; - sf::Texture playerTexture, playerBulletTexture, playerRocketTexture; - if (!playerTexture.loadFromFile("../assets/ship/Dreadnought-Base.png") || - !playerBulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png") || - !playerRocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png")) { - std::cerr << "Failed to load player textures!" << std::endl; - return -1; - } + sf::RenderWindow mainWindow(sf::VideoMode(600, 800), "LotoStatek"); mainWindow.setVerticalSyncEnabled(true); mainWindow.setFramerateLimit(60); @@ -23,7 +16,7 @@ int main() - Plansza plansza(mainWindow.getSize().y, mainWindow.getSize().x, &mainWindow,playerTexture, playerBulletTexture, playerRocketTexture); + Plansza plansza(mainWindow.getSize().y, mainWindow.getSize().x, &mainWindow); while (mainWindow.isOpen()) { mainWindow.clear(); diff --git a/sources/Actor.cpp b/sources/Actor.cpp index 5770cbd..ab90b0a 100644 --- a/sources/Actor.cpp +++ b/sources/Actor.cpp @@ -2,8 +2,8 @@ #include -Actor::Actor(int x, int y, const sf::Texture& texture) { - actorSprite.setTexture(texture); +Actor::Actor(int x, int y, std::string path) { + loadTexture(path); position.x = x; position.y = y; actorSprite.setOrigin(actorSprite.getLocalBounds().width / 2, actorSprite.getLocalBounds().height / 2); // wycentrowanie sprite @@ -16,12 +16,11 @@ void Actor::loadTexture(std::string path) { bulletTextureLeft.loadFromFile("../assets/img/bullets/bullet_pink.png"); bulletTextureRight.loadFromFile("../assets/img/rockets/Rocket_111.png"); + enemyTexture.loadFromFile("../assets/img/enemy/enemy.png"); + advancedEnemyTexture.loadFromFile("../assets/img/enemy/advanced_enemy.png"); } sf::Sprite &Actor::getSprite() { - if (!actorSprite.getTexture()) { - std::cerr << "actorSprite has no texture set!" << std::endl; - } return actorSprite; } diff --git a/sources/AdvancedEnemy.cpp b/sources/AdvancedEnemy.cpp index 44be9ee..0439a56 100644 --- a/sources/AdvancedEnemy.cpp +++ b/sources/AdvancedEnemy.cpp @@ -1,9 +1,7 @@ #include "../headers/AdvancedEnemy.h" #include "../headers/Bullet.h" -AdvancedEnemy::AdvancedEnemy(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture) : Actor(x, y, texture) { - actorSprite.setTexture(texture); - enemyBulletTexture = bulletTexture; +AdvancedEnemy::AdvancedEnemy(int x, int y, std::string path) : Actor(x, y, path) { hp = 2; // 2 punkty życia firerate = 2000; // Strzela co 2 moving_speed = 2.0f; // Prędkość diff --git a/sources/Enemy.cpp b/sources/Enemy.cpp index 9319e39..f04723e 100644 --- a/sources/Enemy.cpp +++ b/sources/Enemy.cpp @@ -1,11 +1,11 @@ #include "../headers/Enemy.h" #include "../headers/Bullet.h" -Enemy::Enemy(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) { - actorSprite.setTexture(texture); +Enemy::Enemy(int x, int y, std::string path) : Actor(x, y, path) { hp = 1; // Przeciwnik ma 1 punkt życia firerate = 2000; // Strzela co 2 moving_speed = 2.0f; // Prędkość + actorSprite.setTexture(enemyTexture); enemyBulletTexture.loadFromFile("../assets/img/bullets/enemy_bullet.png"); } diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index 19727cb..a8040b4 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -3,12 +3,9 @@ #include "../headers/Plansza.h" #include "../headers/ObjectItem.hpp" -Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow, - const sf::Texture& playerTexture, - const sf::Texture& playerBulletTexture, - const sf::Texture& playerRocketTexture) +Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow) : background("../assets/img/background/background.png", 2.0f), -ship(static_cast(mainWindow->getSize().x) / 2, static_cast(mainWindow->getSize().y) - 100, playerTexture, playerBulletTexture, playerRocketTexture) +ship(static_cast(mainWindow->getSize().x) / 2, static_cast(mainWindow->getSize().y) - 100, "../assets/ship/Dreadnought-Base.png") { window = mainWindow; @@ -32,15 +29,7 @@ ship(static_cast(mainWindow->getSize().x) / 2, static_cast(mainWindow- meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png"); meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png"); - // Ładowanie tekstur wrogów - if (!enemyTexture.loadFromFile("../assets/img/enemy/enemy.png")) { - std::cerr << "Failed to load enemy texture!" << std::endl; - exit(-1); - } - if (!advancedEnemyTexture.loadFromFile("../assets/img/enemy/advanced_enemy.png")) { - std::cerr << "Failed to load advanced enemy texture!" << std::endl; - exit(-1); - } + spawnClock.restart(); } @@ -400,7 +389,7 @@ void Plansza::update_meteors() { void Plansza::spawn_enemy() { if (enemySpawnClock.getElapsedTime().asSeconds() >= 6) { // Spawn co 10 sekund int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50); - enemies.emplace_back(spawnX, -50, enemyTexture); + enemies.emplace_back(spawnX, -50, "../assets/img/enemy/enemy.png"); enemySpawnClock.restart(); @@ -410,7 +399,7 @@ void Plansza::spawn_enemy() { void Plansza::spawn_advanced_enemy() { if (AenemySpawnClock.getElapsedTime().asSeconds() >= 5) { // Spawn co 10 sekund int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50); - AEnemies.emplace_back(spawnX, 50, advancedEnemyTexture, enemyBulletTexture); + AEnemies.emplace_back(spawnX, 50, "../assets/img/enemy/advanced_enemy.png"); std::cout << "Spawned Advanced Enemy at X: " << spawnX << std::endl; AenemySpawnClock.restart(); } diff --git a/sources/Player.cpp b/sources/Player.cpp index f3b3810..5a8a91a 100644 --- a/sources/Player.cpp +++ b/sources/Player.cpp @@ -7,16 +7,11 @@ #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) { - +Player::Player(int x, int y, std::string path) : Actor(x, y, path) { + bulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png"); + rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png"); }; -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(now - lastShotTime).count() >= firerate) {