From 8b6a50af1ea70c3482a9baed15bd5565086a5555 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 18 May 2024 20:23:58 +0300 Subject: [PATCH] createable trade offers --- Assets/Scripts/Gui/tradingPlayerGuiTray.lua | 16 ++++---- concreteGuiManager.cpp | 12 +++++- game.cpp | 10 +++++ game.h | 4 ++ offerButton.cpp | 18 ++++++++- playerTradeButton.cpp | 22 +++++++++-- tradeOffer.h | 25 ++++++++++++ tradingScreenButton.cpp | 44 +++++++++++++++++---- tradingScreenButton.h | 7 +++- 9 files changed, 134 insertions(+), 24 deletions(-) create mode 100644 tradeOffer.h diff --git a/Assets/Scripts/Gui/tradingPlayerGuiTray.lua b/Assets/Scripts/Gui/tradingPlayerGuiTray.lua index c7be8ce..3452598 100644 --- a/Assets/Scripts/Gui/tradingPlayerGuiTray.lua +++ b/Assets/Scripts/Gui/tradingPlayerGuiTray.lua @@ -1,7 +1,7 @@ res = graphics.resolution buttonSize = {x = 100, y = 20} -textboxSize = {x = 200, y = 25} +textboxSize = {x = 200, y = 20} height = HubPos.y + HubMargin.top + lineId * 30 warIcon = 'Icons/PlayerStates/war.png' @@ -19,22 +19,19 @@ gui = { fontLastChar = 256, color = {x = 1, y = 1, z = 1, w = 1} }, - --[[ { guiType = GuiType.GUI_RECTANGLE, - pos = {x = Pos.x + margin.left + 20, y = height}, - size = Size, + pos = {x = HubPos.x + HubMargin.left + 20, y = height, z = .11}, + size = {x = 20, y = 20}, color = {x = 0, y = 0, z = 0, w = .6} }, { - pos = {x = Pos.x + margin.left + 40, y = height}, + pos = {x = HubPos.x + HubMargin.left + 40, y = height, z = .11}, size = textboxSize, guiType = GuiType.LISTBOX, listboxType = ListboxType.TRADE_OFFERS, - numMaxDisplay = 1, - lines = {''} + numMaxDisplay = 3 }, - ]]-- { guiType = GuiType.BUTTON, buttonType = ButtonType.TRADING_SCREEN, @@ -42,6 +39,7 @@ gui = { guiScreen = 'tradingScreen.lua', pos = {x = HubPos.x + HubSize.x - buttonSize.x - HubMargin.right, y = height, z = .11}, size = buttonSize, - trigger = 10 + trigger = 10, + dependencies = {{id = 2}} } } diff --git a/concreteGuiManager.cpp b/concreteGuiManager.cpp index 262c459..920bd48 100644 --- a/concreteGuiManager.cpp +++ b/concreteGuiManager.cpp @@ -237,9 +237,13 @@ namespace battleship{ case PLAYER_TRADE: button = new PlayerTradeButton(pos, size, (string)guiTable["guiScreen"], name, (int)guiTable["trigger"], imagePath); break; - case TRADING_SCREEN: - button = new TradingScreenButton(pos, size, (int)SOL_LUA_STATE["playerId"], (string)guiTable["guiScreen"], name, (int)guiTable["trigger"], imagePath); + case TRADING_SCREEN:{ + int lid = guiTable["dependencies"][1]["id"]; + Listbox *listbox = (Listbox*)guiElements[lid].second; + + button = new TradingScreenButton(pos, size, listbox, (int)SOL_LUA_STATE["playerId"], (string)guiTable["guiScreen"], name, (int)guiTable["trigger"], imagePath); break; + } case TRADE_OFFER: button = new OfferButton(pos, size, (int)SOL_LUA_STATE["playerId"], name, (int)guiTable["trigger"], imagePath); break; @@ -380,6 +384,10 @@ namespace battleship{ listbox = new Listbox(pos, size, lines, maxDisplay, fontPath, closable); } break; + case TRADE_OFFERS: + closable = true; + listbox = new Listbox(pos, size, lines, numMaxDisplay, fontPath, closable); + break; } int typeArr[2]{(int)GuiElementType::LISTBOX, (int)listboxType}; diff --git a/game.cpp b/game.cpp index b8b850e..365d4d3 100644 --- a/game.cpp +++ b/game.cpp @@ -298,4 +298,14 @@ namespace battleship{ return false; } + + vector Game::findTradeOffers(Player *pl1, Player *pl2){ + vector offers; + + for(TradeOffer *to : tradeOffers) + if((to->initPlayer == pl1 || to->recPlayer == pl1) && (to->initPlayer == pl2 || to->recPlayer == pl2)) + offers.push_back(to); + + return offers; + } } diff --git a/game.h b/game.h index ae42ab8..52bc729 100644 --- a/game.h +++ b/game.h @@ -4,6 +4,7 @@ #include "fx.h" #include "technology.h" #include "ability.h" +#include "tradeOffer.h" #include @@ -28,6 +29,8 @@ namespace battleship{ void initTechnologies(); float calcAbilFromTech(Ability::Type, std::vector, int, int); bool isUnitUnlocked(std::vector, int); + std::vector findTradeOffers(Player*, Player*); + inline void addTradeOffer(TradeOffer *to){tradeOffers.push_back(to);} inline void addFx(Fx f){fx.push_back(f);} inline void addPlayer(Player *pl){players.push_back(pl);} inline std::vector& getPlayers(){return players;} @@ -46,6 +49,7 @@ namespace battleship{ std::vector technologies; std::vector abilities; std::vector players; + std::vector tradeOffers; }; } diff --git a/offerButton.cpp b/offerButton.cpp index c12a9b3..7fae80f 100644 --- a/offerButton.cpp +++ b/offerButton.cpp @@ -1,8 +1,10 @@ #include "offerButton.h" #include "concreteGuiManager.h" -#include "gameManager.h" #include "activeGameState.h" +#include "gameManager.h" +#include "tradeOffer.h" #include "player.h" +#include "game.h" #include @@ -19,5 +21,19 @@ namespace battleship{ void OfferButton::onClick(){ ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); vector textboxes = guiManager->getTextboxes(); + + int recRef = (textboxes[0]->getText() == L"" ? 0 : stoi(textboxes[0]->getText())); + int initRef = (textboxes[1]->getText() == L"" ? 0 : stoi(textboxes[1]->getText())); + int recWealth = (textboxes[2]->getText() == L"" ? 0 : stoi(textboxes[2]->getText())); + int initWealth = (textboxes[3]->getText() == L"" ? 0 : stoi(textboxes[3]->getText())); + int recRes = (textboxes[4]->getText() == L"" ? 0 : stoi(textboxes[4]->getText())); + int initRes = (textboxes[5]->getText() == L"" ? 0 : stoi(textboxes[5]->getText())); + + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + ActiveGameState *activeState = (ActiveGameState*)stateManager->getAppStateByType((int)AppStateType::ACTIVE_STATE); + Player *mainPlayer = activeState->getPlayer(); + + Game *game = Game::getSingleton(); + game->addTradeOffer(new TradeOffer(mainPlayer, game->getPlayer(playerId), initRef, recRef, initWealth, recWealth, initRes, recRes)); } } diff --git a/playerTradeButton.cpp b/playerTradeButton.cpp index 1caf49d..b04e05a 100644 --- a/playerTradeButton.cpp +++ b/playerTradeButton.cpp @@ -1,6 +1,7 @@ #include "playerTradeButton.h" -#include "activeGameState.h" #include "concreteGuiManager.h" +#include "activeGameState.h" +#include "offerButton.h" #include "game.h" #include @@ -20,8 +21,9 @@ namespace battleship{ StateManager *stateManager = GameManager::getSingleton()->getStateManager(); ActiveGameState *activeState = (ActiveGameState*)stateManager->getAppStateByType((int)AppStateType::ACTIVE_STATE); Player *mainPlayer = activeState->getPlayer(); + Game *game = Game::getSingleton(); - vector players = Game::getSingleton()->getPlayers(); + vector players = game->getPlayers(); ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); sol::state_view SOL_LUA_VIEW = generateView(); @@ -39,8 +41,22 @@ namespace battleship{ SOL_LUA_VIEW.script("playerId = " + to_string(i)); guiManager->readLuaScreenScript("tradingPlayerGuiTray.lua", buttons, listboxes, checkboxes, sliders, textboxes, guiRects, texts); - int numButtons = guiManager->getButtons().size(); + vector guiButtons = guiManager->getButtons(); + + for(int j = guiButtons.size() - 1; j > 0; j--) + if(guiButtons[j]->getName() == "Offer") + ((OfferButton*)guiButtons[j])->setPlayerId(i); + int numListboxes = guiManager->getListboxes().size(); + Listbox *listbox = guiManager->getListboxes()[numListboxes - 1]; + int numOffers = game->findTradeOffers(mainPlayer, players[i]).size(); + + for(int j = 0; j < numOffers; j++) + listbox->addLine(L"TRADE_OFFER_" + to_wstring(j)); + + if(listbox->getNumLines() < listbox->getMaxDisplay()) + listbox->setMaxDisplay(listbox->getNumLines()); + int numTexts = guiManager->getTexts().size(); guiManager->getTexts()[numTexts - 1]->setText(stringToWstring(players[i]->getName())); diff --git a/tradeOffer.h b/tradeOffer.h new file mode 100644 index 0000000..a0797d2 --- /dev/null +++ b/tradeOffer.h @@ -0,0 +1,25 @@ +#ifndef TRADE_OFFER_H +#define TRADE_OFFER_H + +#include "player.h" + +namespace battleship{ + struct TradeOffer{ + Player *initPlayer = nullptr, *recPlayer = nullptr; + int initRefineds = 0, recRefineds = 0, initWealth = 0, recWealth = 0, initResearch = 0, recResearch = 0; + bool initPlAgreed = true, recPlAgreed = false; + + TradeOffer(Player *ip, Player *rp, int ir, int rr, int iw, int rw, int it, int rt) : + initPlayer(ip), + recPlayer(rp), + initRefineds(ir), + recRefineds(rr), + initWealth(iw), + recWealth(rw), + initResearch(it), + recResearch(rr) + {} + }; +} + +#endif diff --git a/tradingScreenButton.cpp b/tradingScreenButton.cpp index b8b9028..94286c2 100644 --- a/tradingScreenButton.cpp +++ b/tradingScreenButton.cpp @@ -1,23 +1,51 @@ #include "tradingScreenButton.h" #include "concreteGuiManager.h" +#include "activeGameState.h" #include "offerButton.h" #include "gameManager.h" +#include "tradeOffer.h" +#include "game.h" + +#include +#include + +#include namespace battleship{ using namespace std; using namespace vb01; using namespace vb01Gui; + using namespace gameBase; - TradingScreenButton::TradingScreenButton(Vector3 pos, Vector2 size, int plId, string gs, string name, int trigger, string imagePath) : ActiveStateButton(pos, size, gs, name, trigger, imagePath), playerId(plId){} + TradingScreenButton::TradingScreenButton(Vector3 pos, Vector2 size, Listbox *lb, int plId, string gs, string name, int trigger, string imagePath) : + ActiveStateButton(pos, size, gs, name, trigger, imagePath), + listbox(lb), + playerId(plId) + {} - //TODO add method to get buttons by name void TradingScreenButton::onClick(){ - buttons.push_back(this); - ActiveStateButton::onClick(); + if(listbox->getNumLines() > 0){ + StateManager *stateManager = GameManager::getSingleton()->getStateManager(); + ActiveGameState *activeState = (ActiveGameState*)stateManager->getAppStateByType((int)AppStateType::ACTIVE_STATE); + Player *mainPlayer = activeState->getPlayer(); + + Game *game = Game::getSingleton(); + TradeOffer *tradeOffer = game->findTradeOffers(mainPlayer, game->getPlayer(playerId))[listbox->getSelectedOption()]; - ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); - int numButtons = guiManager->getButtons().size(); - ((OfferButton*)guiManager->getButtons()[numButtons - 1])->setPlayerId(playerId); - guiManager->removeButton(this); + ActiveStateButton::onClick(); + ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); + vector textboxes = guiManager->getTextboxes(); + + textboxes[0]->setEntry(to_wstring(tradeOffer->recRefineds)); + textboxes[1]->setEntry(to_wstring(tradeOffer->initRefineds)); + textboxes[2]->setEntry(to_wstring(tradeOffer->recWealth)); + textboxes[3]->setEntry(to_wstring(tradeOffer->initWealth)); + textboxes[4]->setEntry(to_wstring(tradeOffer->recResearch)); + textboxes[5]->setEntry(to_wstring(tradeOffer->initResearch)); + } + else + ActiveStateButton::onClick(); + + ConcreteGuiManager::getSingleton()->removeButton(this); } } diff --git a/tradingScreenButton.h b/tradingScreenButton.h index 07763e0..3fa4dcc 100644 --- a/tradingScreenButton.h +++ b/tradingScreenButton.h @@ -3,13 +3,18 @@ #include "activeStateButton.h" +namespace vb01Gui{ + class Listbox; +} + namespace battleship{ class TradingScreenButton : public ActiveStateButton{ public: - TradingScreenButton(vb01::Vector3, vb01::Vector2, int, std::string, std::string, int, std::string); + TradingScreenButton(vb01::Vector3, vb01::Vector2, vb01Gui::Listbox*, int, std::string, std::string, int, std::string); void onClick(); private: int playerId; + vb01Gui::Listbox *listbox; }; }