Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.88 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/Audio.hpp>
  4. #include "map.h"
  5. #include "view.h"
  6. #include <vector>
  7. #include <queue>
  8. #include <iostream>
  9. #include <sstream>
  10. #include "mission.h"
  11. #include <windows.h>
  12. using namespace sf;
  13. using namespace std;
  14.  
  15. //Constants
  16. const int Block = 32;
  17.  
  18. //////////////////Point///////////////////////////////
  19. struct point { int x; int y; int num; };
  20.  
  21. //////////////////Player Class////////////////////////
  22. class Player {
  23. public:
  24. //Init Variables
  25. float Width, Heigth, CurrentPX, CurrentPY,
  26. PlayerX, PlayerY, Speed, health, mana, stamina;
  27. int Direction, playerScore, BDir;
  28. int mouseX, mouseY;
  29. const int BarMax = 100;
  30. //Init Variables
  31. bool life;
  32. String File;
  33. Image PlayerIm;
  34. Texture PlayerTex;
  35. Sprite PlayerSpr;
  36. /////////////
  37.  
  38. Player(String F, float X, float Y, float W, float H) {
  39. //Variables
  40. Direction = 0; Speed = 0; playerScore = 0;
  41. health = 100; CurrentPX = 0; CurrentPY = 0;
  42. mana = 100; stamina = 100; life = true;
  43. File = F; Width = W; Heigth = H;
  44. //Set Texture
  45. PlayerIm.loadFromFile("img/gera.png");
  46. PlayerIm.createMaskFromColor(Color::White);
  47. PlayerTex.loadFromImage(PlayerIm);
  48. PlayerSpr.setTexture(PlayerTex);
  49. //Set Player Position
  50. PlayerX = X;
  51. PlayerY = Y;
  52. PlayerSpr.setOrigin(Width / 4, Heigth / 4);
  53. }
  54.  
  55. void update(float time)
  56. {
  57. //cout << PlayerX << " " << PlayerY << endl;
  58. //Player Direction
  59. switch (Direction)
  60. {
  61. case 0: CurrentPX = Speed; CurrentPY = 0; break;
  62. case 1: CurrentPX = -Speed; CurrentPY = 0; break;
  63. case 2: CurrentPX = 0; CurrentPY = Speed; break;
  64. case 3: CurrentPX = 0; CurrentPY = -Speed; break;
  65. }
  66. //Player Speed
  67. PlayerX += CurrentPX * time;
  68. PlayerY += CurrentPY * time;
  69. Speed = 0;
  70. PlayerSpr.setPosition(PlayerX, PlayerY);
  71. //Collision
  72. interactionWithMap(time);
  73. //Life Check
  74. if (health <= 0) { life = false; }
  75. //ManaRegen
  76. if (mana + 0.0005*time <= BarMax) {
  77. mana += 0.0005*time;
  78. }
  79. else {
  80. mana = 100;
  81. }
  82. //StaminaRegen
  83. if (stamina + 0.0020*time <= BarMax) {
  84. stamina += 0.0020*time;
  85. }
  86. else {
  87. stamina = 100;
  88. }
  89. //HPRegen
  90. if (health + 0.0005*time <= BarMax) {
  91. health += 0.0005*time;
  92. }
  93. else {
  94. health = 100;
  95. }
  96. }
  97. //X Player
  98. float getplayercoordinateX() {
  99. return PlayerX;
  100. }
  101. //Y Player
  102. float getplayercoordinateY() {
  103. return PlayerY;
  104. }
  105. //Collision
  106. void interactionWithMap(int time)
  107. {
  108. for (int i = PlayerY / Block; i < (PlayerY + Heigth / 4) / Block; i++)
  109. for (int j = PlayerX / Block; j < (PlayerX + Width / 4) / Block; j++)
  110. {
  111. if (TileMap[i][j] == '0' || TileMap[i][j] == 'w')
  112. {
  113. if (CurrentPY > 0)
  114. {
  115. Speed = 0;
  116. PlayerY = i * Block - Heigth / 4;
  117. }
  118. if (CurrentPY < 0)
  119. {
  120. Speed = 0;
  121. PlayerY = i * Block + Block;
  122. }
  123. if (CurrentPX > 0)
  124. {
  125. Speed = 0;
  126. PlayerX = j * Block - Width / 4;
  127. }
  128. if (CurrentPX < 0)
  129. {
  130. Speed = 0;
  131. PlayerX = j * Block + Block;
  132. }
  133. }
  134.  
  135.  
  136.  
  137. if (TileMap[i][j] == 's') {
  138. playerScore++;
  139. TileMap[i][j] = ' ';
  140. }
  141. if (TileMap[i][j] == 'f') {
  142. health -= 40;
  143. TileMap[i][j] = ' ';
  144. }
  145. if (TileMap[i][j] == 'h') {
  146. if (health + 20 <= 100) {
  147. health += 20;
  148. TileMap[i][j] = ' ';
  149. }
  150. }
  151.  
  152. }
  153. }
  154.  
  155. };
  156. /////////////////Entity Class/////////////////////////
  157. class Entity {
  158. public:
  159. vector<Sprite> obj;
  160. float dx, dy, x, y, speed, moveTimer;
  161. int w, h, health;
  162. bool life, onGround;
  163. Texture texture;
  164. Sprite sprite;
  165. String name;
  166. Entity(Image &image, String Name, float X, float Y, int W, int H) {
  167. x = X; y = Y; w = W; h = H; name = Name; moveTimer = 0;
  168. speed = 0; health = 100; dx = 0; dy = 0;
  169. life = true; onGround = false;
  170. texture.loadFromImage(image);
  171. sprite.setTexture(texture);
  172. sprite.setOrigin(w / 2, h / 2);
  173. }
  174.  
  175. FloatRect getRect() {
  176. return FloatRect(x, y, w, h);
  177. }
  178.  
  179. virtual void update(float time) = 0;//все потомки переопределяют эту ф-цию
  180.  
  181. };
  182.  
  183.  
  184.  
  185. void GoToPos(Player& p, float X, float Y, float time) {
  186. bool isMove = true;
  187. p.PlayerX += (p.PlayerX - X) * time;
  188. p.PlayerY += (p.PlayerX - Y) * time;
  189.  
  190. //cout << distance << endl;
  191. }
  192.  
  193.  
  194.  
  195. /////////////////////Graphs/////////////////////////////
  196. struct graph {
  197.  
  198. graph(size_t nodes)
  199. : m_adjacency_list(nodes) {
  200. }
  201.  
  202. size_t number_of_nodes() const {
  203. return m_adjacency_list.size();
  204. }
  205.  
  206. std::vector<size_t> const& neighbours_of(size_t node) const {
  207. return m_adjacency_list.at(node);
  208. }
  209.  
  210. void add_edge(size_t from, size_t to) {
  211. std::vector<size_t>& al = m_adjacency_list.at(from);
  212. if (to >= m_adjacency_list.size())
  213. throw std::runtime_error("Tried to add edge to non-existant node");
  214. for (size_t i = 0; i < al.size(); ++i) if (al[i] == to) return;
  215. al.push_back(to);
  216. }
  217.  
  218. private:
  219. std::vector<std::vector<size_t>> m_adjacency_list;
  220. };
  221. size_t searchPos(size_t startT, size_t targeT, graph GraphMap) {
  222. vector<size_t> reached_by(GraphMap.number_of_nodes(), GraphMap.number_of_nodes());
  223. queue<size_t> q;
  224. size_t start = startT;
  225. size_t target = targeT;
  226. reached_by[start] = start;
  227. q.push(start);
  228. while (!q.empty()) {
  229. size_t node = q.front();
  230. q.pop();
  231. for (size_t i = 0; i < GraphMap.neighbours_of(node).size(); ++i) {
  232. size_t candidate = GraphMap.neighbours_of(node)[i];
  233. if (reached_by[candidate] == GraphMap.number_of_nodes()) {
  234. reached_by[candidate] = node;
  235. if (candidate == target) break;
  236. q.push(candidate);
  237. }
  238. }
  239. }
  240. bool first_el = true;
  241. size_t need_num = 0;
  242.  
  243. if (reached_by[target] == GraphMap.number_of_nodes())
  244. cout << "No path to " << target << " found!" << endl;
  245. // TODO: Сделать что-то в случае не нахождения маршрута
  246. else {
  247. //cout << "Path to " << target << ": ";
  248. for (size_t node = target; node != start; node = reached_by[node]) {
  249. //cout << node << " <- ";
  250. need_num = node;
  251. }
  252.  
  253. return need_num;
  254. }
  255.  
  256. }
  257. point searchGraphCoords(int needNum) {
  258. int freeNum = 0;
  259. for (int i = 0; i < HEIGHT_MAP; i++)
  260. for (int j = 0; j < WIDTH_MAP; j++)
  261. {
  262. if (TileMap[i][j] == ' ') {
  263. freeNum++;
  264. if (needNum == freeNum) {
  265. point coords;
  266. coords.x = j*Block;
  267. coords.y = i*Block;
  268. coords.num = freeNum;
  269. //cout << coords.x << " " << coords.y << endl;
  270. return coords;
  271.  
  272. }
  273. }
  274.  
  275.  
  276. }
  277.  
  278. }
  279. point getPlayerGraph(Player p) {
  280. int freeNum = 0;
  281. for (int i = 0; i < HEIGHT_MAP; i++)
  282. for (int j = 0; j < WIDTH_MAP; j++)
  283. {
  284. if (TileMap[i][j] == ' ') {
  285. freeNum++;
  286. if (p.PlayerX - 32 < j*32 && p.PlayerX + 32 > j*32 &&
  287. p.PlayerY - 32 < i*32 && p.PlayerY + 32 > i*32) {
  288. point coords;
  289. coords.x = i*Block;
  290. coords.y = j*Block;
  291. coords.num = freeNum;
  292. /*cout << coords.num << endl;
  293. cout << p.PlayerX << " " << p.PlayerY << endl;*/
  294. return coords;
  295.  
  296. }
  297. }
  298.  
  299.  
  300. }
  301.  
  302. }
  303. /////////////////////GUI////////////////////////////////
  304. class GUI {
  305. public:
  306. Image Bgi;
  307. Texture Bgt;
  308. Sprite BGs;
  309. String File;
  310. GUI(String F) {
  311. Bgi.loadFromFile("img/bg.jpg");
  312. Bgt.loadFromImage(Bgi);
  313. BGs.setTexture(Bgt);
  314. File = F;
  315. }
  316.  
  317. };
  318.  
  319. int main()
  320. {
  321.  
  322. //Init Variables
  323. const int TSizeGiant = 50;
  324. const int TSizeBig = 20;
  325. const int TSizeSmall = 15;
  326. float dX = 0;
  327. float dY = 0;
  328. int sizeM = 0;
  329. int massiveTile[40][160];
  330. //////////GUI
  331. GUI bg("img/bg.jpg");
  332.  
  333. //АКНО
  334. RenderWindow window(VideoMode(1920, 1080), "Game");
  335. view.reset(FloatRect(0, 0, 640, 480));
  336. //Sound
  337. SoundBuffer buffer;
  338. buffer.loadFromFile("Sound/Punch.flac");
  339. Sound sound;
  340. sound.setBuffer(buffer);
  341. //Font
  342. Font BFont, GUI;
  343. BFont.loadFromFile("fonts/CyrillicOldBold.ttf");
  344. GUI.loadFromFile("fonts/GUI.otf");
  345. Text Play("", GUI, TSizeGiant);
  346. Text BarsH("", BFont, TSizeBig);
  347. Text BarsM("", BFont, TSizeBig);
  348. Text BarsS("", BFont, TSizeBig);
  349. Text Quest("", BFont, TSizeSmall);
  350. //Map Elements
  351. Image map_image;
  352. map_image.loadFromFile("img/map.png");
  353. Texture map;
  354. map.loadFromImage(map_image);
  355. Sprite s_map;
  356. s_map.setTexture(map);
  357. //QuestList
  358. Image quest_image;
  359. quest_image.loadFromFile("img/missionbg.jpg");
  360. quest_image.createMaskFromColor(Color(0, 0, 0));
  361. Texture quest_texture;
  362. quest_texture.loadFromImage(quest_image);
  363. Sprite s_quest;
  364. s_quest.setTexture(quest_texture);
  365. s_quest.setTextureRect(IntRect(0, 0, 340, 510));
  366. s_quest.setScale(0.5f, 0.5f);
  367. //Player&Obj init
  368. Player p("gera.png", 205, 1030, 50, 50);
  369. //Variables init
  370. bool showMissionText = true;
  371. float currentFrame = 0;
  372. int Resolution = 0;
  373. //Time Variables
  374. Clock clock;
  375. Clock gameTimeClock;
  376. int gameTime = 0;
  377. int createObjectForMapTimer = 0;
  378. // Creating Graph Map
  379. for (int i = 0; i < HEIGHT_MAP; i++) {
  380. for (int j = 0; j < WIDTH_MAP; j++) {
  381. if (TileMap[i][j] == ' ') {
  382. sizeM++;
  383. massiveTile[i][j] = sizeM;
  384. }
  385. }
  386. }
  387.  
  388. graph GraphMap(sizeM + 1 * 4);
  389.  
  390. for (int i = 0; i < 40; i++) {
  391. for (int j = 0; j < 160; j++) {
  392. if (massiveTile[i][j] < 0) continue;
  393.  
  394. if (massiveTile[i][j + 1] > 0) {
  395. GraphMap.add_edge(massiveTile[i][j], massiveTile[i][j + 1]);
  396. }
  397. if (massiveTile[i + 1][j] > 0) {
  398. GraphMap.add_edge(massiveTile[i][j], massiveTile[i + 1][j]);
  399. }
  400. if (massiveTile[i][j - 1] > 0) {
  401. GraphMap.add_edge(massiveTile[i][j], massiveTile[i][j - 1]);
  402. }
  403. if (massiveTile[i - 1][j] > 0) {
  404. GraphMap.add_edge(massiveTile[i][j], massiveTile[i - 1][j]);
  405. }
  406. }
  407. }
  408.  
  409.  
  410.  
  411. bool life;
  412. int tempX = 0;//TMP for X
  413. int tempY = 0;//TMP for Y
  414. float distance = 0;//Distance from hero to click
  415.  
  416. //Window loop
  417. while (window.isOpen())
  418. {
  419. Vector2i pixelPos = Mouse::getPosition(window);//Cursor cords
  420. Vector2f pos = window.mapPixelToCoords(pixelPos);
  421. //Text init variables
  422. ostringstream playerHealthString;
  423. ostringstream playerManaString;
  424. ostringstream playerStaminaString;
  425. ostringstream task;
  426. ostringstream score;
  427. playerHealthString << (int)p.health;
  428. playerManaString << (int)p.mana;
  429. playerStaminaString << (int)p.stamina;
  430. ////////
  431. Play.setFillColor(Color::Yellow);
  432. Play.setString("Play");
  433. Play.setPosition(view.getCenter().x - 50, view.getCenter().y - 150);
  434. ////////HP
  435. BarsH.setFillColor(Color::Red);
  436. BarsH.setString("HP:" + playerHealthString.str());
  437. BarsH.setPosition(view.getCenter().x - 300, view.getCenter().y - 240);
  438. ///////Mana
  439. BarsM.setFillColor(Color::Blue);
  440. BarsM.setString("Mana:" + playerManaString.str());
  441. BarsM.setPosition(view.getCenter().x - 300, view.getCenter().y - 220);
  442. ///////Stamina
  443. BarsS.setFillColor(Color::Green);
  444. BarsS.setString("Stamina:" + playerStaminaString.str());
  445. BarsS.setPosition(view.getCenter().x - 300, view.getCenter().y - 200);
  446. ///////Time
  447. float time = clock.getElapsedTime().asMicroseconds();
  448. if (p.life) { gameTime = gameTimeClock.getElapsedTime().asSeconds(); }
  449. else { view.move(0.8, 0); }
  450. clock.restart();
  451. time = time / 800;
  452. ///////Event Loop
  453. Event event;
  454. while (window.pollEvent(event))
  455. {
  456. if (event.type == Event::KeyPressed) {
  457. if (event.key.code == Keyboard::Escape) {
  458. window.close();
  459. }
  460. if ((event.key.code == Keyboard::Tab)) {
  461. switch (showMissionText) {
  462. case true: {
  463. task << getTextMission(getCurrentMission(p.getplayercoordinateX(), p.getplayercoordinateY()));
  464. score << p.playerScore;
  465. Quest.setFillColor(Color::Black);
  466. Quest.setString("\n Миссия : " + task.str() + "\n Очки :" + score.str());
  467. showMissionText = false;
  468. break;
  469. }
  470. case false: {
  471. Quest.setString("");
  472. showMissionText = true;
  473. break;
  474. }
  475. }
  476. }
  477. }
  478.  
  479. }
  480. //Rotation Funct
  481. float dX = pos.x - p.PlayerX;
  482. float dY = pos.y - p.PlayerY;
  483. float rotation = (atan2(dY, dX)) * 180 / 3.14159265;
  484. p.PlayerSpr.setRotation(rotation);
  485. ///////////////////////////////////////////Hero Control////////////////////////////////////////////////////////////////////////
  486. if (p.life) { //If alive
  487. if (Keyboard::isKeyPressed(Keyboard::A)) {
  488. p.Direction = 1; p.Speed = 0.1;
  489. }
  490. if (Keyboard::isKeyPressed(Keyboard::D)) {
  491. p.Direction = 0; p.Speed = 0.1;
  492. }
  493. if (Keyboard::isKeyPressed(Keyboard::W)) {
  494. p.Direction = 3; p.Speed = 0.1;
  495. }
  496. if (Keyboard::isKeyPressed(Keyboard::S)) {
  497. p.Direction = 2; p.Speed = 0.1;
  498. }
  499. if (Mouse::isButtonPressed(Mouse::Left)) {
  500. if (p.stamina - 1 < 1) {
  501. }
  502. else {
  503. sound.play();
  504. p.stamina--;
  505. }
  506. }
  507.  
  508.  
  509.  
  510.  
  511. if (Mouse::isButtonPressed(Mouse::Right)) {
  512. if (p.mana - 1 < 1) {
  513. }
  514. else {
  515. sound.play();
  516. p.mana--;
  517. }
  518.  
  519. }
  520. ///////////////////View
  521. getplayercoordinateforview(p.getplayercoordinateX(), p.getplayercoordinateY());
  522. }
  523.  
  524. //////Random Generate
  525. //createObjectForMapTimer += time;//Timer +
  526. //if (createObjectForMapTimer > 3000) {
  527. // randomMapGenerate();
  528. // createObjectForMapTimer = 0;//Timer Null
  529. //}
  530. //Player&Window funct
  531. //cout << searchPos(3157, 2153, GraphMap) << endl;
  532. //cout << getPlayerGraph(p).num << endl;
  533. point needGraph = searchGraphCoords(searchPos(getPlayerGraph(p).num, 2153, GraphMap));
  534.  
  535. cout << "Need: " << needGraph.num << "x: " << needGraph.x << "y: " << needGraph.y << endl;
  536. cout << "Current: " << getPlayerGraph(p).num << endl;
  537. //cout << "Cursor: " << pos.x << " " << pos.y << endl;
  538. //GoToPos(p, needGraph.x, needGraph.y, time);
  539.  
  540.  
  541. p.update(time);
  542. //p.PlayerSpr.setPosition(p.PlayerX, p.PlayerY);
  543.  
  544. window.setView(view);
  545. window.clear();
  546. //Map objects
  547. for (int i = 0; i < HEIGHT_MAP; i++)
  548. for (int j = 0; j < WIDTH_MAP; j++)
  549. {
  550. if (TileMap[i][j] == ' ') s_map.setTextureRect(IntRect(0, 0, 32, 32));
  551. if (TileMap[i][j] == 's') s_map.setTextureRect(IntRect(32, 0, 32, 32));
  552. if ((TileMap[i][j] == 'w')) s_map.setTextureRect(IntRect(64, 0, 32, 32));
  553. if ((TileMap[i][j] == 'f')) s_map.setTextureRect(IntRect(96, 0, 32, 32));
  554. if ((TileMap[i][j] == 'h')) s_map.setTextureRect(IntRect(128, 0, 32, 32));
  555. if ((TileMap[i][j] == 'g'))s_map.setTextureRect(IntRect(192, 0, 32, 32));
  556. if ((TileMap[i][j] == 'o'))s_map.setTextureRect(IntRect(160, 0, 32, 32));
  557. if ((TileMap[i][j] == '0')) s_map.setTextureRect(IntRect(224, 0, 32, 32));
  558. s_map.setPosition(j * 32, i * 32);
  559.  
  560. window.draw(s_map);
  561. }
  562. //Mission Check
  563. if (!showMissionText) {
  564. Quest.setPosition(view.getCenter().x + 120, view.getCenter().y - 130);
  565. s_quest.setPosition(view.getCenter().x + 115, view.getCenter().y - 130);
  566. bg.BGs.setPosition(view.getCenter().x -320, view.getCenter().y - 250);
  567. window.draw(s_quest);
  568. window.draw(Quest);
  569. window.draw(bg.BGs);
  570. window.draw(Play);
  571. }
  572. //Drawing Textures and objects
  573. window.draw(BarsS);
  574. window.draw(BarsH);
  575. window.draw(BarsM);
  576. window.draw(p.PlayerSpr);
  577. window.display();
  578. }
  579. return 0;
  580. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement