Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.73 KB | None | 0 0
  1. #include "SFML/Graphics.hpp"
  2. #include "SFML/Audio.hpp"
  3. #include <iostream>
  4. #include <cmath>
  5.  
  6. class man {
  7. public:
  8.     std::string name;
  9.     float health, attackDmg;
  10.     float speed;
  11.     sf::Clock clock;
  12.     sf::Clock attackClock;
  13.     float enemyAttackDmg; //atck speed t.t.
  14.     bool isEnemy = false, isDead = false, didAnimEnd = false, fight = false, isWaiting=false;
  15.     sf::Sprite body;
  16.     float animationSpeed;
  17.     sf::IntRect selection;
  18.     int animationState;
  19.     float animationStart, animationEnd, animationStep;
  20.     sf::RectangleShape HPgreen;
  21.     sf::RectangleShape HPred;
  22.  
  23.  
  24.     void setAnimation(int state) {
  25.  
  26.         switch (state) {
  27.  
  28.         //Walking
  29.         case 0:
  30.         {
  31.             fight = false;
  32.             isWaiting = false;
  33.             speed = 0.04f;
  34.             animationStart = 0;
  35.             animationEnd = 250;
  36.             animationStep = 50;
  37.             sf::IntRect s2(0, 0, 50, 45);//0,30,10,15
  38.             selection = s2;
  39.  
  40.             break;
  41.         }
  42.  
  43.         case 1:
  44.         {
  45.             fight = true;
  46.             animationStart = 0;
  47.             animationEnd = 250;
  48.             animationStep = 50;
  49.             sf::IntRect s2(0, 45, 50, 45);//0,30,10,15
  50.             selection = s2;
  51.             break;
  52.         }
  53.  
  54.         case 2:
  55.         {
  56.             fight = true;
  57.             isDead = true;
  58.             didAnimEnd = false;
  59.             animationStart = 0;
  60.             animationEnd = 300;
  61.             animationStep = 50;
  62.             sf::IntRect s3(0, 90, 50, 90);
  63.             selection = s3;
  64.             break;
  65.         }
  66.  
  67.         case 3:
  68.         {
  69.             isWaiting = true;
  70.             animationStart = 0;
  71.             animationEnd = 300;
  72.             animationStep = 50;
  73.             sf::IntRect s4(0, 135, 50, 135);
  74.             selection = s4;
  75.             break;
  76.         }
  77.  
  78.         }
  79.     }
  80.  
  81.     void animation() {
  82.         if (clock.getElapsedTime().asSeconds() > animationSpeed) {
  83.             body.setTextureRect(selection);
  84.  
  85.             if (selection.left >= animationEnd && isDead)
  86.             {
  87.                 selection.left = animationEnd;
  88.                 didAnimEnd = true;
  89.             }
  90.  
  91.  
  92.             if (selection.left >= animationEnd)
  93.                 selection.left = animationStart;
  94.             else
  95.                 selection.left += animationStep;
  96.  
  97.  
  98.  
  99.             clock.restart();
  100.         }
  101.     }
  102.  
  103.     void setFightingVariables(float _enemyAD) {
  104.         enemyAttackDmg = _enemyAD;
  105.     }
  106.  
  107.     void fighting(){
  108.         if (attackClock.getElapsedTime().asSeconds() > 0.5f) {
  109.  
  110.             health -= enemyAttackDmg;
  111.  
  112.             if (health <= 0)
  113.             {
  114.                 health = -99;
  115.                 setAnimation(2);
  116.             }
  117.  
  118.             attackClock.restart();
  119.         }
  120.     }
  121. };
  122.  
  123. struct laneArmy {
  124.     std::vector<man> troops;
  125. };
  126.  
  127. struct Node {
  128.     float centerX, centerY;
  129.  
  130.     float scale;
  131.     float x1;
  132.     float x2;
  133.     float y1;
  134.     float y2;
  135. };
  136.  
  137. bool inside(float mousex, float mousey, float shapex, float shapey, float sizex, float sizey) {
  138.     if (mousex> shapex && mousex < shapex + sizex && mousey > shapey &&   mousey <  shapey + sizey)
  139.         return 1;
  140.     else return 0;
  141. }
  142.  
  143. bool insideTwoPoints(float mousex, float mousey, float x1, float y1, float x2, float y2) {
  144.     if (mousey<y2 && mousey>y1 && mousex>x1 && mousex<x2)
  145.         return 1;
  146.     else return 0;
  147. }
  148.  
  149. void createUnit(laneArmy A[], Node B[], int index, sf::Sprite sprite, float animationSpeed, float health, float ad, std::string name, bool isEnemy) {
  150.  
  151.     man x;
  152.     x.speed = 0.04f;
  153.     x.body.setPosition(B[index].centerX - x.body.getGlobalBounds().width / 2, B[index].centerY - (x.body.getGlobalBounds().height / 2));
  154.     x.body = sprite;
  155.     x.body.setScale(B[index].scale, B[index].scale);
  156.     x.isEnemy = isEnemy;
  157.     x.health = health;
  158.     x.attackDmg = ad;
  159.     x.name = name;
  160.     if (!isEnemy) x.body.setPosition(B[index].centerX - x.body.getGlobalBounds().width / 2, B[index].centerY - (x.body.getGlobalBounds().height / 2));
  161.     else  x.body.setPosition(B[index].centerX - x.body.getGlobalBounds().width / 2 + 700, B[index].centerY - (x.body.getGlobalBounds().height / 2));
  162.     x.animationSpeed = animationSpeed;
  163.     x.setAnimation(0);
  164.     A[index].troops.push_back(x);
  165. }
  166.  
  167. bool collision(float x1, float x2, float enemyWidth){
  168.  
  169.     enemyWidth = 0.3f*enemyWidth;
  170.  
  171.     if (x1 - enemyWidth + 80.f >= x2 - 10.f && x1 - enemyWidth + 80.f <= x2 + 10.f)
  172.     {
  173.         return true;
  174.     }
  175.     else {
  176.         return false;
  177.     }
  178. }
  179.  
  180. void UpdateAnimation(laneArmy A[], int i, int j, int & mHP, int & eHp){
  181.  
  182.     A[i].troops[j].animation();
  183.  
  184.     if (A[i].troops[j].isDead)
  185.     {
  186.         if (A[i].troops[j].didAnimEnd)
  187.             A[i].troops.erase(A[i].troops.begin() + j);
  188.  
  189.     }
  190.     else if (A[i].troops[j].fight)
  191.     {
  192.         A[i].troops[j].fighting();
  193.     }
  194.  
  195.     else if(!A[i].troops[j].isWaiting)
  196.     {
  197.         if (A[i].troops[j].isEnemy)
  198.         {
  199.             A[i].troops[j].body.move(-1 * A[i].troops[j].speed, 0);
  200.             if (A[i].troops[j].body.getPosition().x <= 200) {
  201.                 mHP -= 2;
  202.                 A[i].troops.erase(A[i].troops.begin() + j);
  203.             }
  204.         }
  205.         else {
  206.             A[i].troops[j].body.move(A[i].troops[j].speed, 0);
  207.             if (A[i].troops[j].body.getPosition().x >= 1000){
  208.                 eHp -= 2;
  209.                 A[i].troops.erase(A[i].troops.begin() + j);
  210.             }
  211.         }
  212.     }
  213. }
  214.  
  215. void settingIfFightingForFirstTime(laneArmy A[], int i, int j){
  216.     if (!A[i].troops[j].fight && !A[i].troops[j].isEnemy && !A[i].troops[j].isDead)
  217.     {
  218.         for (int e = 0; e<A[i].troops.size(); e++)
  219.         {
  220.             if (!A[i].troops[e].fight && A[i].troops[e].isEnemy && !A[i].troops[j].isDead)
  221.             {
  222.                 if (collision(A[i].troops[j].body.getPosition().x, A[i].troops[e].body.getPosition().x, A[i].troops[e].body.getGlobalBounds().width))
  223.                 {
  224.                     A[i].troops[j].setAnimation(1);
  225.                     A[i].troops[e].setAnimation(1);
  226.                     A[i].troops[j].setFightingVariables(A[i].troops[e].attackDmg);
  227.                     A[i].troops[e].setFightingVariables(A[i].troops[j].attackDmg);
  228.                     break;
  229.                 }
  230.             }
  231.         }
  232.     }
  233. }
  234.  
  235. void chechIfDead(laneArmy A[], int i, int j){
  236.     if (A[i].troops[j].fight)
  237.     {
  238.         for (int e = 0; e<A[i].troops.size(); e++)
  239.         {
  240.             if (A[i].troops[e].fight && e != j)
  241.             {
  242.                 if (collision(A[i].troops[j].body.getPosition().x, A[i].troops[e].body.getPosition().x, A[i].troops[e].body.getGlobalBounds().width))
  243.                 {
  244.                     if (A[i].troops[j].isDead && A[i].troops[e].isDead)
  245.                     {
  246.                         A[i].troops[j].setAnimation(2);
  247.                         A[i].troops[e].setAnimation(2);
  248.  
  249.                         for (int a = 0; a<A[i].troops.size(); a++)
  250.                         {
  251.                             if(j!=a && e!=a )
  252.                                 A[i].troops[a].setAnimation(0);
  253.                         }
  254.  
  255.                     }
  256.                     else {
  257.                         if (A[i].troops[j].isDead)
  258.                         {
  259.                             A[i].troops[e].setAnimation(0);
  260.                             A[i].troops[e].enemyAttackDmg = 0;
  261.  
  262.                             for (int a = 0; a<A[i].troops.size(); a++)
  263.                             {
  264.                                 if(j!=a && e!=a)
  265.                                 A[i].troops[a].setAnimation(0);
  266.                             }
  267.                         }
  268.                         if (A[i].troops[e].isDead)
  269.                         {
  270.                             A[i].troops[j].setAnimation(0);
  271.                             A[i].troops[j].enemyAttackDmg = 0;
  272.  
  273.                             for (int a = 0; a<A[i].troops.size(); a++)
  274.                             {
  275.                                 if(j!=a && e!=a)
  276.                                 A[i].troops[a].setAnimation(0);
  277.                             }
  278.                         }
  279.                     }
  280.                 }
  281.             }
  282.         }
  283.     }
  284.  
  285. }
  286.  
  287. void troopStoping(laneArmy A[], int i, int j){
  288.  
  289.     if (!A[i].troops[j].fight && !A[i].troops[j].isDead)
  290.     {
  291.         if(!A[i].troops[j].isEnemy)
  292.         {
  293.             for (int e = 0; e<A[i].troops.size(); e++)
  294.             {
  295.                 if (A[i].troops[e].isWaiting || A[i].troops[e].fight && !A[i].troops[e].isEnemy && !A[i].troops[j].isDead)
  296.                 {
  297.                     if (collision(A[i].troops[j].body.getPosition().x, A[i].troops[e].body.getPosition().x, A[i].troops[e].body.getGlobalBounds().width))
  298.                     {
  299.                         A[i].troops[j].setAnimation(3);
  300.                         break;
  301.                     }
  302.                 }
  303.             }
  304.         }else{
  305.             for (int e = 0; e<A[i].troops.size(); e++)
  306.             {
  307.                 if (A[i].troops[e].isWaiting || A[i].troops[e].fight && A[i].troops[e].isEnemy && !A[i].troops[j].isDead)
  308.                 {
  309.                     if (collision(A[i].troops[e].body.getPosition().x, A[i].troops[j].body.getPosition().x, A[i].troops[j].body.getGlobalBounds().width))
  310.                     {
  311.                         A[i].troops[j].setAnimation(3);
  312.                         break;
  313.                     }
  314.                 }
  315.             }
  316.         }
  317.     }
  318. }
  319.  
  320. const int videoRezX = 1280;
  321. const int videoRezY = 1024;
  322.  
  323. int main() {
  324.  
  325.  
  326.     float mouseX, mouseY;
  327.  
  328.     Node N[5];
  329.     laneArmy Lane[5];
  330.     {
  331.         N[0].scale = 3.f;                       N[1].scale = 2.6f;
  332.         N[0].x1 = 154;                          N[1].x1 = 200;
  333.         N[0].y1 = 800;                          N[1].y1 = 700;
  334.         N[0].x2 = 330;                          N[1].x2 = 350;
  335.         N[0].y2 = 932;                          N[1].y2 = 800;
  336.         N[0].centerX = (N[0].x2 + N[0].x1) / 2; N[1].centerX = (N[1].x2 + N[1].x1) / 2;
  337.         N[0].centerY = (N[0].y2 + N[0].y1) / 2; N[1].centerY = (N[1].y2 + N[1].y1) / 2;
  338.  
  339.         N[2].scale = 2.5f;                      N[3].scale = 2.3f;
  340.         N[2].x1 = 244;                          N[3].x1 = 275;
  341.         N[2].y1 = 600;                          N[3].y1 = 520;
  342.         N[2].x2 = 385;                          N[3].x2 = 400;
  343.         N[2].y2 = 690;                          N[3].y2 = 600;
  344.         N[2].centerX = (N[2].x2 + N[2].x1) / 2; N[3].centerX = (N[3].x2 + N[3].x1) / 2;
  345.         N[2].centerY = (N[2].y2 + N[2].y1) / 2; N[3].centerY = (N[3].y2 + N[3].y1) / 2;
  346.  
  347.         N[4].scale = 2.1f;
  348.         N[4].x1 = 300;
  349.         N[4].y1 = 450;
  350.         N[4].x2 = 420;
  351.         N[4].y2 = 518;
  352.         N[4].centerX = (N[4].x2 + N[4].x1) / 2;
  353.         N[4].centerY = (N[4].y2 + N[4].y1) / 2;
  354.     }
  355.  
  356.  
  357.     sf::RenderWindow renderWindow(sf::VideoMode(videoRezX, videoRezY), "Ciggy war ", sf::Style::Titlebar | sf::Style::Close | sf::Style::Fullscreen);
  358.     sf::Event event;
  359.  
  360.     std::vector <sf::Texture> Textures;
  361.     sf::Texture cigarMan, enemyCigarMan, skeletonmage, backg, castl, Grid;
  362.     Grid.loadFromFile("images/grid.png");
  363.     backg.loadFromFile("images/background.png");
  364.     cigarMan.loadFromFile("images/cigarManNew.png");
  365.     enemyCigarMan.loadFromFile("images/enemyCigar.png");
  366.     sf::Sprite background(backg);
  367.     sf::Sprite castle(castl);
  368.     sf::Sprite grid(Grid);
  369.     sf::Music music;
  370.     music.openFromFile("ost.wav");
  371.     music.setVolume(10);
  372.     music.setLoop(true);
  373.     music.play();
  374.     castle.setPosition(0, 0);
  375.  
  376.     grid.setPosition(0,0);
  377.  
  378.     int selected; //pasirinks kuri kareivi spawnint
  379.  
  380.     int myHP=100, enemyHP=100;
  381.     sf::Font font;
  382.     font.loadFromFile("arial.ttf");
  383.  
  384.     sf::Text myHPstring, enemyHPstring;
  385.     myHPstring.setFont(font);
  386.     enemyHPstring.setFont(font);
  387.  
  388.  
  389.  
  390.     sf::IntRect defaultSelection(0, 0, 50, 45);
  391.  
  392.     while (renderWindow.isOpen()) {
  393.  
  394.  
  395.  
  396.         myHPstring.setString( std::to_string(myHP) );
  397.         enemyHPstring.setString(std::to_string(enemyHP));
  398.  
  399.         myHPstring.setPosition(200, 200);
  400.         enemyHPstring.setPosition(1000, 200);
  401.  
  402.  
  403.         mouseX = sf::Mouse::getPosition(renderWindow).x;
  404.         mouseY = sf::Mouse::getPosition(renderWindow).y;
  405.  
  406.         while (renderWindow.pollEvent(event)) {
  407.  
  408.             switch (event.type) {
  409.                 case sf::Event::EventType::Closed:
  410.                     renderWindow.close();
  411.  
  412.  
  413.                 case sf::Event::MouseButtonPressed:
  414.  
  415.                     switch (event.key.code) {
  416.  
  417.                         case sf::Mouse::Left:
  418.  
  419.                             for (int i = 0; i < 5; i++) {
  420.  
  421.                                 if (insideTwoPoints(mouseX, mouseY, N[i].x1, N[i].y1, N[i].x2, N[i].y2)){
  422.                                     sf::Sprite sprite(cigarMan, defaultSelection);
  423.                                     createUnit(Lane, N, i, sprite, 0.1f, 100.f, 8.f, "Cigar", false);
  424.  
  425.                                 }
  426.                             }
  427.  
  428.                             break;
  429.  
  430.                         case sf::Mouse::Right:
  431.  
  432.                             for (int i = 0; i < 5; i++) {
  433.                                 if (insideTwoPoints(mouseX, mouseY, N[i].x1, N[i].y1, N[i].x2, N[i].y2)) {
  434.                                     sf::Sprite spriteEnemy(enemyCigarMan, defaultSelection);
  435.                                     createUnit(Lane, N, i, spriteEnemy, 0.1f, 100.f, 6.f, "CigarEnemy", true);
  436.  
  437.                                 }
  438.                             }
  439.                             break;
  440.                     }
  441.  
  442.  
  443.             }
  444.         }
  445.  
  446.  
  447.         for (int i = 0; i<5; i++)
  448.         {
  449.             for (int j = 0; j<Lane[i].troops.size(); j++)
  450.             {
  451.                 //Pradeda kovojima
  452.                 settingIfFightingForFirstTime(Lane,i,j);
  453.  
  454.                 //Ziuri ar gali praeiti
  455.                 troopStoping(Lane,i,j);
  456.  
  457.                 //Pastoviai ziuri ar kovojantis unit nenuzude vienas kito
  458.                 chechIfDead(Lane,i,j);
  459.  
  460.                 //Updatina animation, atima hp
  461.                 UpdateAnimation(Lane,i,j,myHP,enemyHP);
  462.             }
  463.         }
  464.  
  465.  
  466.  
  467.         //DRAWING
  468.         renderWindow.clear();
  469.         renderWindow.draw(background);
  470.         renderWindow.draw(myHPstring);
  471.         renderWindow.draw(enemyHPstring);
  472.         // renderWindow.draw(button);
  473.         renderWindow.draw(grid);
  474.  
  475.         for (int i = 4; i >= 0; i--) {
  476.             for (int j = 0; j<Lane[i].troops.size(); j++) {
  477.                 renderWindow.draw(Lane[i].troops[j].body);
  478.                 //HP DRAW
  479.                 if (!Lane[i].troops[j].isDead ){
  480.                     if (Lane[i].troops[j].isEnemy) Lane[i].troops[j].HPgreen.setPosition(Lane[i].troops[j].body.getPosition().x + Lane[i].troops[j].body.getGlobalBounds().width - 35, Lane[i].troops[j].body.getPosition().y + Lane[i].troops[j].body.getGlobalBounds().height - 15);
  481.                     if (!Lane[i].troops[j].isEnemy) Lane[i].troops[j].HPgreen.setPosition(Lane[i].troops[j].body.getPosition().x + 25, Lane[i].troops[j].body.getPosition().y + Lane[i].troops[j].body.getGlobalBounds().height - 15);
  482.                     Lane[i].troops[j].HPgreen.setFillColor(sf::Color::Green);
  483.                     Lane[i].troops[j].HPgreen.setSize(sf::Vector2f(3, -1 * Lane[i].troops[j].health));
  484.                     renderWindow.draw(Lane[i].troops[j].HPgreen);
  485.                 }
  486.  
  487.  
  488.             }
  489.         }
  490.  
  491.         renderWindow.draw(castle);
  492.         renderWindow.display();
  493.     }
  494. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement