Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SFML/Graphics.hpp"
- #include "SFML/Audio.hpp"
- #include <iostream>
- #include <cmath>
- class man {
- public:
- std::string name;
- float health, attackDmg;
- float speed;
- sf::Clock clock;
- sf::Clock attackClock;
- float enemyAttackDmg; //atck speed t.t.
- bool isEnemy = false, isDead = false, didAnimEnd = false, fight = false, isWaiting=false;
- sf::Sprite body;
- float animationSpeed;
- sf::IntRect selection;
- int animationState;
- float animationStart, animationEnd, animationStep;
- sf::RectangleShape HPgreen;
- sf::RectangleShape HPred;
- void setAnimation(int state) {
- switch (state) {
- //Walking
- case 0:
- {
- fight = false;
- isWaiting = false;
- speed = 0.04f;
- animationStart = 0;
- animationEnd = 250;
- animationStep = 50;
- sf::IntRect s2(0, 0, 50, 45);//0,30,10,15
- selection = s2;
- break;
- }
- case 1:
- {
- fight = true;
- animationStart = 0;
- animationEnd = 250;
- animationStep = 50;
- sf::IntRect s2(0, 45, 50, 45);//0,30,10,15
- selection = s2;
- break;
- }
- case 2:
- {
- fight = true;
- isDead = true;
- didAnimEnd = false;
- animationStart = 0;
- animationEnd = 300;
- animationStep = 50;
- sf::IntRect s3(0, 90, 50, 90);
- selection = s3;
- break;
- }
- case 3:
- {
- isWaiting = true;
- animationStart = 0;
- animationEnd = 300;
- animationStep = 50;
- sf::IntRect s4(0, 135, 50, 135);
- selection = s4;
- break;
- }
- }
- }
- void animation() {
- if (clock.getElapsedTime().asSeconds() > animationSpeed) {
- body.setTextureRect(selection);
- if (selection.left >= animationEnd && isDead)
- {
- selection.left = animationEnd;
- didAnimEnd = true;
- }
- if (selection.left >= animationEnd)
- selection.left = animationStart;
- else
- selection.left += animationStep;
- clock.restart();
- }
- }
- void setFightingVariables(float _enemyAD) {
- enemyAttackDmg = _enemyAD;
- }
- void fighting(){
- if (attackClock.getElapsedTime().asSeconds() > 0.5f) {
- health -= enemyAttackDmg;
- if (health <= 0)
- {
- health = -99;
- setAnimation(2);
- }
- attackClock.restart();
- }
- }
- };
- struct laneArmy {
- std::vector<man> troops;
- };
- struct Node {
- float centerX, centerY;
- float scale;
- float x1;
- float x2;
- float y1;
- float y2;
- };
- bool inside(float mousex, float mousey, float shapex, float shapey, float sizex, float sizey) {
- if (mousex> shapex && mousex < shapex + sizex && mousey > shapey && mousey < shapey + sizey)
- return 1;
- else return 0;
- }
- bool insideTwoPoints(float mousex, float mousey, float x1, float y1, float x2, float y2) {
- if (mousey<y2 && mousey>y1 && mousex>x1 && mousex<x2)
- return 1;
- else return 0;
- }
- void createUnit(laneArmy A[], Node B[], int index, sf::Sprite sprite, float animationSpeed, float health, float ad, std::string name, bool isEnemy) {
- man x;
- x.speed = 0.04f;
- x.body.setPosition(B[index].centerX - x.body.getGlobalBounds().width / 2, B[index].centerY - (x.body.getGlobalBounds().height / 2));
- x.body = sprite;
- x.body.setScale(B[index].scale, B[index].scale);
- x.isEnemy = isEnemy;
- x.health = health;
- x.attackDmg = ad;
- x.name = name;
- if (!isEnemy) x.body.setPosition(B[index].centerX - x.body.getGlobalBounds().width / 2, B[index].centerY - (x.body.getGlobalBounds().height / 2));
- else x.body.setPosition(B[index].centerX - x.body.getGlobalBounds().width / 2 + 700, B[index].centerY - (x.body.getGlobalBounds().height / 2));
- x.animationSpeed = animationSpeed;
- x.setAnimation(0);
- A[index].troops.push_back(x);
- }
- bool collision(float x1, float x2, float enemyWidth){
- enemyWidth = 0.3f*enemyWidth;
- if (x1 - enemyWidth + 80.f >= x2 - 10.f && x1 - enemyWidth + 80.f <= x2 + 10.f)
- {
- return true;
- }
- else {
- return false;
- }
- }
- void UpdateAnimation(laneArmy A[], int i, int j, int & mHP, int & eHp){
- A[i].troops[j].animation();
- if (A[i].troops[j].isDead)
- {
- if (A[i].troops[j].didAnimEnd)
- A[i].troops.erase(A[i].troops.begin() + j);
- }
- else if (A[i].troops[j].fight)
- {
- A[i].troops[j].fighting();
- }
- else if(!A[i].troops[j].isWaiting)
- {
- if (A[i].troops[j].isEnemy)
- {
- A[i].troops[j].body.move(-1 * A[i].troops[j].speed, 0);
- if (A[i].troops[j].body.getPosition().x <= 200) {
- mHP -= 2;
- A[i].troops.erase(A[i].troops.begin() + j);
- }
- }
- else {
- A[i].troops[j].body.move(A[i].troops[j].speed, 0);
- if (A[i].troops[j].body.getPosition().x >= 1000){
- eHp -= 2;
- A[i].troops.erase(A[i].troops.begin() + j);
- }
- }
- }
- }
- void settingIfFightingForFirstTime(laneArmy A[], int i, int j){
- if (!A[i].troops[j].fight && !A[i].troops[j].isEnemy && !A[i].troops[j].isDead)
- {
- for (int e = 0; e<A[i].troops.size(); e++)
- {
- if (!A[i].troops[e].fight && A[i].troops[e].isEnemy && !A[i].troops[j].isDead)
- {
- if (collision(A[i].troops[j].body.getPosition().x, A[i].troops[e].body.getPosition().x, A[i].troops[e].body.getGlobalBounds().width))
- {
- A[i].troops[j].setAnimation(1);
- A[i].troops[e].setAnimation(1);
- A[i].troops[j].setFightingVariables(A[i].troops[e].attackDmg);
- A[i].troops[e].setFightingVariables(A[i].troops[j].attackDmg);
- break;
- }
- }
- }
- }
- }
- void chechIfDead(laneArmy A[], int i, int j){
- if (A[i].troops[j].fight)
- {
- for (int e = 0; e<A[i].troops.size(); e++)
- {
- if (A[i].troops[e].fight && e != j)
- {
- if (collision(A[i].troops[j].body.getPosition().x, A[i].troops[e].body.getPosition().x, A[i].troops[e].body.getGlobalBounds().width))
- {
- if (A[i].troops[j].isDead && A[i].troops[e].isDead)
- {
- A[i].troops[j].setAnimation(2);
- A[i].troops[e].setAnimation(2);
- for (int a = 0; a<A[i].troops.size(); a++)
- {
- if(j!=a && e!=a )
- A[i].troops[a].setAnimation(0);
- }
- }
- else {
- if (A[i].troops[j].isDead)
- {
- A[i].troops[e].setAnimation(0);
- A[i].troops[e].enemyAttackDmg = 0;
- for (int a = 0; a<A[i].troops.size(); a++)
- {
- if(j!=a && e!=a)
- A[i].troops[a].setAnimation(0);
- }
- }
- if (A[i].troops[e].isDead)
- {
- A[i].troops[j].setAnimation(0);
- A[i].troops[j].enemyAttackDmg = 0;
- for (int a = 0; a<A[i].troops.size(); a++)
- {
- if(j!=a && e!=a)
- A[i].troops[a].setAnimation(0);
- }
- }
- }
- }
- }
- }
- }
- }
- void troopStoping(laneArmy A[], int i, int j){
- if (!A[i].troops[j].fight && !A[i].troops[j].isDead)
- {
- if(!A[i].troops[j].isEnemy)
- {
- for (int e = 0; e<A[i].troops.size(); e++)
- {
- if (A[i].troops[e].isWaiting || A[i].troops[e].fight && !A[i].troops[e].isEnemy && !A[i].troops[j].isDead)
- {
- if (collision(A[i].troops[j].body.getPosition().x, A[i].troops[e].body.getPosition().x, A[i].troops[e].body.getGlobalBounds().width))
- {
- A[i].troops[j].setAnimation(3);
- break;
- }
- }
- }
- }else{
- for (int e = 0; e<A[i].troops.size(); e++)
- {
- if (A[i].troops[e].isWaiting || A[i].troops[e].fight && A[i].troops[e].isEnemy && !A[i].troops[j].isDead)
- {
- if (collision(A[i].troops[e].body.getPosition().x, A[i].troops[j].body.getPosition().x, A[i].troops[j].body.getGlobalBounds().width))
- {
- A[i].troops[j].setAnimation(3);
- break;
- }
- }
- }
- }
- }
- }
- const int videoRezX = 1280;
- const int videoRezY = 1024;
- int main() {
- float mouseX, mouseY;
- Node N[5];
- laneArmy Lane[5];
- {
- N[0].scale = 3.f; N[1].scale = 2.6f;
- N[0].x1 = 154; N[1].x1 = 200;
- N[0].y1 = 800; N[1].y1 = 700;
- N[0].x2 = 330; N[1].x2 = 350;
- N[0].y2 = 932; N[1].y2 = 800;
- N[0].centerX = (N[0].x2 + N[0].x1) / 2; N[1].centerX = (N[1].x2 + N[1].x1) / 2;
- N[0].centerY = (N[0].y2 + N[0].y1) / 2; N[1].centerY = (N[1].y2 + N[1].y1) / 2;
- N[2].scale = 2.5f; N[3].scale = 2.3f;
- N[2].x1 = 244; N[3].x1 = 275;
- N[2].y1 = 600; N[3].y1 = 520;
- N[2].x2 = 385; N[3].x2 = 400;
- N[2].y2 = 690; N[3].y2 = 600;
- N[2].centerX = (N[2].x2 + N[2].x1) / 2; N[3].centerX = (N[3].x2 + N[3].x1) / 2;
- N[2].centerY = (N[2].y2 + N[2].y1) / 2; N[3].centerY = (N[3].y2 + N[3].y1) / 2;
- N[4].scale = 2.1f;
- N[4].x1 = 300;
- N[4].y1 = 450;
- N[4].x2 = 420;
- N[4].y2 = 518;
- N[4].centerX = (N[4].x2 + N[4].x1) / 2;
- N[4].centerY = (N[4].y2 + N[4].y1) / 2;
- }
- sf::RenderWindow renderWindow(sf::VideoMode(videoRezX, videoRezY), "Ciggy war ", sf::Style::Titlebar | sf::Style::Close | sf::Style::Fullscreen);
- sf::Event event;
- std::vector <sf::Texture> Textures;
- sf::Texture cigarMan, enemyCigarMan, skeletonmage, backg, castl, Grid;
- Grid.loadFromFile("images/grid.png");
- backg.loadFromFile("images/background.png");
- cigarMan.loadFromFile("images/cigarManNew.png");
- enemyCigarMan.loadFromFile("images/enemyCigar.png");
- sf::Sprite background(backg);
- sf::Sprite castle(castl);
- sf::Sprite grid(Grid);
- sf::Music music;
- music.openFromFile("ost.wav");
- music.setVolume(10);
- music.setLoop(true);
- music.play();
- castle.setPosition(0, 0);
- grid.setPosition(0,0);
- int selected; //pasirinks kuri kareivi spawnint
- int myHP=100, enemyHP=100;
- sf::Font font;
- font.loadFromFile("arial.ttf");
- sf::Text myHPstring, enemyHPstring;
- myHPstring.setFont(font);
- enemyHPstring.setFont(font);
- sf::IntRect defaultSelection(0, 0, 50, 45);
- while (renderWindow.isOpen()) {
- myHPstring.setString( std::to_string(myHP) );
- enemyHPstring.setString(std::to_string(enemyHP));
- myHPstring.setPosition(200, 200);
- enemyHPstring.setPosition(1000, 200);
- mouseX = sf::Mouse::getPosition(renderWindow).x;
- mouseY = sf::Mouse::getPosition(renderWindow).y;
- while (renderWindow.pollEvent(event)) {
- switch (event.type) {
- case sf::Event::EventType::Closed:
- renderWindow.close();
- case sf::Event::MouseButtonPressed:
- switch (event.key.code) {
- case sf::Mouse::Left:
- for (int i = 0; i < 5; i++) {
- if (insideTwoPoints(mouseX, mouseY, N[i].x1, N[i].y1, N[i].x2, N[i].y2)){
- sf::Sprite sprite(cigarMan, defaultSelection);
- createUnit(Lane, N, i, sprite, 0.1f, 100.f, 8.f, "Cigar", false);
- }
- }
- break;
- case sf::Mouse::Right:
- for (int i = 0; i < 5; i++) {
- if (insideTwoPoints(mouseX, mouseY, N[i].x1, N[i].y1, N[i].x2, N[i].y2)) {
- sf::Sprite spriteEnemy(enemyCigarMan, defaultSelection);
- createUnit(Lane, N, i, spriteEnemy, 0.1f, 100.f, 6.f, "CigarEnemy", true);
- }
- }
- break;
- }
- }
- }
- for (int i = 0; i<5; i++)
- {
- for (int j = 0; j<Lane[i].troops.size(); j++)
- {
- //Pradeda kovojima
- settingIfFightingForFirstTime(Lane,i,j);
- //Ziuri ar gali praeiti
- troopStoping(Lane,i,j);
- //Pastoviai ziuri ar kovojantis unit nenuzude vienas kito
- chechIfDead(Lane,i,j);
- //Updatina animation, atima hp
- UpdateAnimation(Lane,i,j,myHP,enemyHP);
- }
- }
- //DRAWING
- renderWindow.clear();
- renderWindow.draw(background);
- renderWindow.draw(myHPstring);
- renderWindow.draw(enemyHPstring);
- // renderWindow.draw(button);
- renderWindow.draw(grid);
- for (int i = 4; i >= 0; i--) {
- for (int j = 0; j<Lane[i].troops.size(); j++) {
- renderWindow.draw(Lane[i].troops[j].body);
- //HP DRAW
- if (!Lane[i].troops[j].isDead ){
- 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);
- 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);
- Lane[i].troops[j].HPgreen.setFillColor(sf::Color::Green);
- Lane[i].troops[j].HPgreen.setSize(sf::Vector2f(3, -1 * Lane[i].troops[j].health));
- renderWindow.draw(Lane[i].troops[j].HPgreen);
- }
- }
- }
- renderWindow.draw(castle);
- renderWindow.display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement