Compare commits
3 Commits
8ecd82eb4e
...
a190e81ba2
| Author | SHA1 | Date | |
|---|---|---|---|
| a190e81ba2 | |||
| 88fa5901b6 | |||
| ce2924e635 |
@@ -7,7 +7,11 @@ add_executable(LotoStatek main.cpp
|
||||
headers/Actor.h
|
||||
headers/Plansza.h
|
||||
sources/Actor.cpp
|
||||
sources/Plansza.cpp)
|
||||
sources/Plansza.cpp
|
||||
sources/Player.cpp
|
||||
headers/Player.h
|
||||
headers/Bullet.h
|
||||
sources/Bullet.cpp)
|
||||
|
||||
if(WIN32)
|
||||
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML")
|
||||
|
||||
BIN
assets/img/bullet.png
Normal file
BIN
assets/img/bullet.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.5 KiB |
BIN
assets/img/space.jpg
Normal file
BIN
assets/img/space.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 169 KiB |
BIN
assets/ship/Dreadnought-Base.png
Normal file
BIN
assets/ship/Dreadnought-Base.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.0 KiB |
BIN
assets/ship/Fighter-Base.png
Normal file
BIN
assets/ship/Fighter-Base.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 773 B |
@@ -1,9 +1,51 @@
|
||||
#ifndef ACTOR_H
|
||||
#define ACTOR_H
|
||||
|
||||
#include "SFML/Graphics/Sprite.hpp"
|
||||
#include "SFML/Graphics/Texture.hpp"
|
||||
#include "Bullet.h"
|
||||
|
||||
struct Position {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
class Actor {
|
||||
public:
|
||||
Actor(int x, int y, std::string path);
|
||||
|
||||
void loadTexture(std::string path);
|
||||
|
||||
sf::Sprite& getSprite();
|
||||
|
||||
Position getPosition();
|
||||
|
||||
void move(float deltaX, float deltaY);
|
||||
|
||||
void moveLeft();
|
||||
|
||||
void moveRight();
|
||||
|
||||
void shoot();
|
||||
|
||||
std::vector<Bullet>& getBullets();
|
||||
|
||||
void updateBullets();
|
||||
|
||||
void setMovingSpeed(float speed) {
|
||||
moving_speed = speed;
|
||||
}
|
||||
|
||||
protected:
|
||||
sf::Sprite actorSprite;
|
||||
sf::Texture actorTexture;
|
||||
sf::Texture bulletTexture;
|
||||
Position position;
|
||||
unsigned int hp;
|
||||
unsigned int damage;
|
||||
unsigned int firerate;
|
||||
float moving_speed;
|
||||
std::vector<Bullet> bullets;
|
||||
};
|
||||
|
||||
#endif //ACTOR_H
|
||||
|
||||
28
headers/Bullet.h
Normal file
28
headers/Bullet.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef LOTOSTATEK_BULLET_H
|
||||
#define LOTOSTATEK_BULLET_H
|
||||
|
||||
#include "SFML/Graphics/Sprite.hpp"
|
||||
#include "SFML/Graphics/Texture.hpp"
|
||||
|
||||
class Bullet {
|
||||
struct Position {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
public:
|
||||
Bullet(float x, float y, sf::Texture &bulletTexture);
|
||||
void update();
|
||||
sf::Sprite& getSprite();
|
||||
void setSpeed(float speed);
|
||||
bool getStatus() const;
|
||||
private:
|
||||
sf::Sprite bulletSprite;
|
||||
sf::Texture bulletTexture;
|
||||
float bulletSpeed;
|
||||
Position bulletPosition;
|
||||
bool outOfBounds;
|
||||
};
|
||||
|
||||
|
||||
#endif //LOTOSTATEK_BULLET_H
|
||||
22
headers/Player.h
Normal file
22
headers/Player.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#ifndef LOTOSTATEK_PLAYER_H
|
||||
#define LOTOSTATEK_PLAYER_H
|
||||
|
||||
|
||||
#include "Actor.h"
|
||||
|
||||
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();
|
||||
};
|
||||
|
||||
|
||||
#endif //LOTOSTATEK_PLAYER_H
|
||||
78
main.cpp
78
main.cpp
@@ -1,51 +1,75 @@
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
|
||||
#include "SFML/Graphics.hpp"
|
||||
#include "headers/Player.h"
|
||||
#include "headers/Bullet.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << "Game started\n";
|
||||
std::clog << "Game started\n";
|
||||
sf::RenderWindow window(sf::VideoMode(600, 800), "My window");
|
||||
window.setVerticalSyncEnabled(true);
|
||||
window.setFramerateLimit(60);
|
||||
|
||||
sf::RectangleShape rectangle(sf::Vector2f(120.0f, 50.0f));
|
||||
float sizeIncrement = 1.0f;
|
||||
sf::Texture backgroundTexture;
|
||||
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
|
||||
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();
|
||||
|
||||
if(rectangle.getSize().x > 5 && rectangle.getSize().y > 5 && rectangle.getSize().x < 300 && rectangle.getSize().y < 300) {
|
||||
rectangle.setSize(rectangle.getSize() + sf::Vector2f(sizeIncrement, sizeIncrement));
|
||||
rectangle.setPosition(
|
||||
static_cast<float>(window.getSize().x) / 2 - rectangle.getSize().x / 2,
|
||||
static_cast<float>(window.getSize().y) / 2 - rectangle.getSize().y / 2
|
||||
);
|
||||
} else {
|
||||
rectangle.setSize(sf::Vector2f(120.0f, 50.0f));
|
||||
rectangle.setPosition(
|
||||
static_cast<float>(window.getSize().x) / 2 - rectangle.getSize().x / 2,
|
||||
static_cast<float>(window.getSize().y) / 2 - rectangle.getSize().y / 2
|
||||
);
|
||||
}
|
||||
|
||||
window.draw(backgroundSprite); // narysuj tło
|
||||
|
||||
// Tu są handlowane eventy
|
||||
sf::Event event{};
|
||||
while (window.pollEvent(event)) {
|
||||
if(event.type == sf::Event::MouseWheelScrolled) {
|
||||
if(event.mouseWheelScroll.delta > 0) {
|
||||
sizeIncrement = 1.0f;
|
||||
} else {
|
||||
sizeIncrement = -1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type == sf::Event::Closed)
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
window.draw(rectangle);
|
||||
// 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()) {
|
||||
bullet.update();
|
||||
window.draw(bullet.getSprite());
|
||||
}
|
||||
|
||||
ship.updateBullets();
|
||||
window.draw(ship.getSprite());
|
||||
window.display();
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1,45 @@
|
||||
#include "../headers/actor.h"
|
||||
#include "../headers/Actor.h"
|
||||
|
||||
Actor::Actor(int x, int y, std::string path) {
|
||||
loadTexture(path);
|
||||
position.x = x;
|
||||
position.y = y;
|
||||
actorSprite.setPosition(x, y);
|
||||
}
|
||||
|
||||
void Actor::loadTexture(std::string path) {
|
||||
actorTexture.loadFromFile(path);
|
||||
actorSprite.setTexture(actorTexture);
|
||||
|
||||
bulletTexture.loadFromFile("../assets/img/bullet.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);
|
||||
}
|
||||
|
||||
std::vector<Bullet> &Actor::getBullets() {
|
||||
return bullets;
|
||||
}
|
||||
|
||||
void Actor::updateBullets() {
|
||||
for (auto& bullet : bullets) {
|
||||
if(bullet.getStatus()) {
|
||||
bullets.erase(bullets.begin());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
31
sources/Bullet.cpp
Normal file
31
sources/Bullet.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include "../headers/Bullet.h"
|
||||
|
||||
Bullet::Bullet(float x, float y, sf::Texture &bulletTexture) {
|
||||
outOfBounds = false;
|
||||
bulletSprite.setTexture(bulletTexture);
|
||||
bulletSprite.setPosition(x, y);
|
||||
bulletSpeed = -10.0f;
|
||||
bulletPosition.x = x;
|
||||
bulletPosition.y = y;
|
||||
}
|
||||
|
||||
void Bullet::setSpeed(float speed) {
|
||||
bulletSpeed = speed;
|
||||
}
|
||||
|
||||
sf::Sprite &Bullet::getSprite() {
|
||||
return bulletSprite;
|
||||
}
|
||||
|
||||
void Bullet::update() {
|
||||
bulletSprite.move(0.0f, bulletSpeed);
|
||||
bulletPosition.y += int(bulletSpeed);
|
||||
if(bulletPosition.y < -100) {
|
||||
outOfBounds = true;
|
||||
}
|
||||
}
|
||||
|
||||
bool Bullet::getStatus() const {
|
||||
return outOfBounds;
|
||||
}
|
||||
39
sources/Player.cpp
Normal file
39
sources/Player.cpp
Normal file
@@ -0,0 +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