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