diff --git a/sem2/KazmenkoMS/DinoFightSFML/.gitignore b/sem2/KazmenkoMS/DinoFightSFML/.gitignore new file mode 100644 index 00000000..36854cc8 --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/.gitignore @@ -0,0 +1 @@ +/.vs diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML.sln b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML.sln new file mode 100644 index 00000000..c4d5229e --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34723.18 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DinoFightSFML", "DinoFightSFML\DinoFightSFML.vcxproj", "{64CB5316-C0A8-4F9F-883C-A410F7B02678}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {64CB5316-C0A8-4F9F-883C-A410F7B02678}.Debug|x64.ActiveCfg = Debug|x64 + {64CB5316-C0A8-4F9F-883C-A410F7B02678}.Debug|x64.Build.0 = Debug|x64 + {64CB5316-C0A8-4F9F-883C-A410F7B02678}.Debug|x86.ActiveCfg = Debug|Win32 + {64CB5316-C0A8-4F9F-883C-A410F7B02678}.Debug|x86.Build.0 = Debug|Win32 + {64CB5316-C0A8-4F9F-883C-A410F7B02678}.Release|x64.ActiveCfg = Release|x64 + {64CB5316-C0A8-4F9F-883C-A410F7B02678}.Release|x64.Build.0 = Release|x64 + {64CB5316-C0A8-4F9F-883C-A410F7B02678}.Release|x86.ActiveCfg = Release|Win32 + {64CB5316-C0A8-4F9F-883C-A410F7B02678}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {33E54961-24CA-4960-839C-1C13D2290D9D} + EndGlobalSection +EndGlobal diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Classes.h b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Classes.h new file mode 100644 index 00000000..824fcedb --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Classes.h @@ -0,0 +1,221 @@ +#pragma once +#include "Resourses.h" + + +// +enum Terrain { Plain, River, Mountain }; + +// +class Dinobase { // +protected: + int STR = 0; // 10 - 30 + int DEX = 0; // 10 - 30 + int INT = 0; // 10 - 30 + int cost = 0; // + int type = 0; + std::string name; // + int TextureOffsetX = 0; +public: + Dinobase() + { + } + ~Dinobase() + { + } + int HP = 100; + sf::Sprite dinoSprite; + virtual int Damage(Terrain terrain) = 0; + int GetCost() { return cost; } + int GetStr() { return STR; } + int GetDex() { return DEX; } + int GetInt() { return INT; } + std::string GetType() { + switch (type) + { + case Plain: return "Plain(STR)"; + case River: return "River(DEX)"; + case Mountain: return "Mountain(INT)"; + default: return "noname"; + } + } + std::string getname() { return name; } + void Animation(std::string animName, int frames) + { + dinoSprite.setTexture(Textures["resources/DinoTextures/" + animName + "_" + name + ".png"]); + TextureOffsetX = (TextureOffsetX + 24) % (24 * frames); + dinoSprite.setTextureRect(sf::IntRect(TextureOffsetX, 0, 24, 24)); + } +}; + +// Plain(STR) +class DinoStr : virtual public Dinobase { + +public: + int Damage(Terrain terrain) { + return terrain == Plain ? + 2 * STR : + terrain == River ? + DEX : + INT; + } +}; + +// River(DEX) +class DinoDex : virtual public Dinobase { +public: + int Damage(Terrain terrain) { + return terrain == River ? + 2 * DEX : + terrain == Plain ? + STR : + INT; + } +}; + +// Mountain(INT) +class DinoInt : virtual public Dinobase { +public: + int Damage(Terrain terrain) { + return terrain == Mountain ? + 2 * INT : + terrain == Plain ? + STR : + DEX; + } +}; + +// 3- , +class Dino : public DinoInt, public DinoStr, public DinoDex +{ +private: + int (Dino::* curdamage)(Terrain terrain); // curdamage + int damage_int(Terrain terrain) { return DinoInt::Damage(terrain); } + int damage_str(Terrain terrain) { return DinoStr::Damage(terrain); } + int damage_dex(Terrain terrain) { return DinoDex::Damage(terrain); } + + +public: + Dino(int str, int dex, int intl, int cost, int type, std::string name) + { + std::cout << "DINO CREATED\n"; + this->STR = str; + this->DEX = dex; + this->INT = intl; + this->cost = cost; + this->type = type; + this->name = name; + dinoSprite.setTexture(Textures["resources/DinoTextures/Idle_" + name + ".png"]); + dinoSprite.setScale(15, 15); + dinoSprite.setTextureRect(sf::IntRect(TextureOffsetX, 0, 24, 24)); + dinoSprite.setOrigin((dinoSprite.getLocalBounds().left + dinoSprite.getLocalBounds().width) / 2.f, (dinoSprite.getLocalBounds().top + dinoSprite.getLocalBounds().height) / 2.f); + dinoSprite.setPosition(width / 2.f, height / 2.f); + switch (type) + { + case Plain: curdamage = &Dino::damage_str; break; // curdamage ( ) + case River: curdamage = &Dino::damage_dex; break; + case Mountain: curdamage = &Dino::damage_int; break; + default: curdamage = &Dino::damage_int; break; + } + } + ~Dino() + { + std::cout << "DINO DELETED\n"; + } + int Damage(Terrain terrain) { // damage + return (this->*curdamage)(terrain); + } +}; + +// +class Button +{ +public: + Button(sf::Sprite sprite, sf::Text text, sf::Vector2f position, sf::Color text_color = sf::Color::Black, int buttonId = 0) + { + this->ButtonId = buttonId; + this->Sprite = sprite; + this->Text = text; + Sprite.setOrigin(Sprite.getLocalBounds().getSize() / 2.f); + Text.setOrigin((Text.getLocalBounds().left + Text.getLocalBounds().width) / 2.f, (Text.getLocalBounds().top + Text.getLocalBounds().height) / 2.f); + Sprite.setPosition(position); + Text.setPosition(Sprite.getPosition() - sf::Vector2f(0, 12)); + Text.setFillColor(text_color); + } + int ButtonId; + sf::Sprite Sprite; + sf::Text Text; +}; + +// +class Player +{ +public: + int currbalance, victnum, losnum; + std::vectordinoset; + +}; + +// +class TextRect +{ +public: + TextRect() + { + } + // + TextRect(float posX, float posY, float width, float height, sf::Color bgColor, sf::Color outlineColor, float outlineThickness, sf::Text text, sf::Vector2f textOffset, sf::Color textColor) + { + this->Rect = sf::RectangleShape(sf::Vector2f(width, height)); + this->Text = text; + CenterOrigin(Rect); + CenterOrigin(Text); + Rect.setFillColor(bgColor); + Text.setFillColor(textColor); + Rect.setPosition(posX, posY); + Text.setPosition(Rect.getPosition() + textOffset); + Rect.setOutlineColor(outlineColor); + Rect.setOutlineThickness(outlineThickness); + } + // ( ) + TextRect(float posX, float posY, float width, float height, sf::Color bgColor, sf::Color outlineColor, float outlineThickness, sf::Text text, sf::Vector2f textOffset, sf::Color textColor, sf::Text valueText, sf::Vector2f valueTextOffSet) + { + this->Rect = sf::RectangleShape(sf::Vector2f(width, height)); + CenterOrigin(Rect); + Rect.setFillColor(bgColor); + Rect.setPosition(posX, posY); + Rect.setOutlineColor(outlineColor); + Rect.setOutlineThickness(outlineThickness); + + this->Text = text; + this->ValueText = valueText; + CenterOrigin(Text); + CenterOrigin(ValueText); + + if (Text.getString() == "") + { + ValueText.setPosition(Rect.getPosition().x, Rect.getPosition().y - 5); + } + else + { + ValueText.setPosition(Rect.getPosition() + valueTextOffSet); + Text.setPosition(Rect.getPosition() + textOffset); + Text.setFillColor(textColor); + } + } + void Draw(sf::RenderWindow& window) // + { + window.draw(Rect); + window.draw(Text); + window.draw(ValueText); + } + void Move(float x, float y) + { + sf::Vector2f vector(x, y); + Rect.move(vector); + Text.move(vector); + ValueText.move(vector); + } + sf::RectangleShape Rect; + sf::Text Text; + sf::Text ValueText; +}; \ No newline at end of file diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DinoFightSFML.vcxproj b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DinoFightSFML.vcxproj new file mode 100644 index 00000000..55f23b1b --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DinoFightSFML.vcxproj @@ -0,0 +1,152 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {64cb5316-c0a8-4f9f-883c-a410f7b02678} + DinoFightSFML + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + $\SFML-2.6.1\include;%(AdditionalIncludeDirectories) + + + Console + true + C:\Users\stron\source\repos\DinoFightSFML\SFML-2.6.1\lib;%(AdditionalLibraryDirectories) + sfml-system.lib;sfml-graphics.lib;sfml-window.lib;sfml-audio.lib;sfml-network.lib;%(AdditionalDependencies) + 2 + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\Users\stron\source\repos\DinoFightSFML\SFML-2.6.1\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + C:\Users\stron\source\repos\DinoFightSFML\SFML-2.6.1\lib;%(AdditionalLibraryDirectories) + sfml-system.lib;sfml-graphics.lib;sfml-window.lib;sfml-audio.lib;sfml-network.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DinoFightSFML.vcxproj.filters b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DinoFightSFML.vcxproj.filters new file mode 100644 index 00000000..956e6d3f --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DinoFightSFML.vcxproj.filters @@ -0,0 +1,48 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + + Файлы заголовков + + + Файлы заголовков + + + \ No newline at end of file diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DinoFightSFML.vcxproj.user b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DinoFightSFML.vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DinoFightSFML.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DisplayDinos.cpp b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DisplayDinos.cpp new file mode 100644 index 00000000..6fddc446 --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/DisplayDinos.cpp @@ -0,0 +1,265 @@ +#include "Functions.h" +// displays dinos from player.dinoset on screen, if isShop true displays balance and buy button +bool DisplayDinos(sf::Color bg_color, std::vector& dinos, bool isShop, bool isGame, std::vector& myTeam) +{ + auto it = dinos.begin(); + //--------------------------RECTANGLES-------------------------- + // p.s. . VS + + // str rect + TextRect* STR = new TextRect + ( + width / 3.f + 20, // position X + height / 8.f + 80, // position Y + 120, 30, // width, height + sf::Color(0, 143, 150, 255), // rect fill color + sf::Color(0, 77, 86, 255), // rect outline color + 5, // rect outline thickness + sf::Text("STR: ", font, 30), // text + sf::Vector2f(-10, -5), // text offset + sf::Color::Red, // text color + sf::Text(std::to_string((*it)->GetStr()), font, 30), // valueText + sf::Vector2f(35, -5) // valueText OffSet + ); + + // int rect + TextRect* INT = new TextRect(width / 3.f + 220, height / 8.f + 80, 120, 30, sf::Color(0, 143, 150, 255), sf::Color(0, 77, 86, 255), 5, sf::Text("INT: ", font, 30), sf::Vector2f(-10, -5), sf::Color::Cyan, sf::Text(std::to_string((*it)->GetInt()), font, 30), sf::Vector2f(35, -5)); + + // dex rect + TextRect* DEX = new TextRect(width / 3.f + 420, height / 8.f + 80, 120, 30, sf::Color(0, 143, 150, 255), sf::Color(0, 77, 86, 255), 5, sf::Text("DEX: ", font, 30), sf::Vector2f(-10, -5), sf::Color::Green, sf::Text(std::to_string((*it)->GetDex()), font, 30), sf::Vector2f(35, -5)); + + // name rect + TextRect* NAME = new TextRect(width / 2.f, height / 8.f - 40, 300, 30, sf::Color(0, 143, 150, 255), sf::Color(0, 77, 86, 255), 5, sf::Text("", font, 30), sf::Vector2f(0, 0), sf::Color::Green, sf::Text((*it)->getname(), font, 30), sf::Vector2f(0, -5)); + + // type rect + TextRect* TYPE = new TextRect(width / 2.f, height / 8.f + 10, 240, 30, sf::Color(0, 143, 150, 255), sf::Color(0, 77, 86, 255), 5, sf::Text("", font, 30), sf::Vector2f(0, 0), sf::Color::Green, sf::Text((*it)->GetType(), font, 30), sf::Vector2f(0, -5)); + + // balance rect + TextRect* BALANCE = new TextRect(150, 250, 240, 30, sf::Color(0, 143, 150, 255), sf::Color(0, 77, 86, 255), 5, sf::Text("BALANCE: ", font, 30), sf::Vector2f(-40, -5), sf::Color::Yellow, sf::Text(std::to_string(player.currbalance), font, 30), sf::Vector2f(65, -5)); + + // cost rect + TextRect* COST = new TextRect(width / 2.f, height / 8.f + 460, 240, 30, sf::Color(0, 143, 150, 255), sf::Color(0, 77, 86, 255), 5, sf::Text("COST: ", font, 30), sf::Vector2f(-52, -5), sf::Color::Yellow, sf::Text(std::to_string((*it)->GetCost()), font, 30), sf::Vector2f(48, -5)); + + if (isShop and !isGame) + { + BALANCE->Rect.setPosition(150, 60); + BALANCE->Text.setPosition(BALANCE->Rect.getPosition() + sf::Vector2f(-40, -5)); + BALANCE->ValueText.setPosition(BALANCE->Rect.getPosition() + sf::Vector2f(52, -5)); + } + + // wins rect + TextRect* WINS = new TextRect(90, 300, 120, 30, sf::Color(0, 143, 150, 255), sf::Color(0, 77, 86, 255), 5, sf::Text("WINS: ", font, 30), sf::Vector2f(-10, -5), sf::Color::Green, sf::Text(std::to_string(player.victnum), font, 30), sf::Vector2f(35, -5)); + + // loses rect + TextRect* LOSES = new TextRect(90, 350, 120, 30, sf::Color(0, 143, 150, 255), sf::Color(0, 77, 86, 255), 5, sf::Text("LOSES: ", font, 30), sf::Vector2f(0, -5), sf::Color::Red, sf::Text(std::to_string(player.losnum), font, 30), sf::Vector2f(50, -5)); + + // stats rect + TextRect* STATS = new TextRect(150, 200, 300, 30, bg_color, sf::Color(0, 77, 86, 255), 0, sf::Text("STATS", font, 30), sf::Vector2f(0, 0), text_color); + + // + TextRect Choice_1(1100, 200, 100, 100, bg_color, sf::Color(0, 77, 86, 255), 5, sf::Text("", font, 30), sf::Vector2f(0, 0), text_color); + TextRect Choice_2 = Choice_1; + Choice_2.Move(0, 150); + TextRect Choice_3 = Choice_2; + Choice_3.Move(0, 150); + + TextRect choices[] = { Choice_1,Choice_2,Choice_3 }; + + //--------------------------BUTTONS-------------------------- + + // arrow buttons + sf::Sprite leftarrow, rightarrow; + leftarrow.setTexture(Textures["resources/ArrowL.png"]); + rightarrow.setTexture(Textures["resources/ArrowR.png"]); + Button leftArrowButton(leftarrow, sf::Text("", font, 25), sf::Vector2f(width / 2.f - 300, height / 2.f + 200), sf::Color::Black, 5); + Button rightArrowButton(rightarrow, sf::Text("", font, 25), sf::Vector2f(width / 2.f + 300, height / 2.f + 200), sf::Color::Black, 6); + + // cross button + sf::Sprite crossSprite; + crossSprite.setTexture(Textures["resources/cross.png"]); + Button cross(crossSprite, sf::Text("", font, 25), sf::Vector2f(width - 50.f, 50.f), sf::Color::White, 7); + + // Buy Button + sf::Sprite BuySprite; + BuySprite.setTexture(Textures["resources/buyButton.png"]); + Button buy(BuySprite, sf::Text("", font, 25), sf::Vector2f(width / 2.f, height / 2.f + 275), sf::Color::White, 7); + + // Pick Button + sf::Sprite PickSprite; + PickSprite.setTexture(Textures["resources/PickButton.png"]); + Button Pick(PickSprite, sf::Text("", font, 25), sf::Vector2f(width / 2.f, height / 2.f + 275), sf::Color::White, 7); + + + + auto deletion = [&]() + { + delete STR; + delete INT; + delete DEX; + delete NAME; + delete TYPE; + delete BALANCE; + delete COST; + delete WINS; + delete LOSES; + delete STATS; + }; + + + + + //--------------------------GAME LOOP-------------------------- + while (window.isOpen()) + { + elapsed = timer.getElapsedTime().asSeconds(); // + + // + STR->ValueText.setString(std::to_string((*it)->GetStr())); + INT->ValueText.setString(std::to_string((*it)->GetInt())); + DEX->ValueText.setString(std::to_string((*it)->GetDex())); + NAME->ValueText.setString((*it)->getname()); + TYPE->ValueText.setString((*it)->GetType()); + BALANCE->ValueText.setString(std::to_string(player.currbalance) + "$"); + if (isShop) + { + COST->ValueText.setString(std::to_string((*it)->GetCost()) + "$"); + } + // + + if (elapsed >= 0.2f) + { + (*it)->Animation("Idle", 3); // + timer.restart(); + } + + sf::Event event; + //---------------------EVENT LOOP--------------------- + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + window.close(); + if (event.type == sf::Event::MouseButtonPressed) + { + sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window)); // + + if (leftArrowButton.Sprite.getGlobalBounds().contains(mousePos)) // left arrow + { + Sounds["click"].play(); + if (it != dinos.begin() and dinos.size() != 0) + { + leftArrowButton.Sprite.setTexture(Textures["resources/PressedArrowL.png"]); + it--; + } + } + + else if (rightArrowButton.Sprite.getGlobalBounds().contains(mousePos)) // right arrow + { + Sounds["click"].play(); + if (it + 1 != dinos.end() and dinos.size() != 0) + { + rightArrowButton.Sprite.setTexture(Textures["resources/PressedArrowR.png"]); + it++; + } + } + + else if (cross.Sprite.getGlobalBounds().contains(mousePos) and !isGame) // cross + { + Sounds["click"].play(); + state = menu; + deletion(); + return false; + } + + else if (buy.Sprite.getGlobalBounds().contains(mousePos) and !isGame) // buy + { + buy.Sprite.setTexture(Textures["resources/buyButtonPressed.png"]); + if (player.currbalance >= (*it)->GetCost()) + { + Sounds["money"].play(); + player.dinoset.push_back(new Dino(*(*it))); // + player.currbalance -= (*it)->GetCost(); + SaveStats(player); + } + } + else if (Pick.Sprite.getGlobalBounds().contains(mousePos) and isGame) // pick + { + Sounds["click"].play(); + Pick.Sprite.setTexture(Textures["resources/PickButtonPressed.png"]); + if (myTeam.size() < 3) + { + myTeam.push_back(new Dino(**it)); + myTeam[myTeam.size() - 1]->dinoSprite.setScale(3.5f, 3.5f); + myTeam[myTeam.size() - 1]->dinoSprite.setPosition(choices[myTeam.size() - 1].Rect.getPosition()); + delete(*it); + dinos.erase(it); + if (it != dinos.begin()) it--; + } + else + { + deletion(); + return true; + } + } + } + if (event.type == sf::Event::MouseButtonReleased) + { + buy.Sprite.setTexture(Textures["resources/buyButton.png"]); + if (myTeam.size() < 3) + Pick.Sprite.setTexture(Textures["resources/PickButton.png"]); + else + Pick.Sprite.setTexture(Textures["resources/StartButton.png"]); + rightArrowButton.Sprite.setTexture(Textures["resources/ArrowR.png"]); + leftArrowButton.Sprite.setTexture(Textures["resources/ArrowL.png"]); + } + if (event.type == sf::Event::KeyPressed and event.key.code == sf::Keyboard::Escape) + { + state = menu; + deletion(); + return false; + } + } + //---------------------RENDER--------------------- + + window.clear(bg_color); + + STR->Draw(window); + INT->Draw(window); + DEX->Draw(window); + NAME->Draw(window); + TYPE->Draw(window); + + if (dinos.size() != 0) window.draw((*it)->dinoSprite); // + if (!isGame) window.draw(cross.Sprite); // + window.draw(rightArrowButton.Sprite); + window.draw(leftArrowButton.Sprite); + + if (isShop) + { + COST->Draw(window); + window.draw(buy.Sprite); + BALANCE->Draw(window); + } + else + { + WINS->Draw(window); + LOSES->Draw(window); + STATS->Draw(window); + BALANCE->Draw(window); + } + if (isGame) + { + Choice_1.Draw(window); + Choice_2.Draw(window); + Choice_3.Draw(window); + window.draw(Pick.Sprite); + for (Dino* dino : myTeam) + { + window.draw(dino->dinoSprite); + } + } + window.display(); + // ----------------------------------------------------- + } + deletion(); + return true; +} diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Functions.h b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Functions.h new file mode 100644 index 00000000..b92d0cbc --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Functions.h @@ -0,0 +1,14 @@ +#pragma once +#include "Resourses.h" +#include "Classes.h" +// +bool DisplayDinos(sf::Color bg_color, std::vector& dinos, bool isShop, bool isGame, std::vector& myTeam = emptyvec); +void Menu(sf::Font& font, sf::Color& bg_color); +void GamePlay(sf::Font& font, sf::Color& bg_color); +void AfterFight(bool isWin); +void LoadStats(Player& player); +void SaveStats(Player& player); +void LoadTextures(std::unordered_map& Textures); +void CreateTexture(std::string name); +void CreateSound(std::string name,bool isLoop,float volume); +void MuteAll(); \ No newline at end of file diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/GamePlay.cpp b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/GamePlay.cpp new file mode 100644 index 00000000..6c4c5493 --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/GamePlay.cpp @@ -0,0 +1,384 @@ +#include "Functions.h" +// gameplay +void GamePlay(sf::Font& font, sf::Color& bg_color) +{ + // - + if (player.dinoset.size() < 3) + { + sf::Sprite crossSprite; + crossSprite.setTexture(Textures["resources/cross.png"]); + Button cross(crossSprite, sf::Text("", font, 25), sf::Vector2f(width - 50.f, 50.f), sf::Color::White, 7); + + sf::Event event; + TextRect Message(width / 2.f, height / 2.f, 500, 100, sf::Color(0, 143, 150, 255), sf::Color::Black, 5, sf::Text("You dont have enough dinos\n\tto start this game!", font, 30), sf::Vector2f(0, 0), sf::Color::White); + while (window.isOpen()) + { + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + { + window.close(); + } + if (event.type == sf::Event::MouseButtonPressed) + { + sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window)); + if (cross.Sprite.getGlobalBounds().contains(mousePos)) // cross + { + state = menu; + return; + } + } + if (event.type == sf::Event::KeyPressed) + if (event.key.code == sf::Keyboard::Escape) + { + state = menu; + return; + } + } + window.clear(bg_color); + window.draw(Message.Rect); + window.draw(Message.Text); + window.draw(cross.Sprite); + window.display(); + } + } + else + { + // + std::vectorenemyteam; + int k = 0; + int prevchoice = -1, choice; + while (k < 3) + { + choice = rand() % dinos.size(); + if (choice != prevchoice) + { + Dino* tmpdino = new Dino(*dinos[choice]); + tmpdino->dinoSprite.setScale(3.5f, 3.5f); + enemyteam.push_back(tmpdino); + prevchoice = choice; + k++; + } + } + + std::vectormyteam; + // ( ) + std::vectordinosetCopy; + for (Dino* dino : player.dinoset) + { + dinosetCopy.push_back(new Dino(*dino)); + } + // + if (!DisplayDinos(bg_color, dinosetCopy, false, true, myteam)) // if esc or cross has been pressed + { + state = menu; + return; + } + // + int loc = rand() % 3; + Terrain location; + std::string location_string; + switch (loc) + { + case 0: + location = Plain; + location_string = "Plain"; + break; + case 1: + location = River; + location_string = "River"; + break; + case 2: + location = Mountain; + location_string = "Mountain"; + break; + default: + location = Mountain; + location_string = "Mountain"; + break; + } + // MY TEAM UI + TextRect myteam_rect1(width / 3.f, 150, 100, 100, bg_color, sf::Color(0, 77, 86, 255), 5, sf::Text("", font, 30), sf::Vector2f(0, 0), text_color); + myteam[0]->dinoSprite.setPosition(myteam_rect1.Rect.getPosition()); + + TextRect myteam_rect2 = myteam_rect1; + myteam_rect2.Move(200, 0); + myteam[1]->dinoSprite.setPosition(myteam_rect2.Rect.getPosition()); + + TextRect myteam_rect3 = myteam_rect2; + myteam_rect3.Move(200, 0); + myteam[2]->dinoSprite.setPosition(myteam_rect3.Rect.getPosition()); + + sf::Text MyTeamName("Player\nDamage:" + std::to_string(myteam[0]->Damage(location)), font, 30); + CenterOrigin(MyTeamName); + MyTeamName.setPosition(myteam_rect1.Rect.getPosition() + sf::Vector2f(-140, 0)); + + // ENEMY TEAM UI + TextRect enemyteam_rect1(width / 3.f, 150, 100, 100, bg_color, sf::Color(0, 77, 86, 255), 5, sf::Text("", font, 30), sf::Vector2f(0, 0), text_color); + enemyteam_rect1.Move(0, 400); + enemyteam[0]->dinoSprite.setPosition(enemyteam_rect1.Rect.getPosition()); + + TextRect enemyteam_rect2 = myteam_rect2; + enemyteam_rect2.Move(0, 400); + enemyteam[1]->dinoSprite.setPosition(enemyteam_rect2.Rect.getPosition()); + + TextRect enemyteam_rect3 = myteam_rect3; + enemyteam_rect3.Move(0, 400); + enemyteam[2]->dinoSprite.setPosition(enemyteam_rect3.Rect.getPosition()); + + sf::Text EnemyTeamName("Enemy\nDamage:" + std::to_string(enemyteam[0]->Damage(location)), font, 30); + CenterOrigin(EnemyTeamName); + EnemyTeamName.setPosition(enemyteam_rect3.Rect.getPosition() + sf::Vector2f(140, 0)); + + // + TextRect locationRect(width / 2.f, 50, 150, 30, bg_color, sf::Color(0, 77, 86, 255), 5, sf::Text(location_string, font, 30), sf::Vector2f(0, -5), sf::Color::Green); + + // + sf::RectangleShape MyHPbar(sf::Vector2f(100, 20)); + MyHPbar.setFillColor(sf::Color::Red); + CenterOrigin(MyHPbar); + + sf::RectangleShape EnemyHPbar(MyHPbar); + sf::RectangleShape BlackRect1(MyHPbar); + BlackRect1.setFillColor(sf::Color::Black); + sf::RectangleShape BlackRect2(BlackRect1); + + // + sf::Text myHPtext("", font, 30); + CenterOrigin(myHPtext); + sf::Text enemyHPtext("", font, 30); + CenterOrigin(enemyHPtext); + + float curtime, period = 0.5f; // + int myID = 0, enemyID = 0; + Dino* attacking; + Dino* defending; + bool move = 1; + + // + sf::Text dmg("", font, 30); + CenterOrigin(dmg); + + sf::Event event; + timer.restart().asSeconds(); + // + auto render = [&]() + { + window.clear(bg_color); + locationRect.Draw(window); + myteam_rect1.Draw(window); + myteam_rect2.Draw(window); + myteam_rect3.Draw(window); + enemyteam_rect1.Draw(window); + enemyteam_rect2.Draw(window); + enemyteam_rect3.Draw(window); + window.draw(MyTeamName); + window.draw(EnemyTeamName); + window.draw(myteam[0]->dinoSprite); + window.draw(myteam[1]->dinoSprite); + window.draw(myteam[2]->dinoSprite); + window.draw(enemyteam[0]->dinoSprite); + window.draw(enemyteam[1]->dinoSprite); + window.draw(enemyteam[2]->dinoSprite); + window.draw(BlackRect1); + window.draw(MyHPbar); + window.draw(BlackRect2); + window.draw(EnemyHPbar); + window.draw(myHPtext); + window.draw(enemyHPtext); + window.draw(dmg); + window.display(); + + }; + Sounds["shop"].stop(); + Sounds["fight"].play(); + //-----------------------------------------FIGHT---------------------------------------- + while (window.isOpen()) + { + curtime = timer.getElapsedTime().asSeconds(); + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + window.close(); + } + + myteam[myID]->dinoSprite.setPosition(width / 3.f, height / 2.f); + MyHPbar.setPosition(myteam[myID]->dinoSprite.getPosition() + sf::Vector2f(-0, -50)); + BlackRect1.setPosition(MyHPbar.getPosition()); + + enemyteam[enemyID]->dinoSprite.setPosition(width / 3.f * 2, height / 2.f); + EnemyHPbar.setPosition(enemyteam[enemyID]->dinoSprite.getPosition() + sf::Vector2f(-0, -50)); + BlackRect2.setPosition(EnemyHPbar.getPosition()); + + myHPtext.setString(std::to_string(myteam[myID]->HP)); + myHPtext.setPosition(MyHPbar.getPosition() + sf::Vector2f(80, -5)); + enemyHPtext.setString(std::to_string(enemyteam[enemyID]->HP)); + enemyHPtext.setPosition(EnemyHPbar.getPosition() + sf::Vector2f(80, -5)); + + if (curtime > period) + { + MyTeamName.setString("Player\nDamage:" + std::to_string(myteam[myID]->Damage(location))); + EnemyTeamName.setString("Enemy\nDamage:" + std::to_string(enemyteam[enemyID]->Damage(location))); + if (!move) + { + attacking = enemyteam[enemyID]; + defending = myteam[myID]; + } + else + { + attacking = myteam[myID]; + defending = enemyteam[enemyID]; + } + // + defending->HP -= attacking->Damage(location); + Sounds["kick"].play(); + move = !move; + + defending->Animation("Idle", 3); // reset + // + if (enemyteam[enemyID]->HP >= 0) EnemyHPbar.setSize(sf::Vector2f(float(enemyteam[enemyID]->HP), float(20))); + else EnemyHPbar.setSize(sf::Vector2f(0, float(20))); + if (myteam[myID]->HP >= 0) MyHPbar.setSize(sf::Vector2f(float(myteam[myID]->HP), float(20))); + else MyHPbar.setSize(sf::Vector2f(0, float(20))); + + // + dmg.setPosition(defending->dinoSprite.getPosition() + sf::Vector2f(float(rand() % 120 - 120), float(-(rand() % 100 - 50)))); + + if (defending->HP <= 0) // + { + for (int i = 0; i < 5; i++) // + { + defending->Animation("Death", 5); + render(); + Sleep(100); + } + } + if (myteam[myID]->HP <= 0) + { + myteam[myID]->dinoSprite.setTexture(emptyTexture); + myID++; + } + if (enemyteam[enemyID]->HP <= 0) + { + enemyteam[enemyID]->dinoSprite.setTexture(emptyTexture); + enemyID++; + } + if (defending->HP <= 0 and myID < 3 and enemyID < 3) continue; + if (myID > 2 or enemyID > 2) // + { + AfterFight(!move); + // + std::cout << "myteam deletion\n"; + for (Dino* dino : myteam) + delete dino; + std::cout << "enemyteam deletion\n"; + for (Dino* dino : enemyteam) + delete dino; + std::cout << "dinosetcopy deletion\n"; + for (Dino* dino : dinosetCopy) + delete dino; + state = menu; + return; + } + for (int i = 0; i < 4; i++) // + { + dmg.setString("-" + std::to_string(attacking->Damage(location))); // + defending->Animation("Hurt", 3); + attacking->Animation("Kick", 3); + render(); + Sleep(50); + } + dmg.setString(""); + defending->Animation("Idle", 3); // reset anim + attacking->Animation("Idle", 3); // reset anim + timer.restart(); + } + //------------------RENDER---------------- + render(); + } + } +} + +void AfterFight(bool isWin) +{ + int reward = (rand() % 6 + 5) * 100; // + if (isWin) + { + MuteAll(); + Sounds["victory"].play(); + player.currbalance += reward; + player.victnum++; + } + else + { + MuteAll(); + Sounds["lose"].play(); + player.losnum++; + } + SaveStats(player); // + Dino* dino = dinos[0]; + dino->dinoSprite.setPosition(width / 2.f, height / 2.f); + float animperiod = 0.2f; + + // cross button + sf::Sprite crossSprite; + crossSprite.setTexture(Textures["resources/cross.png"]); + Button cross(crossSprite, sf::Text("", font, 25), sf::Vector2f(width - 50.f, 50.f), sf::Color::White, 7); + // wintext + sf::Text wintext("YOU WIN!!!", font, 50); + wintext.setFillColor(sf::Color::Green); + CenterOrigin(wintext); + wintext.setPosition(width / 2.f, height / 5.f); + // losetext + sf::Text losetext("YOU LOST!!!", font, 50); + losetext.setFillColor(sf::Color::Red); + CenterOrigin(losetext); + losetext.setPosition(width / 2.f, height / 5.f); + // reward + sf::Text rewardtext("+" + std::to_string(reward) + "$", font, 50); + rewardtext.setFillColor(sf::Color::Yellow); + CenterOrigin(rewardtext); + rewardtext.setPosition(width / 2.f, height / 5.f + 40); + + timer.restart(); + //------------------------------GAME LOOP-------------------------------- + while (window.isOpen()) + { + sf::Event event; + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + { + window.close(); + } + if (event.type == sf::Event::MouseButtonPressed) + { + sf::Vector2f mousePos = window.mapPixelToCoords(sf::Mouse::getPosition(window)); + if (cross.Sprite.getGlobalBounds().contains(mousePos)) + return; + } + if (event.type == sf::Event::KeyPressed and event.key.code == sf::Keyboard::Escape) + return; + } + if (timer.getElapsedTime().asSeconds() >= animperiod) + { + dino->Animation("Idle", 3); + timer.restart(); + } + + + //--------------------RENDER----------------------- + window.clear(bg_color); + window.draw(dino->dinoSprite); + window.draw(cross.Sprite); + if (isWin) + { + window.draw(rewardtext); + window.draw(wintext); + } + else + window.draw(losetext); + window.display(); + //-------------------------------------------------- + } +} \ No newline at end of file diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Initialization.cpp b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Initialization.cpp new file mode 100644 index 00000000..83ec4982 --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Initialization.cpp @@ -0,0 +1,74 @@ +#include "Resourses.h" +#include "Functions.h" +// window +const unsigned int width = 1280, height = 720; +sf::ContextSettings settings; +sf::RenderWindow window(sf::VideoMode(width, height), "Dino Fight", sf::Style::Default); +sf::Color bg_color(0, 121, 128, 255); +sf::Color text_color(66, 66, 66, 255); +sf::Font font; + +float elapsed, elapsed1; +sf::Clock timer, timer1; +State state; + +sf::Texture emptyTexture; +std::unordered_mapTextures; +std::unordered_map < std::string, sf::Sound> Sounds; +std::vectordinos; + +std::vectoremptyvec; // DisplayDinos +Player player; + +// +void CreateTexture(std::string name) +{ + sf::Texture tmptext; + tmptext.loadFromFile(name); + Textures[name] = tmptext; +} +// +void CreateSound(std::string soundname,bool isLoop,float volume) +{ + sf::SoundBuffer* buffer = new sf::SoundBuffer; + buffer->loadFromFile("resources/" + soundname + ".mp3"); + sf::Sound sound(*buffer); + sound.setLoop(isLoop); + sound.setVolume(volume); + Sounds[soundname] = sound; +} +// +void MuteAll() +{ + for (auto& sound : Sounds) + { + sound.second.stop(); + } +} +// texture manager +void LoadTextures(std::unordered_map& Textures) +{ + for (Dino* dino : dinos) + { + CreateTexture("resources/DinoTextures/Idle_" + dino->getname() + ".png"); + CreateTexture("resources/DinoTextures/Run_" + dino->getname() + ".png"); + CreateTexture("resources/DinoTextures/Hurt_" + dino->getname() + ".png"); + CreateTexture("resources/DinoTextures/Kick_" + dino->getname() + ".png"); + CreateTexture("resources/DinoTextures/Death_" + dino->getname() + ".png"); + } + CreateTexture("resources/logo.png"); + CreateTexture("resources/Button.png"); + CreateTexture("resources/PressedButton.png"); + CreateTexture("resources/cross.png"); + CreateTexture("resources/ArrowL.png"); + CreateTexture("resources/ArrowR.png"); + CreateTexture("resources/PressedArrowL.png"); + CreateTexture("resources/PressedArrowR.png"); + CreateTexture("resources/buyButton.png"); + CreateTexture("resources/buyButtonPressed.png"); + CreateTexture("resources/PickButton.png"); + CreateTexture("resources/PickButtonPressed.png"); + CreateTexture("resources/StartButton.png"); +} + +// \ No newline at end of file diff --git a/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Menu.cpp b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Menu.cpp new file mode 100644 index 00000000..3008a715 --- /dev/null +++ b/sem2/KazmenkoMS/DinoFightSFML/DinoFightSFML/Menu.cpp @@ -0,0 +1,121 @@ +#include "Functions.h" +// menu +void Menu(sf::Font& font, sf::Color& bg_color) +{ + // + sf::Sprite dino1, dino2; + + sf::Sprite Logo(Textures["resources/logo.png"]); + CenterOrigin(Logo); + Logo.setPosition(width / 2.f, height / 4.f); + + // + sf::Sprite ButtonSprite; + ButtonSprite.setTexture(Textures["resources/Button.png"]); + // + std::vector