Advertisement
Guest User

Teh Codez

a guest
Sep 28th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.78 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <vector>
  3. #include <cassert>
  4. #include <chrono>
  5. #include <thread>
  6. #include <random>
  7. #include <iostream>
  8. #include <cmath>
  9.  
  10. float pointDirection(sf::Vector2f looker, sf::Vector2f target)
  11. {
  12. return 3.12 + std::atan2(looker.y - target.y, looker.x - target.x);
  13. }
  14.  
  15. float distance(sf::Vector2f point1, sf::Vector2f point2)
  16. {
  17. return std::sqrt(std::pow(point1.x - point2.x, 2) + std::pow(point1.y - point2.y, 2));
  18. }
  19.  
  20. class Random
  21. {
  22. std::default_random_engine engine;
  23.  
  24. public:
  25. Random::Random()
  26. {
  27. std::random_device device;
  28.  
  29. engine.seed(device());
  30. }
  31.  
  32. Random::Random(long seed)
  33. {
  34. engine.seed(seed);
  35. }
  36.  
  37. bool Random::nextBool()
  38. {
  39. std::uniform_int_distribution<int> distribution(0, 1);
  40.  
  41. return distribution(engine) != 0;
  42. }
  43.  
  44. int Random::nextInt()
  45. {
  46. return engine();
  47. }
  48.  
  49. int Random::nextInt(int n)
  50. {
  51. std::uniform_int_distribution<int> distribution(0, n - 1);
  52.  
  53. return distribution(engine);
  54. }
  55.  
  56.  
  57. float Random::nextFloat()
  58. {
  59. std::uniform_real_distribution<float> distribution(0.f, 1.f);
  60.  
  61. return distribution(engine);
  62. }
  63.  
  64. double Random::nextDouble()
  65. {
  66. std::uniform_real_distribution<float> distribution(0.f, 1.f);
  67.  
  68. return distribution(engine);
  69. }
  70.  
  71. void Random::setSeed(long seed)
  72. {
  73. engine.seed(seed);
  74. }
  75.  
  76. int Random::random_range(int lowerBound, int upperBound)
  77. {
  78. return nextInt(upperBound) + lowerBound;
  79. }
  80.  
  81. int Random::irandom_range(int lowerBound, int upperBound)
  82. {
  83. int number = nextInt((upperBound + 1) - lowerBound) + lowerBound;
  84.  
  85. return number;
  86. }
  87. };
  88.  
  89. class BlockGrid
  90. {
  91. friend class Generator;
  92.  
  93. std::vector<std::vector<bool>> blocks;
  94.  
  95. sf::Vector2i size;
  96.  
  97. void setSolid(int x, int y, bool solid)
  98. {
  99. assert(x >= 0 && x < size.x && y >= 0 && y < size.y);
  100.  
  101. blocks[x][y] = solid;
  102. }
  103.  
  104. public:
  105. BlockGrid(sf::Vector2i size) : size(size)
  106. {
  107. blocks.resize(size.x);
  108.  
  109. for (auto & vector : blocks)
  110. vector.resize(size.y);
  111.  
  112. for (int x = 0; x < size.x; ++x)
  113. for (int y = 0; y < size.y; ++y)
  114. blocks[x][y] = false;
  115. }
  116.  
  117. bool isSolid(int x, int y)
  118. {
  119. assert(x >= 0 && x < size.x && y >= 0 && y < size.y);
  120.  
  121. return blocks[x][y];
  122. }
  123.  
  124. sf::Vector2i getSize() {return size;}
  125. };
  126.  
  127. class Generator
  128. {
  129. public:
  130. static void generate(BlockGrid & grid)
  131. {
  132. Random random;
  133.  
  134. for (int x = 0; x < grid.getSize().x; ++x)
  135. for (int y = 0; y < grid.getSize().y; ++y)
  136. if (random.nextBool() && random.nextBool() && random.nextBool())
  137. grid.setSolid(x, y, true);
  138. }
  139. };
  140.  
  141. class Bullet
  142. {
  143. float speed;
  144. float direction;
  145.  
  146. sf::Vector2f position;
  147.  
  148. public:
  149. Bullet(sf::Vector2f position, float speed, float direction) : position(position), speed(speed), direction(direction) {}
  150.  
  151. void move()
  152. {
  153. position.x += std::cos(direction)*speed;
  154. position.y += std::sin(direction)*speed;
  155. }
  156.  
  157. sf::Vector2f getPosition() {return position;}
  158.  
  159. bool operator==(const Bullet & other) {return (position == position && speed == speed&& direction == direction);}
  160. };
  161.  
  162. class BulletManager
  163. {
  164. std::vector<Bullet> bullets;
  165.  
  166. public:
  167. void addBullet(Bullet bullet) {bullets.push_back(bullet);std::cout << bullets.size() << std::endl;;}
  168.  
  169. void removeBullet(Bullet bullet) {bullets.erase(std::find(bullets.begin(), bullets.end(), bullet));}
  170.  
  171. void update()
  172. {
  173. for (Bullet & bullet : bullets)
  174. bullet.move();
  175. }
  176.  
  177. void draw(sf::RenderWindow & window)
  178. {
  179. static sf::RectangleShape rectangle(sf::Vector2f(1, 1));
  180.  
  181. rectangle.setFillColor(sf::Color::Black);
  182.  
  183. for (Bullet bullet : bullets)
  184. {
  185. rectangle.setPosition(bullet.getPosition());
  186.  
  187. window.draw(rectangle);
  188. }
  189. }
  190.  
  191. std::vector<Bullet>::iterator begin() {return bullets.begin();}
  192. std::vector<Bullet>::iterator end() {return bullets.end();}
  193. };
  194.  
  195. class Turret
  196. {
  197. bool canShoot;
  198.  
  199. sf::Vector2f position;
  200.  
  201. int shotsPerSecond;
  202.  
  203. sf::Clock clock;
  204.  
  205. void shoot(sf::Vector2f target, BulletManager & bulletManager)
  206. {
  207. float angle = pointDirection(position, target);
  208.  
  209. bulletManager.addBullet(Bullet(position, 1, angle));
  210. }
  211.  
  212. public:
  213. Turret(sf::Vector2i position, int shotsPerSecond) : canShoot(true), position(position), shotsPerSecond(shotsPerSecond) {}
  214.  
  215. void update(sf::Vector2f target, BulletManager & bulletManager)
  216. {
  217. if (canShoot)
  218. {
  219. canShoot = false;
  220.  
  221. clock.restart();
  222.  
  223. shoot(target, bulletManager);
  224. }
  225.  
  226. if (!canShoot)
  227. {
  228. if (clock.getElapsedTime().asSeconds() > 1/shotsPerSecond)
  229. canShoot = true;
  230. }
  231. }
  232.  
  233. void draw(sf::RenderWindow & window, int blockSize)
  234. {
  235. static sf::RectangleShape rectangle(sf::Vector2f(blockSize, blockSize));
  236.  
  237. rectangle.setFillColor(sf::Color(255, 255, 0));
  238.  
  239. rectangle.setPosition(position);
  240.  
  241. window.draw(rectangle);
  242. }
  243.  
  244. bool operator==(const Turret & other) {return position == other.position && shotsPerSecond == other.shotsPerSecond;}
  245. };
  246.  
  247. class TurretManager
  248. {
  249. std::vector<Turret> turrets;
  250.  
  251. public:
  252. void addTurret(Turret turret) {turrets.push_back(turret);}
  253.  
  254. void removeTurret(Turret turret) {turrets.erase(std::find(turrets.begin(), turrets.end(), turret));}
  255.  
  256. void update(BulletManager & bulletManager)
  257. {
  258. for (Turret & turret : turrets)
  259. turret.update(sf::Vector2f(300, 300), bulletManager);
  260. }
  261.  
  262. void draw(sf::RenderWindow & window, int blockSize)
  263. {
  264. for (Turret & turret : turrets)
  265. turret.draw(window, blockSize);
  266. }
  267.  
  268. std::vector<Turret>::iterator begin() {return turrets.begin();}
  269. std::vector<Turret>::iterator end() {return turrets.end();}
  270. };
  271.  
  272. class Collision
  273. {
  274. public:
  275. static bool bulletGridCollision(Bullet bullet, BlockGrid & blockGrid, int blockSize)
  276. {
  277. if (blockGrid.isSolid(static_cast<int> (bullet.getPosition().x/blockSize), static_cast<int> (bullet.getPosition().y/blockSize)))
  278. {
  279. return true;
  280. }
  281. }
  282. };
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289. int main()
  290. {
  291. sf::RenderWindow window(sf::VideoMode(500, 500), "Gun Game");
  292.  
  293. Random random;
  294.  
  295. sf::Clock clock;
  296.  
  297. BulletManager bulletManager;
  298. TurretManager turretManager;
  299.  
  300. turretManager.addTurret(Turret(sf::Vector2i(200, 200), 5));
  301.  
  302. BlockGrid blockGrid(sf::Vector2i(20, 20));
  303.  
  304. Generator::generate(blockGrid);
  305.  
  306. sf::RectangleShape rectangle(sf::Vector2f(25, 25));
  307.  
  308. while (true)
  309. {
  310. if (clock.getElapsedTime().asSeconds() > 0.05)
  311. {
  312. //UPDATES
  313.  
  314. sf::Event evt;
  315.  
  316. while (window.pollEvent(evt))
  317. {
  318. if (evt.type == sf::Event::Closed)
  319. return 0;
  320. }
  321.  
  322. bulletManager.update();
  323. turretManager.update(bulletManager);
  324.  
  325. 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));
  326.  
  327. bulletManager.addBullet(Bullet(sf::Vector2f(window.getSize().x/2, window.getSize().y/2), 1, angle));
  328.  
  329. std::vector<Bullet> toRemove;
  330.  
  331. for (Bullet & bullet : bulletManager)
  332. {
  333. if (bullet.getPosition().x < 0 || bullet.getPosition().x >= window.getSize().x || bullet.getPosition().y < 0 || bullet.getPosition().y >= window.getSize().y)
  334. {
  335. toRemove.push_back(bullet);
  336.  
  337. continue;
  338. }
  339.  
  340. if (Collision::bulletGridCollision(bullet, blockGrid, 25))
  341. toRemove.push_back(bullet);
  342. }
  343.  
  344. for (Bullet & bullet : toRemove)
  345. bulletManager.removeBullet(bullet);
  346.  
  347. //DRAW
  348.  
  349. window.clear(sf::Color::White);
  350.  
  351. for (int x = 0; x < blockGrid.getSize().x; ++x)
  352. for (int y = 0; y < blockGrid.getSize().y; ++y)
  353. {
  354. if (blockGrid.isSolid(x, y))
  355. rectangle.setFillColor(sf::Color::Black);
  356. else
  357. rectangle.setFillColor(sf::Color::White);
  358.  
  359. rectangle.setPosition(x*25, window.getSize().y - (25*y + 25));
  360.  
  361. window.draw(rectangle);
  362. }
  363.  
  364. bulletManager.draw(window);
  365. turretManager.draw(window, 25);
  366.  
  367. window.display();
  368. }
  369.  
  370. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  371. }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement