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

View File

@@ -7,13 +7,13 @@
class Player : public Actor {
public:
Player(int x, int y, std::string path);
void shoot();
void shoot() override;
void setFirerate(unsigned int firerate);
void move(float deltaX, float deltaY);
void moveLeft();
void moveRight();
void moveUp();
void moveDown();
void move(float deltaX, float deltaY) override;
void moveLeft() override;
void moveRight() override;
void moveUp() override;
void moveDown() override;
private:
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(ship.getPosition().x > -10) {
ship.moveLeft();

View File

@@ -1,5 +1,6 @@
#include "../headers/Actor.h"
// TODO: Naprawić krzywy sprite statku
Actor::Actor(int x, int y, std::string path) {
loadTexture(path);
position.x = x;
@@ -11,25 +12,20 @@ void Actor::loadTexture(std::string path) {
actorTexture.loadFromFile(path);
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() {
return actorSprite;
}
void Actor::move(float deltaX, float deltaY) {}
void Actor::moveLeft() {}
void Actor::moveRight() {}
Position Actor::getPosition() {
return {position.x, position.y};
}
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() {

View File

@@ -5,7 +5,7 @@ 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<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;
}
}