Strzelanie Lewym i Prawym przyciskiem myszy.
Dodanie tekstury strzału alternatywnego
This commit is contained in:
|
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 |
@@ -2,12 +2,14 @@
|
|||||||
#define LOTOSTATEK_PLAYER_H
|
#define LOTOSTATEK_PLAYER_H
|
||||||
|
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include "Actor.h"
|
#include "Actor.h"
|
||||||
|
|
||||||
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() override;
|
void shoot() override;
|
||||||
|
void alternate_shoot();
|
||||||
void setFirerate(unsigned int firerate);
|
void setFirerate(unsigned int firerate);
|
||||||
void move(float deltaX, float deltaY) override;
|
void move(float deltaX, float deltaY) override;
|
||||||
void moveLeft() override;
|
void moveLeft() override;
|
||||||
@@ -16,6 +18,8 @@ public:
|
|||||||
void moveDown() override;
|
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();
|
||||||
|
std::vector<Bullet> rightBullets;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
18
main.cpp
18
main.cpp
@@ -36,27 +36,31 @@ int main()
|
|||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
|
||||||
ship.shoot();
|
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) {
|
if(ship.getPosition().x > -10) {
|
||||||
ship.moveLeft();
|
ship.moveLeft();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
|
||||||
if(ship.getPosition().x < 480) {
|
if(ship.getPosition().x < 480) {
|
||||||
ship.moveRight();
|
ship.moveRight();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
|
||||||
if(ship.getPosition().y > 0) {
|
if(ship.getPosition().y > 0) {
|
||||||
ship.moveUp();
|
ship.moveUp();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
|
||||||
if(ship.getPosition().y < 700) {
|
if(ship.getPosition().y < 700) {
|
||||||
ship.moveDown();
|
ship.moveDown();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ void Actor::loadTexture(std::string path) {
|
|||||||
actorTexture.loadFromFile(path);
|
actorTexture.loadFromFile(path);
|
||||||
actorSprite.setTexture(actorTexture);
|
actorSprite.setTexture(actorTexture);
|
||||||
|
|
||||||
bulletTextureLeft.loadFromFile("../assets/img/bullet.png");
|
bulletTextureLeft.loadFromFile("../assets/img/bullet_left.png");
|
||||||
bulletTextureRight.loadFromFile("../assets/img/bullet_right.png");
|
bulletTextureRight.loadFromFile("../assets/img/bullet_right.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
void Player::setFirerate(unsigned int firerate) {
|
||||||
this->firerate = firerate;
|
this->firerate = firerate;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user