Dziuala 3 strzlowiec

This commit is contained in:
2024-12-11 00:05:53 +01:00
parent 81b04bae0f
commit 2bffc2de0c
11 changed files with 25 additions and 52 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<Meteor> &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<Enemy> enemies;

View File

@@ -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<Rocket> rockets;
sf::Texture rocketTexture;
int health = 3; // Liczba punktów życia gracza
sf::Texture bulletTexture;
};

View File

@@ -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();

View File

@@ -2,8 +2,8 @@
#include <iostream>
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;
}

View File

@@ -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ść

View File

@@ -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");
}

View File

@@ -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<int>(mainWindow->getSize().x) / 2, static_cast<int>(mainWindow->getSize().y) - 100, playerTexture, playerBulletTexture, playerRocketTexture)
ship(static_cast<int>(mainWindow->getSize().x) / 2, static_cast<int>(mainWindow->getSize().y) - 100, "../assets/ship/Dreadnought-Base.png")
{
window = mainWindow;
@@ -32,15 +29,7 @@ ship(static_cast<int>(mainWindow->getSize().x) / 2, static_cast<int>(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();
}

View File

@@ -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<std::chrono::milliseconds>(now - lastShotTime).count() >= firerate) {