Compare commits
7 Commits
Strzaly-w-
...
13066709a7
| Author | SHA1 | Date | |
|---|---|---|---|
| 13066709a7 | |||
| 44f4556fda | |||
| c655409596 | |||
| e2e44ff1ba | |||
| b6830c305b | |||
| 087a8d7672 | |||
| 0e1b53fc9b |
@@ -25,7 +25,14 @@ add_executable(LotoStatek main.cpp
|
||||
headers/Rocket.h
|
||||
sources/Rocket.cpp
|
||||
headers/Size.h
|
||||
headers/Position.h)
|
||||
headers/Position.h
|
||||
headers/ObjectItem.hpp
|
||||
sources/ObjectItem.cpp
|
||||
sources/Enemy.cpp
|
||||
headers/Enemy.h
|
||||
headers/EnemyBullet.h
|
||||
sources/EnemyBullet.cpp
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/lib/SFML")
|
||||
@@ -40,9 +47,9 @@ if(WIN32)
|
||||
target_link_libraries(LotoStatek ${SFML_LIBRARIES})
|
||||
endif()
|
||||
elseif(APPLE)
|
||||
find_package(lib/SFML 2.6.2 COMPONENTS graphics window system REQUIRED)
|
||||
find_package(SFML 2.6.2 COMPONENTS graphics window system REQUIRED)
|
||||
target_link_libraries(LotoStatek sfml-graphics sfml-window sfml-audio sfml-system)
|
||||
elseif(LINUX)
|
||||
find_package(lib/SFML 2.6.2 COMPONENTS graphics window system REQUIRED)
|
||||
find_package(SFML 2.6.2 COMPONENTS graphics window system REQUIRED)
|
||||
target_link_libraries(LotoStatek sfml-graphics sfml-window sfml-audio sfml-system)
|
||||
endif()
|
||||
BIN
assets/img/bullets/enemy_bullet.png
Normal file
BIN
assets/img/bullets/enemy_bullet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 8.3 KiB |
BIN
assets/img/enemy/enemy.png
Normal file
BIN
assets/img/enemy/enemy.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 18 KiB |
BIN
assets/img/hearts/heart.png
Normal file
BIN
assets/img/hearts/heart.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 782 B |
BIN
assets/sounds/explosion.mp3
Normal file
BIN
assets/sounds/explosion.mp3
Normal file
Binary file not shown.
@@ -8,6 +8,8 @@ class Bullet : public Projectile {
|
||||
public:
|
||||
Bullet(float x, float y, sf::Texture &texture) : Projectile(x,y, texture) {};
|
||||
void update() override;
|
||||
private:
|
||||
float directionY;
|
||||
};
|
||||
|
||||
|
||||
|
||||
39
headers/Enemy.h
Normal file
39
headers/Enemy.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef ENEMY_H
|
||||
#define ENEMY_H
|
||||
|
||||
#include "Actor.h"
|
||||
#include "SFML/System/Clock.hpp"
|
||||
|
||||
enum class Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right
|
||||
};
|
||||
|
||||
class Enemy : public Actor {
|
||||
public:
|
||||
Enemy(int x, int y, std::string path);
|
||||
|
||||
void shoot() override;
|
||||
void move(float deltaX, float deltaY) override;
|
||||
void moveLeft() override;
|
||||
void moveRight() override;
|
||||
void moveUp() override;
|
||||
void moveDown() override;
|
||||
|
||||
void update();
|
||||
|
||||
bool isAlive() const;
|
||||
void takeDamage();
|
||||
void updateDirection();
|
||||
|
||||
private:
|
||||
sf::Clock shootClock;
|
||||
sf::Texture enemyBulletTexture;
|
||||
float movementSpeed = 2.0f;
|
||||
bool alive = true;
|
||||
Direction direction = Direction::Down;
|
||||
};
|
||||
|
||||
#endif // ENEMY_H
|
||||
14
headers/EnemyBullet.h
Normal file
14
headers/EnemyBullet.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#ifndef ENEMY_BULLET_H
|
||||
#define ENEMY_BULLET_H
|
||||
|
||||
#include "Projectile.h"
|
||||
|
||||
class EnemyBullet : public Projectile {
|
||||
public:
|
||||
EnemyBullet(float x, float y, sf::Texture &texture);
|
||||
void update() override;
|
||||
|
||||
void setOutOfBounds(bool status);
|
||||
};
|
||||
|
||||
#endif // ENEMY_BULLET_H
|
||||
@@ -4,22 +4,12 @@
|
||||
#include "SFML/Graphics/Texture.hpp"
|
||||
#include "SFML/Graphics/Sprite.hpp"
|
||||
#include "Position.h"
|
||||
#include "ObjectItem.hpp"
|
||||
|
||||
class Meteor {
|
||||
class Meteor : public ObjectItem {
|
||||
public:
|
||||
Meteor(float x, float y, sf::Texture &texture);
|
||||
sf::Sprite &getSprite();
|
||||
bool getStatus();
|
||||
void update();
|
||||
// ~Meteor();
|
||||
private:
|
||||
sf::Texture meteorTexture;
|
||||
sf::Sprite meteorSprite;
|
||||
Position meteorPosition;
|
||||
float meteorRotationSpeed;
|
||||
float meteorSpeed;
|
||||
bool outOfBounds;
|
||||
static unsigned int counter;
|
||||
};
|
||||
|
||||
|
||||
|
||||
24
headers/ObjectItem.hpp
Normal file
24
headers/ObjectItem.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef LOTOSTATEK_OBJECTITEM_HPP
|
||||
#define LOTOSTATEK_OBJECTITEM_HPP
|
||||
|
||||
#include "SFML/Graphics/Sprite.hpp"
|
||||
#include "Position.h"
|
||||
#include "SFML/Graphics/Texture.hpp"
|
||||
|
||||
class ObjectItem {
|
||||
public:
|
||||
ObjectItem(float x, float y, sf::Texture &texture);
|
||||
sf::Sprite &getSprite();
|
||||
bool getStatus();
|
||||
virtual void update() = 0;
|
||||
protected:
|
||||
sf::Texture texture;
|
||||
sf::Sprite sprite;
|
||||
Position position;
|
||||
float rotationSpeed;
|
||||
float movingSpeed;
|
||||
bool outOfBounds;
|
||||
static unsigned int counter;
|
||||
};
|
||||
|
||||
#endif //LOTOSTATEK_OBJECTITEM_HPP
|
||||
@@ -2,6 +2,7 @@
|
||||
#define PLANSZA_H
|
||||
|
||||
#include "Meteor.h"
|
||||
#include "Enemy.h"
|
||||
#include "RandomNumberGenerator.h"
|
||||
#include "SFML/System/Clock.hpp"
|
||||
#include "SFML/Graphics/RenderWindow.hpp"
|
||||
@@ -18,9 +19,13 @@ public:
|
||||
Plansza(unsigned int windowHeight,unsigned int windowWidth, sf::RenderWindow *mainWindow);
|
||||
Size getSize();
|
||||
std::vector<Meteor> &getMeteors();
|
||||
|
||||
|
||||
void spawn_meteor();
|
||||
void update_meteors();
|
||||
void update();
|
||||
void spawn_enemy();
|
||||
void setOutOfBounds(bool status);
|
||||
private:
|
||||
Size size;
|
||||
Background background;
|
||||
@@ -29,6 +34,9 @@ private:
|
||||
sf::Texture meteorTexture1;
|
||||
sf::Texture meteorTexture2;
|
||||
sf::Clock spawnClock;
|
||||
sf::Clock shooterSpawnClock;
|
||||
std::vector<Enemy> enemies;
|
||||
sf::Clock enemySpawnClock;
|
||||
std::vector<Meteor> meteors;
|
||||
sf::RenderWindow *window;
|
||||
};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "../headers/Actor.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Actor::Actor(int x, int y, std::string path) {
|
||||
loadTexture(path);
|
||||
position.x = x;
|
||||
@@ -29,13 +31,17 @@ std::vector<Bullet> &Actor::getBullets() {
|
||||
}
|
||||
|
||||
void Actor::updateBullets() {
|
||||
for (auto& bullet : bullets) {
|
||||
if(bullet.isOutOfBounds()) {
|
||||
bullets.erase(bullets.begin());
|
||||
for (auto it = bullets.begin(); it != bullets.end(); ) {
|
||||
if (it->isOutOfBounds()) {
|
||||
it = bullets.erase(it); // Usuwa element i zwraca iterator na następny element
|
||||
} else {
|
||||
++it; // Przechodzi do następnego elementu
|
||||
}
|
||||
}
|
||||
std::cout << "Liczba pociskow: " << bullets.size() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
void Actor::setMovingSpeed(float speed) {
|
||||
moving_speed = speed;
|
||||
}
|
||||
|
||||
79
sources/Enemy.cpp
Normal file
79
sources/Enemy.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "../headers/Enemy.h"
|
||||
#include "../headers/Bullet.h"
|
||||
|
||||
Enemy::Enemy(int x, int y, std::string path) : Actor(x, y, path) {
|
||||
hp = 1; // Przeciwnik ma 1 punkt życia
|
||||
firerate = 2000; // Strzela co 1 sekundę
|
||||
moving_speed = 2.0f; // Prędkość ruchu przeciwnika
|
||||
enemyBulletTexture.loadFromFile("../assets/img/bullets/enemy_bullet.png");
|
||||
}
|
||||
|
||||
void Enemy::shoot() {
|
||||
if (shootClock.getElapsedTime().asMilliseconds() >= firerate) {
|
||||
Bullet bullet(position.x, position.y + actorSprite.getGlobalBounds().height / 2, enemyBulletTexture);
|
||||
bullet.setSpeed(10.0f); // Prędkość w dół
|
||||
bullets.emplace_back(std::move(bullet));
|
||||
shootClock.restart();
|
||||
}
|
||||
}
|
||||
|
||||
void Enemy::updateDirection() {
|
||||
// Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi
|
||||
if (position.y <= 0) { // Górna krawędź ekranu
|
||||
direction = Direction::Down; // Zmieniamy na ruch w dół
|
||||
} else if (position.y >= 800) { // Dolna krawędź ekranu
|
||||
direction = Direction::Up; // Zmieniamy na ruch w górę
|
||||
}
|
||||
|
||||
// Podobna logika dla kierunku lewo/prawo, jeśli przeciwnik będzie się poruszał w poziomie
|
||||
if (position.x <= 0) { // Lewa krawędź
|
||||
direction = Direction::Right; // Zmieniamy na ruch w prawo
|
||||
} else if (position.x >= 1200) { // Prawa krawędź
|
||||
direction = Direction::Left; // Zmieniamy na ruch w lewo
|
||||
}
|
||||
}
|
||||
|
||||
void Enemy::move(float deltaX, float deltaY) {
|
||||
actorSprite.move(deltaX, deltaY);
|
||||
position.x += static_cast<int>(deltaX);
|
||||
position.y += static_cast<int>(deltaY);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Enemy::moveLeft() { move(-moving_speed, 0.0f); }
|
||||
void Enemy::moveRight() { move(moving_speed, 0.0f); }
|
||||
void Enemy::moveUp() { move(0.0f, -moving_speed); }
|
||||
void Enemy::moveDown() { move(0.0f, moving_speed); }
|
||||
|
||||
void Enemy::update() {
|
||||
// Sprawdzamy, czy przeciwnik dotarł do krawędzi i zmieniamy kierunek
|
||||
updateDirection();
|
||||
|
||||
// Zgodnie z kierunkiem, poruszamy przeciwnikiem
|
||||
switch (direction) {
|
||||
case Direction::Up:
|
||||
moveUp();
|
||||
break;
|
||||
case Direction::Down:
|
||||
moveDown();
|
||||
break;
|
||||
case Direction::Left:
|
||||
moveLeft();
|
||||
break;
|
||||
case Direction::Right:
|
||||
moveRight();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Enemy::isAlive() const {
|
||||
return alive;
|
||||
}
|
||||
|
||||
void Enemy::takeDamage() {
|
||||
if (--hp <= 0) {
|
||||
alive = false;
|
||||
}
|
||||
}
|
||||
18
sources/EnemyBullet.cpp
Normal file
18
sources/EnemyBullet.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "../headers/EnemyBullet.h"
|
||||
|
||||
EnemyBullet::EnemyBullet(float x, float y, sf::Texture &texture)
|
||||
: Projectile(x, y, texture) {
|
||||
speed = 5.0f; // Ruch w dół (dodatnia prędkość)
|
||||
}
|
||||
|
||||
void EnemyBullet::update() {
|
||||
sprite.move(0.0f, speed); // Przesuwanie pocisku w dół
|
||||
position.y += speed;
|
||||
|
||||
if (position.y > 800) {
|
||||
outOfBounds = true;
|
||||
}
|
||||
}
|
||||
void EnemyBullet::setOutOfBounds(bool status) {
|
||||
outOfBounds = status;
|
||||
}
|
||||
@@ -1,40 +1,29 @@
|
||||
#include <iostream>
|
||||
#include "../headers/Meteor.h"
|
||||
|
||||
Meteor::Meteor(float x, float y, sf::Texture &texture) {
|
||||
Meteor::Meteor(float x, float y, sf::Texture &texture) : ObjectItem(x, y, texture) {
|
||||
outOfBounds = false;
|
||||
meteorTexture = texture;
|
||||
meteorSprite.setTexture(texture);
|
||||
meteorSprite.setOrigin(meteorSprite.getLocalBounds().width / 2, meteorSprite.getLocalBounds().height / 2); // wycentrowanie sprite
|
||||
meteorSprite.setPosition(x, y);
|
||||
meteorSpeed = 5.0f;
|
||||
meteorSprite.scale(0.05f, 0.05f);
|
||||
meteorPosition.x = x;
|
||||
meteorPosition.y = y;
|
||||
meteorRotationSpeed = static_cast<float>(rand() % 2 + 1) * (rand() % 2 == 0 ? 1 : -1);
|
||||
texture = texture;
|
||||
sprite.setTexture(texture);
|
||||
sprite.setOrigin(sprite.getLocalBounds().width / 2, sprite.getLocalBounds().height / 2); // wycentrowanie sprite
|
||||
sprite.setPosition(x, y);
|
||||
movingSpeed = 5.0f;
|
||||
sprite.scale(0.05f, 0.05f);
|
||||
position.x = x;
|
||||
position.y = y;
|
||||
rotationSpeed = static_cast<float>(rand() % 2 + 1) * (rand() % 2 == 0 ? 1 : -1);
|
||||
}
|
||||
|
||||
sf::Sprite &Meteor::getSprite() {
|
||||
return meteorSprite;
|
||||
}
|
||||
|
||||
|
||||
void Meteor::update() {
|
||||
meteorSprite.move(0.0f, meteorSpeed); // przesunięcie sprajta
|
||||
meteorPosition.y += int(meteorSpeed); // przesunięcie pozycji
|
||||
meteorSprite.rotate(meteorRotationSpeed); // obracanie tym meteorkiem pięknym
|
||||
if(meteorPosition.y > 900) {
|
||||
sprite.move(0.0f, movingSpeed); // przesunięcie sprajta
|
||||
position.y += int(movingSpeed); // przesunięcie pozycji
|
||||
sprite.rotate(rotationSpeed); // obracanie tym meteorkiem pięknym
|
||||
if(position.y > 900) {
|
||||
outOfBounds = true; // jeżeli wyszedł poza granice ekranu ustaw tą zmienną
|
||||
}
|
||||
// std::cout << "x: " << meteorSprite.getPosition().x << std::endl;
|
||||
// std::cout << "y: " << meteorSprite.getPosition().y << std::endl;
|
||||
}
|
||||
|
||||
bool Meteor::getStatus() {
|
||||
return outOfBounds;
|
||||
}
|
||||
|
||||
unsigned int Meteor::counter = 0;
|
||||
|
||||
// było użyte do testowania czy meteoryt jest kasowany
|
||||
//Meteor::~Meteor() {
|
||||
// Meteor::counter++;
|
||||
|
||||
17
sources/ObjectItem.cpp
Normal file
17
sources/ObjectItem.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "../headers/ObjectItem.hpp"
|
||||
|
||||
ObjectItem::ObjectItem(float x, float y, sf::Texture &texture) {
|
||||
Position position_;
|
||||
position_.x = x;
|
||||
position_.y = y;
|
||||
position = position_;
|
||||
this->texture = texture;
|
||||
}
|
||||
|
||||
bool ObjectItem::getStatus() {
|
||||
return outOfBounds;
|
||||
}
|
||||
|
||||
sf::Sprite &ObjectItem::getSprite() {
|
||||
return sprite;
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
#include "../headers/Plansza.h"
|
||||
#include "../headers/ObjectItem.hpp"
|
||||
|
||||
Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow)
|
||||
: background("../assets/img/background/background.png", 2.0f),
|
||||
@@ -66,7 +67,7 @@ void Plansza::update() {
|
||||
|
||||
// generowanie nowego meteoru
|
||||
spawn_meteor();
|
||||
|
||||
spawn_enemy();
|
||||
|
||||
// utrzymanie meteorów i pocisków w ruchu
|
||||
for (auto& meteor : getMeteors()) {
|
||||
@@ -156,6 +157,63 @@ void Plansza::update() {
|
||||
++meteorIt;
|
||||
}
|
||||
}
|
||||
// Ruch i renderowanie przeciwników
|
||||
for (auto it = enemies.begin(); it != enemies.end();) {
|
||||
it->update(); // Aktualizacja kierunku i ruchu przeciwnika
|
||||
it->shoot();
|
||||
|
||||
// Rysowanie przeciwników
|
||||
window->draw(it->getSprite());
|
||||
|
||||
// Usunięcie martwych przeciwników
|
||||
if (!it->isAlive()) {
|
||||
it = enemies.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& enemy : enemies) { // Lista przeciwników w grze
|
||||
enemy.shoot();
|
||||
enemy.updateBullets();
|
||||
for (auto& bullet : enemy.getBullets()) {
|
||||
bullet.update();
|
||||
window->draw(bullet.getSprite());
|
||||
}
|
||||
}
|
||||
|
||||
// Usuwanie pocisków, które są poza ekranem
|
||||
for (auto enemyIt = enemies.begin(); enemyIt != enemies.end(); ) {
|
||||
for (auto bulletIt = enemyIt->getBullets().begin(); bulletIt != enemyIt->getBullets().end();) {
|
||||
if (bulletIt->isOutOfBounds()) {
|
||||
bulletIt = enemyIt->getBullets().erase(bulletIt); // Usuwamy pocisk, który wyszedł poza ekran
|
||||
} else {
|
||||
++bulletIt;
|
||||
}
|
||||
}
|
||||
++enemyIt;
|
||||
}
|
||||
|
||||
|
||||
// Kolizje między pociskami gracza a przeciwnikami
|
||||
for (auto enemyIt = enemies.begin(); enemyIt != enemies.end();) {
|
||||
bool hit = false;
|
||||
for (auto bulletIt = ship.getBullets().begin(); bulletIt != ship.getBullets().end();) {
|
||||
if (enemyIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) {
|
||||
bulletIt = ship.getBullets().erase(bulletIt);
|
||||
enemyIt->takeDamage();
|
||||
hit = true;
|
||||
break;
|
||||
} else {
|
||||
++bulletIt;
|
||||
}
|
||||
}
|
||||
if (hit && !enemyIt->isAlive()) {
|
||||
enemyIt = enemies.erase(enemyIt);
|
||||
} else {
|
||||
++enemyIt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Meteor-related niżej
|
||||
@@ -182,6 +240,14 @@ void Plansza::update_meteors() {
|
||||
}
|
||||
}
|
||||
|
||||
void Plansza::spawn_enemy() {
|
||||
if (enemySpawnClock.getElapsedTime().asSeconds() >= 10) { // Spawn co 10 sekund
|
||||
int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50);
|
||||
enemies.emplace_back(spawnX, -50, "../assets/img/enemy/enemy.png");
|
||||
enemySpawnClock.restart();
|
||||
}
|
||||
}
|
||||
|
||||
Size Plansza::getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user