diff --git a/Assets/Scripts/Gui/techTree.lua b/Assets/Scripts/Gui/techTree.lua new file mode 100644 index 0000000..05bfe30 --- /dev/null +++ b/Assets/Scripts/Gui/techTree.lua @@ -0,0 +1,109 @@ +res = graphics.resolution +Size = {x = res.x * .5, y = res.y * .5} +techButtonSize = {x = 50, y = 50} +Pos = {x = res.x * .3, y = res.y * .1} +margin = {top = 10, left = 10, right = 10, bottom = 10} +baseTechPath = 'Icons/Technologies/' +heightOffset = techButtonSize.y + 10 + +gui = { + { + guiType = GuiType.GUI_RECTANGLE, + pos = Pos, + zIndex = .9, + size = Size, + color = {x = 0, y = 0, z = 0, w = .6} + }, + { + pos = {x = Pos.x + .5 * (Size.x - techButtonSize.x), y = Pos.y + margin.top}, + size = techButtonSize, + name = "Research", + imagePath = baseTechPath .. 'apt.jpg', + guiType = GuiType.BUTTON, + buttonType = ButtonType.RESEARCH, + techId = TechId.APT, + trigger = 82, + }, + { + pos = {x = Pos.x + margin.left, y = Pos.y + margin.top + heightOffset}, + size = techButtonSize, + name = "Research", + imagePath = baseTechPath .. 'fpt.jpg', + guiType = GuiType.BUTTON, + buttonType = ButtonType.RESEARCH, + techId = TechId.FPT, + trigger = 82, + }, + { + pos = {x = Pos.x + margin.left, y = Pos.y + margin.top + 2 * heightOffset}, + size = techButtonSize, + name = "Research", + imagePath = baseTechPath .. 'fpt.jpg', + guiType = GuiType.BUTTON, + buttonType = ButtonType.RESEARCH, + techId = TechId.FPT, + trigger = 82, + }, + { + pos = {x = Pos.x + .5 * (Size.x - techButtonSize.x), y = Pos.y + margin.top + heightOffset}, + size = techButtonSize, + name = "Research", + imagePath = baseTechPath .. 'ept.jpg', + guiType = GuiType.BUTTON, + buttonType = ButtonType.RESEARCH, + techId = TechId.EFT, + trigger = 82, + }, + { + pos = {x = Pos.x + .5 * (Size.x - 3 * techButtonSize.x), y = Pos.y + margin.top + 2 * heightOffset}, + size = techButtonSize, + name = "Research", + imagePath = baseTechPath .. 'ept.jpg', + guiType = GuiType.BUTTON, + buttonType = ButtonType.RESEARCH, + techId = TechId.EFT, + trigger = 82, + }, + { + pos = {x = Pos.x + .5 * (Size.x + 3 * techButtonSize.x), y = Pos.y + margin.top + 2 * heightOffset}, + size = techButtonSize, + name = "Research", + imagePath = baseTechPath .. 'ept.jpg', + guiType = GuiType.BUTTON, + buttonType = ButtonType.RESEARCH, + techId = TechId.EFT, + trigger = 82, + }, + { + pos = {x = Pos.x + Size.x - (margin.right + techButtonSize.x), y = Pos.y + margin.top + heightOffset}, + size = techButtonSize, + name = "Research", + imagePath = baseTechPath .. 'pct.jpg', + guiType = GuiType.BUTTON, + buttonType = ButtonType.RESEARCH, + techId = TechId.PCT, + trigger = 82, + }, + { + pos = {x = Pos.x + Size.x - (margin.right + 3 * techButtonSize.x), y = Pos.y + margin.top + 2 * heightOffset}, + size = techButtonSize, + name = "Research", + imagePath = baseTechPath .. 'pct.jpg', + guiType = GuiType.BUTTON, + buttonType = ButtonType.RESEARCH, + techId = TechId.PCT, + trigger = 82, + }, + { + pos = {x = Pos.x + Size.x - (margin.right + techButtonSize.x), y = Pos.y + margin.top + 2 * heightOffset}, + size = techButtonSize, + name = "Research", + imagePath = baseTechPath .. 'pct.jpg', + guiType = GuiType.BUTTON, + buttonType = ButtonType.RESEARCH, + techId = TechId.PCT, + trigger = 82, + }, + --[[ + ]]-- +} diff --git a/researchButton.cpp b/researchButton.cpp index c950e9c..2c8fc03 100644 --- a/researchButton.cpp +++ b/researchButton.cpp @@ -1,24 +1,89 @@ #include "researchButton.h" #include "activeGameState.h" #include "researchStruct.h" +#include "game.h" #include #include +#include +#include +#include +#include + +#include + +#include + namespace battleship{ using namespace vb01; using namespace std; ResearchButton::ResearchButton(Vector2 pos, Vector2 size, string name, int trigger, string imagePath, int uid, int tid) : - UnitButton(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", trigger, imagePath, uid), techId(tid){} + UnitButton(pos, size, name, GameManager::getSingleton()->getPath() + "Fonts/batang.ttf", trigger, imagePath, uid), techId(tid){ + Root *root = Root::getSingleton(); + Material *mat = new Material(root->getLibPath() + "gui"); + mat->addVec4Uniform("diffuseColor", Vector4::VEC_IJKL); + + Quad *quad = new Quad(Vector3(size.x, size.y, 1), false); + quad->setMaterial(mat); + + overlay = new Node(Vector3(pos.x, pos.y, -.8)); + overlay->attachMesh(quad); + root->getGuiNode()->attachChild(overlay); + } + + ResearchButton::~ResearchButton(){ + Root::getSingleton()->getGuiNode()->dettachChild(overlay); + delete overlay; + } void ResearchButton::onClick(){ - ActiveGameState *activeState = (ActiveGameState*)(GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::ACTIVE_STATE)); - Player *player = activeState->getPlayer(); - vector selUnits = player->getSelectedUnits(), researchStructs = player->getUnitsById(unitId); + vector labs = getUnits(unitId); - for(Unit *rs : researchStructs) - if(find(selUnits.begin(), selUnits.end(), rs) != selUnits.end()) - ((ResearchStruct*)rs)->appendToQueue(techId); + for(Unit *lab : labs) + ((ResearchStruct*)lab)->appendToQueue(techId); + } + + void ResearchButton::update(){ + Button::update(); + + ActiveGameState *activeState = (ActiveGameState*)(GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::ACTIVE_STATE)); + vector technologies = activeState->getPlayer()->getTechnologies(); + bool hasTech = (find(technologies.begin(), technologies.end(), techId) != technologies.end()); + + float alpha = .6; + string uniform = "diffuseColor"; + + if(!hasTech){ + vector techParents = Game::getSingleton()->getTechnology(techId).parents; + bool hasTechParents = true; + + for(int tp : techParents) + if(find(technologies.begin(), technologies.end(), techId) == technologies.end()){ + hasTechParents = false; + break; + } + + if(hasTechParents){ + overlay->setVisible(false); + + vector labs = getUnits(unitId); + + for(Unit *lab : labs){ + vector researchQueue = ((ResearchStruct*)lab)->getQueue(); + + if(find(researchQueue.begin(), researchQueue.end(), techId) != researchQueue.end()){ + overlay->setVisible(true); + overlay->getMesh(0)->getMaterial()->setVec4Uniform(uniform, Vector4(0, 0, 1, alpha)); + break; + } + } + } + else + overlay->getMesh(0)->getMaterial()->setVec4Uniform(uniform, Vector4(1, 0, 0, alpha)); + } + else + overlay->getMesh(0)->getMaterial()->setVec4Uniform(uniform, Vector4(0, 0, 0, alpha)); } } diff --git a/researchButton.h b/researchButton.h index 337bc2f..2ba1ad3 100644 --- a/researchButton.h +++ b/researchButton.h @@ -7,9 +7,12 @@ namespace battleship{ class ResearchButton : public UnitButton{ public: ResearchButton(vb01::Vector2, vb01::Vector2, std::string, int, std::string, int, int); + ~ResearchButton(); void onClick(); + void update(); private: int techId; + vb01::Node *overlay = nullptr; }; } diff --git a/researchStruct.h b/researchStruct.h index 03ef393..5cdc969 100644 --- a/researchStruct.h +++ b/researchStruct.h @@ -14,6 +14,7 @@ namespace battleship{ ~ResearchStruct(); void update(); inline void appendToQueue(int tid){researchQueue.push_back(tid);} + inline std::vector getQueue(){return researchQueue;} private: vb01::s64 lastUpdateTime = 0; int researchCost, generationRate, generationSpeed, researchStatus = 0; diff --git a/trainButton.cpp b/trainButton.cpp index bab5d5e..b2fb4e6 100644 --- a/trainButton.cpp +++ b/trainButton.cpp @@ -16,12 +16,9 @@ namespace battleship{ slotId(slId) {} void TrainButton::onClick(){ - ActiveGameState *activeState = (ActiveGameState*)(GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::ACTIVE_STATE)); - Player *player = activeState->getPlayer(); - vector selUnits = player->getSelectedUnits(), factories = player->getUnitsById(unitId); + vector labs = getUnits(unitId); - for(Unit *fac : factories) - if(fac->getBuildableUnit(slotId).buildable && find(selUnits.begin(), selUnits.end(), fac) != selUnits.end()) - ((Factory*)fac)->appendToQueue(slotId); + for(Unit *lab : labs) + ((Factory*)lab)->appendToQueue(slotId); } } diff --git a/unitButton.cpp b/unitButton.cpp index bb568a1..fddecb7 100644 --- a/unitButton.cpp +++ b/unitButton.cpp @@ -17,4 +17,19 @@ namespace battleship{ if(activeState) activeState->addButton(this); } + + //TODO optimize this code + vector UnitButton::getUnits(int unitId){ + vector units; + + ActiveGameState *activeState = (ActiveGameState*)(GameManager::getSingleton()->getStateManager()->getAppStateByType((int)AppStateType::ACTIVE_STATE)); + Player *player = activeState->getPlayer(); + vector selUnits = player->getSelectedUnits(), researchStructs = player->getUnitsById(unitId); + + for(Unit *rs : researchStructs) + if(find(selUnits.begin(), selUnits.end(), rs) != selUnits.end()) + units.push_back(rs); + + return units; + } } diff --git a/unitButton.h b/unitButton.h index 3184544..94fa57f 100644 --- a/unitButton.h +++ b/unitButton.h @@ -11,6 +11,8 @@ namespace battleship{ UnitButton(vb01::Vector2, vb01::Vector2, std::string, std::string, int, std::string, int); protected: int unitId; + + std::vector getUnits(int); }; }