Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <vector>
- #include <cassert>
- #include <chrono>
- #include <thread>
- #include <random>
- #include <iostream>
- #include <cmath>
- float pointDirection(sf::Vector2f looker, sf::Vector2f target)
- {
- return 3.12 + std::atan2(looker.y - target.y, looker.x - target.x);
- }
- float distance(sf::Vector2f point1, sf::Vector2f point2)
- {
- return std::sqrt(std::pow(point1.x - point2.x, 2) + std::pow(point1.y - point2.y, 2));
- }
- class Random
- {
- std::default_random_engine engine;
- public:
- Random::Random()
- {
- std::random_device device;
- engine.seed(device());
- }
- Random::Random(long seed)
- {
- engine.seed(seed);
- }
- bool Random::nextBool()
- {
- std::uniform_int_distribution<int> distribution(0, 1);
- return distribution(engine) != 0;
- }
- int Random::nextInt()
- {
- return engine();
- }
- int Random::nextInt(int n)
- {
- std::uniform_int_distribution<int> distribution(0, n - 1);
- return distribution(engine);
- }
- float Random::nextFloat()
- {
- std::uniform_real_distribution<float> distribution(0.f, 1.f);
- return distribution(engine);
- }
- double Random::nextDouble()
- {
- std::uniform_real_distribution<float> distribution(0.f, 1.f);
- return distribution(engine);
- }
- void Random::setSeed(long seed)
- {
- engine.seed(seed);
- }
- int Random::random_range(int lowerBound, int upperBound)
- {
- return nextInt(upperBound) + lowerBound;
- }
- int Random::irandom_range(int lowerBound, int upperBound)
- {
- int number = nextInt((upperBound + 1) - lowerBound) + lowerBound;
- return number;
- }
- };
- class BlockGrid
- {
- friend class Generator;
- std::vector<std::vector<bool>> blocks;
- sf::Vector2i size;
- void setSolid(int x, int y, bool solid)
- {
- assert(x >= 0 && x < size.x && y >= 0 && y < size.y);
- blocks[x][y] = solid;
- }
- public:
- BlockGrid(sf::Vector2i size) : size(size)
- {
- blocks.resize(size.x);
- for (auto & vector : blocks)
- vector.resize(size.y);
- for (int x = 0; x < size.x; ++x)
- for (int y = 0; y < size.y; ++y)
- blocks[x][y] = false;
- }
- bool isSolid(int x, int y)
- {
- assert(x >= 0 && x < size.x && y >= 0 && y < size.y);
- return blocks[x][y];
- }
- sf::Vector2i getSize() {return size;}
- };
- class Generator
- {
- public:
- static void generate(BlockGrid & grid)
- {
- Random random;
- for (int x = 0; x < grid.getSize().x; ++x)
- for (int y = 0; y < grid.getSize().y; ++y)
- if (random.nextBool() && random.nextBool() && random.nextBool())
- grid.setSolid(x, y, true);
- }
- };
- class Bullet
- {
- float speed;
- float direction;
- sf::Vector2f position;
- public:
- Bullet(sf::Vector2f position, float speed, float direction) : position(position), speed(speed), direction(direction) {}
- void move()
- {
- position.x += std::cos(direction)*speed;
- position.y += std::sin(direction)*speed;
- }
- sf::Vector2f getPosition() {return position;}
- bool operator==(const Bullet & other) {return (position == position && speed == speed&& direction == direction);}
- };
- class BulletManager
- {
- std::vector<Bullet> bullets;
- public:
- void addBullet(Bullet bullet) {bullets.push_back(bullet);std::cout << bullets.size() << std::endl;;}
- void removeBullet(Bullet bullet) {bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));}
- void update()
- {
- for (Bullet & bullet : bullets)
- bullet.move();
- }
- void draw(sf::RenderWindow & window)
- {
- static sf::RectangleShape rectangle(sf::Vector2f(1, 1));
- rectangle.setFillColor(sf::Color::Black);
- for (Bullet bullet : bullets)
- {
- rectangle.setPosition(bullet.getPosition());
- window.draw(rectangle);
- }
- }
- std::vector<Bullet>::iterator begin() {return bullets.begin();}
- std::vector<Bullet>::iterator end() {return bullets.end();}
- };
- class Turret
- {
- bool canShoot;
- sf::Vector2f position;
- int shotsPerSecond;
- sf::Clock clock;
- void shoot(sf::Vector2f target, BulletManager & bulletManager)
- {
- float angle = pointDirection(position, target);
- bulletManager.addBullet(Bullet(position, 1, angle));
- }
- public:
- Turret(sf::Vector2i position, int shotsPerSecond) : canShoot(true), position(position), shotsPerSecond(shotsPerSecond) {}
- void update(sf::Vector2f target, BulletManager & bulletManager)
- {
- if (canShoot)
- {
- canShoot = false;
- clock.restart();
- shoot(target, bulletManager);
- }
- if (!canShoot)
- {
- if (clock.getElapsedTime().asSeconds() > 1/shotsPerSecond)
- canShoot = true;
- }
- }
- void draw(sf::RenderWindow & window, int blockSize)
- {
- static sf::RectangleShape rectangle(sf::Vector2f(blockSize, blockSize));
- rectangle.setFillColor(sf::Color(255, 255, 0));
- rectangle.setPosition(position);
- window.draw(rectangle);
- }
- bool operator==(const Turret & other) {return position == other.position && shotsPerSecond == other.shotsPerSecond;}
- };
- class TurretManager
- {
- std::vector<Turret> turrets;
- public:
- void addTurret(Turret turret) {turrets.push_back(turret);}
- void removeTurret(Turret turret) {turrets.erase(std::find(turrets.begin(), turrets.end(), turret));}
- void update(BulletManager & bulletManager)
- {
- for (Turret & turret : turrets)
- turret.update(sf::Vector2f(300, 300), bulletManager);
- }
- void draw(sf::RenderWindow & window, int blockSize)
- {
- for (Turret & turret : turrets)
- turret.draw(window, blockSize);
- }
- std::vector<Turret>::iterator begin() {return turrets.begin();}
- std::vector<Turret>::iterator end() {return turrets.end();}
- };
- class Collision
- {
- public:
- static bool bulletGridCollision(Bullet bullet, BlockGrid & blockGrid, int blockSize)
- {
- if (blockGrid.isSolid(static_cast<int> (bullet.getPosition().x/blockSize), static_cast<int> (bullet.getPosition().y/blockSize)))
- {
- return true;
- }
- }
- };
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(500, 500), "Gun Game");
- Random random;
- sf::Clock clock;
- BulletManager bulletManager;
- TurretManager turretManager;
- turretManager.addTurret(Turret(sf::Vector2i(200, 200), 5));
- BlockGrid blockGrid(sf::Vector2i(20, 20));
- Generator::generate(blockGrid);
- sf::RectangleShape rectangle(sf::Vector2f(25, 25));
- while (true)
- {
- if (clock.getElapsedTime().asSeconds() > 0.05)
- {
- //UPDATES
- sf::Event evt;
- while (window.pollEvent(evt))
- {
- if (evt.type == sf::Event::Closed)
- return 0;
- }
- bulletManager.update();
- turretManager.update(bulletManager);
- float angle = pointDirection(sf::Vector2f(window.getSize().x/2, window.getSize().y/2), sf::Vector2f(sf::Mouse::getPosition(window).x, sf::Mouse::getPosition(window).y));
- bulletManager.addBullet(Bullet(sf::Vector2f(window.getSize().x/2, window.getSize().y/2), 1, angle));
- std::vector<Bullet> toRemove;
- for (Bullet & bullet : bulletManager)
- {
- if (bullet.getPosition().x < 0 || bullet.getPosition().x >= window.getSize().x || bullet.getPosition().y < 0 || bullet.getPosition().y >= window.getSize().y)
- {
- toRemove.push_back(bullet);
- continue;
- }
- if (Collision::bulletGridCollision(bullet, blockGrid, 25))
- toRemove.push_back(bullet);
- }
- for (Bullet & bullet : toRemove)
- bulletManager.removeBullet(bullet);
- //DRAW
- window.clear(sf::Color::White);
- for (int x = 0; x < blockGrid.getSize().x; ++x)
- for (int y = 0; y < blockGrid.getSize().y; ++y)
- {
- if (blockGrid.isSolid(x, y))
- rectangle.setFillColor(sf::Color::Black);
- else
- rectangle.setFillColor(sf::Color::White);
- rectangle.setPosition(x*25, window.getSize().y - (25*y + 25));
- window.draw(rectangle);
- }
- bulletManager.draw(window);
- turretManager.draw(window, 25);
- window.display();
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(10));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement