diff --git a/headers/Actor.h b/headers/Actor.h index 7ba2175..1c62ef9 100644 --- a/headers/Actor.h +++ b/headers/Actor.h @@ -30,12 +30,10 @@ public: std::vector& getBullets(); - void updateBullets() { - for (auto& bullet : bullets) { - if(bullet.getStatus()) { - bullets.erase(bullets.begin()); - } - } + void updateBullets(); + + void setMovingSpeed(float speed) { + moving_speed = speed; } protected: diff --git a/headers/Bullet.h b/headers/Bullet.h index 40dc448..e4c34d4 100644 --- a/headers/Bullet.h +++ b/headers/Bullet.h @@ -1,9 +1,9 @@ #ifndef LOTOSTATEK_BULLET_H #define LOTOSTATEK_BULLET_H - #include "SFML/Graphics/Sprite.hpp" #include "SFML/Graphics/Texture.hpp" + class Bullet { struct Position { int x; @@ -15,9 +15,7 @@ public: void update(); sf::Sprite& getSprite(); void setSpeed(float speed); - bool getStatus() const { - return outOfBounds; - } + bool getStatus() const; private: sf::Sprite bulletSprite; sf::Texture bulletTexture; diff --git a/headers/Player.h b/headers/Player.h index 5928d31..76e73ec 100644 --- a/headers/Player.h +++ b/headers/Player.h @@ -7,8 +7,15 @@ class Player : public Actor { public: Player(int x, int y, std::string path); + void shoot(); + void setFirerate(unsigned int firerate); + void move(float deltaX, float deltaY); + void moveLeft(); + void moveRight(); + void moveUp(); + void moveDown(); private: - + std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now(); }; diff --git a/main.cpp b/main.cpp index a4bb5cc..cf61214 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include +#include #include "SFML/Graphics.hpp" -#include "headers/Actor.h" #include "headers/Player.h" #include "headers/Bullet.h" @@ -17,6 +17,8 @@ int main() sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku + ship.setMovingSpeed(8); + ship.setFirerate(200); while (window.isOpen()) { window.clear(); @@ -26,32 +28,39 @@ int main() // Tu są handlowane eventy sf::Event event{}; while (window.pollEvent(event)) { - - 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(event.type == sf::Event::MouseButtonPressed) { - ship.shoot(); - } - + if(event.type == sf::Event::Closed) + window.close(); if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { window.close(); } + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { + ship.shoot(); + } + if(event.type == sf::Event::MouseButtonPressed) { + ship.shoot(); + } + } - if (event.type == sf::Event::Closed) - window.close(); - + // Sprawdzanie stanu klawiszy w głównej pętli gry + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + if(ship.getPosition().x > -10) { + ship.moveLeft(); + } + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + if(ship.getPosition().x < 480) { + ship.moveRight(); + } + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { + if(ship.getPosition().y > 0) { + ship.moveUp(); + } + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { + if(ship.getPosition().y < 700) { + ship.moveDown(); + } } for (auto& bullet : ship.getBullets()) { @@ -65,4 +74,4 @@ int main() } return 0; -} +} \ No newline at end of file diff --git a/sources/Actor.cpp b/sources/Actor.cpp index 6e10cd2..c98ec74 100644 --- a/sources/Actor.cpp +++ b/sources/Actor.cpp @@ -18,19 +18,11 @@ sf::Sprite &Actor::getSprite() { return actorSprite; } -void Actor::move(float deltaX, float deltaY) { - actorSprite.move(deltaX, deltaY); -} +void Actor::move(float deltaX, float deltaY) {} -void Actor::moveLeft() { - move(-10.0f, 0.0f); - position.x -= 10; -} +void Actor::moveLeft() {} -void Actor::moveRight() { - move(10.0f, 0.0f); - position.x += 10; -} +void Actor::moveRight() {} Position Actor::getPosition() { return {position.x, position.y}; @@ -43,3 +35,11 @@ void Actor::shoot() { std::vector &Actor::getBullets() { return bullets; } + +void Actor::updateBullets() { + for (auto& bullet : bullets) { + if(bullet.getStatus()) { + bullets.erase(bullets.begin()); + } + } +} diff --git a/sources/Bullet.cpp b/sources/Bullet.cpp index c655bac..c897f7d 100644 --- a/sources/Bullet.cpp +++ b/sources/Bullet.cpp @@ -3,7 +3,6 @@ Bullet::Bullet(float x, float y, sf::Texture &bulletTexture) { outOfBounds = false; -// bulletTexture.loadFromFile("../assets/img/bullet.png"); bulletSprite.setTexture(bulletTexture); bulletSprite.setPosition(x, y); bulletSpeed = -10.0f; @@ -21,9 +20,12 @@ sf::Sprite &Bullet::getSprite() { void Bullet::update() { bulletSprite.move(0.0f, bulletSpeed); - bulletPosition.y += bulletSpeed; + bulletPosition.y += int(bulletSpeed); if(bulletPosition.y < -100) { outOfBounds = true; -// std::cout << "Bullet out of bounds\n"; } } + +bool Bullet::getStatus() const { + return outOfBounds; +} diff --git a/sources/Player.cpp b/sources/Player.cpp index b3b1c54..2dea282 100644 --- a/sources/Player.cpp +++ b/sources/Player.cpp @@ -1,3 +1,39 @@ #include "../headers/Player.h" Player::Player(int x, int y, std::string path) : Actor(x, y, path) {}; + +void Player::shoot() { + auto now = std::chrono::steady_clock::now(); + if (std::chrono::duration_cast(now - lastShotTime).count() >= firerate) { + bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2-62, position.y, bulletTexture); + lastShotTime = now; + } +} + +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 -= moving_speed; +} + +void Player::moveRight() { + move(moving_speed, 0.0f); + position.x += moving_speed; +} + +void Player::moveUp() { + move(0.0f, -moving_speed); + position.y -= moving_speed; +} + +void Player::moveDown() { + move(0.0f, moving_speed); + position.y += moving_speed; +} \ No newline at end of file