A little code refactor

+ new bullet texture added
This commit is contained in:
2024-11-16 16:01:01 +01:00
parent a190e81ba2
commit fcabc0534a
6 changed files with 19 additions and 24 deletions

BIN
assets/img/bullet_right.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@@ -20,13 +20,12 @@ public:
Position getPosition(); Position getPosition();
void move(float deltaX, float deltaY); virtual void move(float deltaX, float deltaY) = 0;
virtual void moveLeft() = 0;
void moveLeft(); virtual void moveRight() = 0;
virtual void moveUp() = 0;
void moveRight(); virtual void moveDown() = 0;
virtual void shoot() = 0;
void shoot();
std::vector<Bullet>& getBullets(); std::vector<Bullet>& getBullets();
@@ -39,7 +38,8 @@ public:
protected: protected:
sf::Sprite actorSprite; sf::Sprite actorSprite;
sf::Texture actorTexture; sf::Texture actorTexture;
sf::Texture bulletTexture; sf::Texture bulletTextureLeft;
sf::Texture bulletTextureRight;
Position position; Position position;
unsigned int hp; unsigned int hp;
unsigned int damage; unsigned int damage;

View File

@@ -7,13 +7,13 @@
class Player : public Actor { class Player : public Actor {
public: public:
Player(int x, int y, std::string path); Player(int x, int y, std::string path);
void shoot(); void shoot() override;
void setFirerate(unsigned int firerate); void setFirerate(unsigned int firerate);
void move(float deltaX, float deltaY); void move(float deltaX, float deltaY) override;
void moveLeft(); void moveLeft() override;
void moveRight(); void moveRight() override;
void moveUp(); void moveUp() override;
void moveDown(); void moveDown() override;
private: private:
std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now();
}; };

View File

@@ -41,7 +41,6 @@ int main()
} }
} }
// Sprawdzanie stanu klawiszy w głównej pętli gry
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
if(ship.getPosition().x > -10) { if(ship.getPosition().x > -10) {
ship.moveLeft(); ship.moveLeft();

View File

@@ -1,5 +1,6 @@
#include "../headers/Actor.h" #include "../headers/Actor.h"
// TODO: Naprawić krzywy sprite statku
Actor::Actor(int x, int y, std::string path) { Actor::Actor(int x, int y, std::string path) {
loadTexture(path); loadTexture(path);
position.x = x; position.x = x;
@@ -11,25 +12,20 @@ void Actor::loadTexture(std::string path) {
actorTexture.loadFromFile(path); actorTexture.loadFromFile(path);
actorSprite.setTexture(actorTexture); actorSprite.setTexture(actorTexture);
bulletTexture.loadFromFile("../assets/img/bullet.png"); bulletTextureLeft.loadFromFile("../assets/img/bullet.png");
bulletTextureRight.loadFromFile("../assets/img/bullet_right.png");
} }
sf::Sprite &Actor::getSprite() { sf::Sprite &Actor::getSprite() {
return actorSprite; return actorSprite;
} }
void Actor::move(float deltaX, float deltaY) {}
void Actor::moveLeft() {}
void Actor::moveRight() {}
Position Actor::getPosition() { Position Actor::getPosition() {
return {position.x, position.y}; return {position.x, position.y};
} }
void Actor::shoot() { void Actor::shoot() {
bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2-62, position.y, bulletTexture); bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2-62, position.y, bulletTextureLeft);
} }
std::vector<Bullet> &Actor::getBullets() { std::vector<Bullet> &Actor::getBullets() {

View File

@@ -5,7 +5,7 @@ Player::Player(int x, int y, std::string path) : Actor(x, y, path) {};
void Player::shoot() { void Player::shoot() {
auto now = std::chrono::steady_clock::now(); auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastShotTime).count() >= firerate) { if (std::chrono::duration_cast<std::chrono::milliseconds>(now - lastShotTime).count() >= firerate) {
bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2-62, position.y, bulletTexture); bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2-62, position.y, bulletTextureLeft);
lastShotTime = now; lastShotTime = now;
} }
} }