Strzelanie Lewym i Prawym przyciskiem myszy.

Dodanie tekstury strzału alternatywnego
This commit is contained in:
2024-11-18 15:45:57 +01:00
parent fcabc0534a
commit a69dc434a9
6 changed files with 24 additions and 8 deletions

View File

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

After

Width:  |  Height:  |  Size: 7.0 KiB

View File

@@ -2,12 +2,14 @@
#define LOTOSTATEK_PLAYER_H
#include <chrono>
#include "Actor.h"
class Player : public Actor {
public:
Player(int x, int y, std::string path);
void shoot() override;
void alternate_shoot();
void setFirerate(unsigned int firerate);
void move(float deltaX, float deltaY) override;
void moveLeft() override;
@@ -16,6 +18,8 @@ public:
void moveDown() override;
private:
std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now();
std::vector<Bullet> rightBullets;
};

View File

@@ -36,27 +36,31 @@ int main()
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
ship.shoot();
}
if(event.type == sf::Event::MouseButtonPressed) {
ship.shoot();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
if(event.type == sf::Event::MouseButtonPressed) {
if(event.mouseButton.button == sf::Mouse::Left)
ship.shoot();
else
ship.alternate_shoot();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
if(ship.getPosition().x > -10) {
ship.moveLeft();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
if(ship.getPosition().x < 480) {
ship.moveRight();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
if(ship.getPosition().y > 0) {
ship.moveUp();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
if(ship.getPosition().y < 700) {
ship.moveDown();
}

View File

@@ -12,7 +12,7 @@ void Actor::loadTexture(std::string path) {
actorTexture.loadFromFile(path);
actorSprite.setTexture(actorTexture);
bulletTextureLeft.loadFromFile("../assets/img/bullet.png");
bulletTextureLeft.loadFromFile("../assets/img/bullet_left.png");
bulletTextureRight.loadFromFile("../assets/img/bullet_right.png");
}

View File

@@ -10,6 +10,14 @@ void Player::shoot() {
}
}
void Player::alternate_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, bulletTextureRight);
lastShotTime = now;
}
}
void Player::setFirerate(unsigned int firerate) {
this->firerate = firerate;
}