Dodane regulacja prędkości i płynejsze poruszanie się
This commit is contained in:
@@ -30,12 +30,10 @@ public:
|
|||||||
|
|
||||||
std::vector<Bullet>& getBullets();
|
std::vector<Bullet>& getBullets();
|
||||||
|
|
||||||
void updateBullets() {
|
void updateBullets();
|
||||||
for (auto& bullet : bullets) {
|
|
||||||
if(bullet.getStatus()) {
|
void setMovingSpeed(float speed) {
|
||||||
bullets.erase(bullets.begin());
|
moving_speed = speed;
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#ifndef LOTOSTATEK_BULLET_H
|
#ifndef LOTOSTATEK_BULLET_H
|
||||||
#define LOTOSTATEK_BULLET_H
|
#define LOTOSTATEK_BULLET_H
|
||||||
|
|
||||||
|
|
||||||
#include "SFML/Graphics/Sprite.hpp"
|
#include "SFML/Graphics/Sprite.hpp"
|
||||||
#include "SFML/Graphics/Texture.hpp"
|
#include "SFML/Graphics/Texture.hpp"
|
||||||
|
|
||||||
class Bullet {
|
class Bullet {
|
||||||
struct Position {
|
struct Position {
|
||||||
int x;
|
int x;
|
||||||
@@ -15,9 +15,7 @@ public:
|
|||||||
void update();
|
void update();
|
||||||
sf::Sprite& getSprite();
|
sf::Sprite& getSprite();
|
||||||
void setSpeed(float speed);
|
void setSpeed(float speed);
|
||||||
bool getStatus() const {
|
bool getStatus() const;
|
||||||
return outOfBounds;
|
|
||||||
}
|
|
||||||
private:
|
private:
|
||||||
sf::Sprite bulletSprite;
|
sf::Sprite bulletSprite;
|
||||||
sf::Texture bulletTexture;
|
sf::Texture bulletTexture;
|
||||||
|
|||||||
@@ -7,8 +7,15 @@
|
|||||||
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 setFirerate(unsigned int firerate);
|
||||||
|
void move(float deltaX, float deltaY);
|
||||||
|
void moveLeft();
|
||||||
|
void moveRight();
|
||||||
|
void moveUp();
|
||||||
|
void moveDown();
|
||||||
private:
|
private:
|
||||||
|
std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
39
main.cpp
39
main.cpp
@@ -1,7 +1,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
#include "SFML/Graphics.hpp"
|
#include "SFML/Graphics.hpp"
|
||||||
#include "headers/Actor.h"
|
|
||||||
#include "headers/Player.h"
|
#include "headers/Player.h"
|
||||||
#include "headers/Bullet.h"
|
#include "headers/Bullet.h"
|
||||||
|
|
||||||
@@ -17,6 +17,8 @@ int main()
|
|||||||
sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła
|
sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła
|
||||||
|
|
||||||
Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
|
Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
|
||||||
|
ship.setMovingSpeed(8);
|
||||||
|
ship.setFirerate(200);
|
||||||
|
|
||||||
while (window.isOpen()) {
|
while (window.isOpen()) {
|
||||||
window.clear();
|
window.clear();
|
||||||
@@ -26,32 +28,39 @@ int main()
|
|||||||
// Tu są handlowane eventy
|
// Tu są handlowane eventy
|
||||||
sf::Event event{};
|
sf::Event event{};
|
||||||
while (window.pollEvent(event)) {
|
while (window.pollEvent(event)) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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();
|
||||||
}
|
}
|
||||||
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
}
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
||||||
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::Space)) {
|
if(ship.getPosition().y > 0) {
|
||||||
ship.shoot();
|
ship.moveUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event.type == sf::Event::MouseButtonPressed) {
|
|
||||||
ship.shoot();
|
|
||||||
}
|
}
|
||||||
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
if(ship.getPosition().y < 700) {
|
||||||
window.close();
|
ship.moveDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type == sf::Event::Closed)
|
|
||||||
window.close();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& bullet : ship.getBullets()) {
|
for (auto& bullet : ship.getBullets()) {
|
||||||
|
|||||||
@@ -18,19 +18,11 @@ sf::Sprite &Actor::getSprite() {
|
|||||||
return actorSprite;
|
return actorSprite;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Actor::move(float deltaX, float deltaY) {
|
void Actor::move(float deltaX, float deltaY) {}
|
||||||
actorSprite.move(deltaX, deltaY);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Actor::moveLeft() {
|
void Actor::moveLeft() {}
|
||||||
move(-10.0f, 0.0f);
|
|
||||||
position.x -= 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Actor::moveRight() {
|
void Actor::moveRight() {}
|
||||||
move(10.0f, 0.0f);
|
|
||||||
position.x += 10;
|
|
||||||
}
|
|
||||||
|
|
||||||
Position Actor::getPosition() {
|
Position Actor::getPosition() {
|
||||||
return {position.x, position.y};
|
return {position.x, position.y};
|
||||||
@@ -43,3 +35,11 @@ void Actor::shoot() {
|
|||||||
std::vector<Bullet> &Actor::getBullets() {
|
std::vector<Bullet> &Actor::getBullets() {
|
||||||
return bullets;
|
return bullets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Actor::updateBullets() {
|
||||||
|
for (auto& bullet : bullets) {
|
||||||
|
if(bullet.getStatus()) {
|
||||||
|
bullets.erase(bullets.begin());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
Bullet::Bullet(float x, float y, sf::Texture &bulletTexture) {
|
Bullet::Bullet(float x, float y, sf::Texture &bulletTexture) {
|
||||||
outOfBounds = false;
|
outOfBounds = false;
|
||||||
// bulletTexture.loadFromFile("../assets/img/bullet.png");
|
|
||||||
bulletSprite.setTexture(bulletTexture);
|
bulletSprite.setTexture(bulletTexture);
|
||||||
bulletSprite.setPosition(x, y);
|
bulletSprite.setPosition(x, y);
|
||||||
bulletSpeed = -10.0f;
|
bulletSpeed = -10.0f;
|
||||||
@@ -21,9 +20,12 @@ sf::Sprite &Bullet::getSprite() {
|
|||||||
|
|
||||||
void Bullet::update() {
|
void Bullet::update() {
|
||||||
bulletSprite.move(0.0f, bulletSpeed);
|
bulletSprite.move(0.0f, bulletSpeed);
|
||||||
bulletPosition.y += bulletSpeed;
|
bulletPosition.y += int(bulletSpeed);
|
||||||
if(bulletPosition.y < -100) {
|
if(bulletPosition.y < -100) {
|
||||||
outOfBounds = true;
|
outOfBounds = true;
|
||||||
// std::cout << "Bullet out of bounds\n";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Bullet::getStatus() const {
|
||||||
|
return outOfBounds;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,39 @@
|
|||||||
#include "../headers/Player.h"
|
#include "../headers/Player.h"
|
||||||
|
|
||||||
Player::Player(int x, int y, std::string path) : Actor(x, y, path) {};
|
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);
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user