diff --git a/CMakeLists.txt b/CMakeLists.txt index 83ddf92..c9b5e55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,11 @@ add_executable(LotoStatek main.cpp headers/Actor.h headers/Plansza.h sources/Actor.cpp - sources/Plansza.cpp) + sources/Plansza.cpp + sources/Player.cpp + headers/Player.h + headers/Bullet.h + sources/Bullet.cpp) if(WIN32) set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML") diff --git a/assets/img/background.jpg b/assets/img/background.jpg new file mode 100644 index 0000000..25b278f Binary files /dev/null and b/assets/img/background.jpg differ diff --git a/assets/img/bullet.png b/assets/img/bullet.png new file mode 100644 index 0000000..4b64b92 Binary files /dev/null and b/assets/img/bullet.png differ diff --git a/assets/ship/Dreadnought-Base.png b/assets/ship/Dreadnought-Base.png new file mode 100644 index 0000000..70cbf76 Binary files /dev/null and b/assets/ship/Dreadnought-Base.png differ diff --git a/assets/ship/Fighter-Base.png b/assets/ship/Fighter-Base.png new file mode 100644 index 0000000..80bb11a Binary files /dev/null and b/assets/ship/Fighter-Base.png differ diff --git a/headers/Actor.h b/headers/Actor.h index f235735..0bbdb2b 100644 --- a/headers/Actor.h +++ b/headers/Actor.h @@ -1,9 +1,52 @@ #ifndef ACTOR_H #define ACTOR_H +#include "SFML/Graphics/Sprite.hpp" +#include "SFML/Graphics/Texture.hpp" +#include "Bullet.h" + +struct Position { + int x; + int y; +}; + class Actor { +public: + Actor(int x, int y, std::string path); + void loadTexture(std::string path); + sf::Sprite& getSprite(); + + Position getPosition(); + + void move(float deltaX, float deltaY); + + void moveLeft(); + + void moveRight(); + + void shoot(); + + std::vector& getBullets(); + + void updateBullets() { + for (auto& bullet : bullets) { + if(bullet.getStatus()) { + bullets.erase(bullets.begin()); + } + } + } + +protected: + sf::Sprite actorSprite; + sf::Texture actorTexture; + Position position; + unsigned int hp; + unsigned int damage; + unsigned int firerate; + float moving_speed; + std::vector bullets; }; #endif //ACTOR_H diff --git a/headers/Bullet.h b/headers/Bullet.h new file mode 100644 index 0000000..5f6a901 --- /dev/null +++ b/headers/Bullet.h @@ -0,0 +1,30 @@ +#ifndef LOTOSTATEK_BULLET_H +#define LOTOSTATEK_BULLET_H + + +#include "SFML/Graphics/Sprite.hpp" +#include "SFML/Graphics/Texture.hpp" +class Bullet { + struct Position { + int x; + int y; + }; + +public: + Bullet(float x, float y); + void update(); + sf::Sprite& getSprite(); + void setSpeed(float speed); + bool getStatus() const { + return outOfBounds; + } +private: + sf::Sprite bulletSprite; + sf::Texture bulletTexture; + float bulletSpeed; + Position bulletPosition; + bool outOfBounds; +}; + + +#endif //LOTOSTATEK_BULLET_H diff --git a/headers/Player.h b/headers/Player.h new file mode 100644 index 0000000..5928d31 --- /dev/null +++ b/headers/Player.h @@ -0,0 +1,15 @@ +#ifndef LOTOSTATEK_PLAYER_H +#define LOTOSTATEK_PLAYER_H + + +#include "Actor.h" + +class Player : public Actor { +public: + Player(int x, int y, std::string path); +private: + +}; + + +#endif //LOTOSTATEK_PLAYER_H diff --git a/main.cpp b/main.cpp index 9cfa570..c6e648e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,51 +1,62 @@ #include #include "SFML/Graphics.hpp" +#include "headers/Actor.h" +#include "headers/Player.h" +#include "headers/Bullet.h" int main() { - std::cout << "Game started\n"; + std::clog << "Game started\n"; sf::RenderWindow window(sf::VideoMode(600, 800), "My window"); window.setVerticalSyncEnabled(true); window.setFramerateLimit(60); - sf::RectangleShape rectangle(sf::Vector2f(120.0f, 50.0f)); - float sizeIncrement = 1.0f; + sf::Texture backgroundTexture; + backgroundTexture.loadFromFile("../assets/img/background.jpg"); // wczytywanie tła + sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła + + Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku while (window.isOpen()) { window.clear(); - if(rectangle.getSize().x > 5 && rectangle.getSize().y > 5 && rectangle.getSize().x < 300 && rectangle.getSize().y < 300) { - rectangle.setSize(rectangle.getSize() + sf::Vector2f(sizeIncrement, sizeIncrement)); - rectangle.setPosition( - static_cast(window.getSize().x) / 2 - rectangle.getSize().x / 2, - static_cast(window.getSize().y) / 2 - rectangle.getSize().y / 2 - ); - } else { - rectangle.setSize(sf::Vector2f(120.0f, 50.0f)); - rectangle.setPosition( - static_cast(window.getSize().x) / 2 - rectangle.getSize().x / 2, - static_cast(window.getSize().y) / 2 - rectangle.getSize().y / 2 - ); - } - + window.draw(backgroundSprite); // narysuj tło // Tu są handlowane eventy sf::Event event{}; while (window.pollEvent(event)) { - if(event.type == sf::Event::MouseWheelScrolled) { - if(event.mouseWheelScroll.delta > 0) { - sizeIncrement = 1.0f; - } else { - sizeIncrement = -1.0f; + + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + if(ship.getPosition().x > -10) { + ship.moveLeft(); } + } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + if(ship.getPosition().x < 480) { + ship.moveRight(); + } + } + + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { + ship.shoot(); + } + + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { + window.close(); } if (event.type == sf::Event::Closed) window.close(); + } - window.draw(rectangle); + for (auto& bullet : ship.getBullets()) { + bullet.update(); + window.draw(bullet.getSprite()); + } + + ship.updateBullets(); + window.draw(ship.getSprite()); window.display(); } diff --git a/sources/Actor.cpp b/sources/Actor.cpp index e84d3f3..d1a57dd 100644 --- a/sources/Actor.cpp +++ b/sources/Actor.cpp @@ -1 +1,44 @@ -#include "../headers/actor.h" \ No newline at end of file +#include "../headers/Actor.h" + +Actor::Actor(int x, int y, std::string path) { + loadTexture(path); + position.x = x; + position.y = y; + actorSprite.setPosition(x, y); +} + +void Actor::loadTexture(std::string path) { + actorTexture.loadFromFile(path); + actorSprite.setTexture(actorTexture); +} + +sf::Sprite &Actor::getSprite() { + return actorSprite; +} + +void Actor::move(float deltaX, float deltaY) { + actorSprite.move(deltaX, deltaY); +} + +void Actor::moveLeft() { + move(-10.0f, 0.0f); + position.x -= 10; +} + +void Actor::moveRight() { + move(10.0f, 0.0f); + position.x += 10; +} + +Position Actor::getPosition() { + return {position.x, position.y}; +} + +void Actor::shoot() { + bullets.emplace_back(position.x + actorSprite.getGlobalBounds().width / 2-62, position.y); +} + +std::vector &Actor::getBullets() { + return bullets; +} + diff --git a/sources/Bullet.cpp b/sources/Bullet.cpp new file mode 100644 index 0000000..fa2906f --- /dev/null +++ b/sources/Bullet.cpp @@ -0,0 +1,29 @@ +#include +#include "../headers/Bullet.h" + +Bullet::Bullet(float x, float y) { + outOfBounds = false; + bulletTexture.loadFromFile("../assets/img/bullet.png"); + bulletSprite.setTexture(bulletTexture); + bulletSprite.setPosition(x, y); + bulletSpeed = -10.0f; + bulletPosition.x = x; + bulletPosition.y = y; +} + +void Bullet::setSpeed(float speed) { + bulletSpeed = speed; +} + +sf::Sprite &Bullet::getSprite() { + return bulletSprite; +} + +void Bullet::update() { + bulletSprite.move(0.0f, bulletSpeed); + bulletPosition.y += bulletSpeed; + if(bulletPosition.y < -100) { + outOfBounds = true; + std::cout << "Bullet out of bounds\n"; + } +} diff --git a/sources/Player.cpp b/sources/Player.cpp new file mode 100644 index 0000000..b3b1c54 --- /dev/null +++ b/sources/Player.cpp @@ -0,0 +1,3 @@ +#include "../headers/Player.h" + +Player::Player(int x, int y, std::string path) : Actor(x, y, path) {};