From 4dbb1ada11a13568304d3287a337c723b4d6caeb Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 24 Sep 2023 12:12:46 +0300 Subject: [PATCH] added structure build status bar fixed bug of not being able to hotkey-select a buildable structure --- structure.cpp | 15 ++++++++++++ structure.h | 6 +++++ unit.cpp | 63 ++++++++++++++++++++++++++------------------------ unit.h | 7 +++--- unitButton.cpp | 2 +- 5 files changed, 58 insertions(+), 35 deletions(-) diff --git a/structure.cpp b/structure.cpp index 45e632d..9e9724d 100644 --- a/structure.cpp +++ b/structure.cpp @@ -1,5 +1,7 @@ #include "structure.h" +#include + using namespace gameBase; using namespace vb01; using namespace std; @@ -7,5 +9,18 @@ using namespace std; namespace battleship{ Structure::Structure(Player *player, int id, Vector3 pos, Quaternion rot, int buildStatus) : Unit(player, id, pos, rot){ this->buildStatus = buildStatus; + buildStatusBackground = createBar(Unit::lenHpBar, Vector4(0, 0, 0, 1)); + buildStatusForeground = createBar(Unit::lenHpBar, Vector4(0, 0, 1, 1)); + } + + void Structure::update(){ + Unit::update(); + + if(selected && buildStatus < 100) + Unit::displayUnitStats(buildStatusForeground, buildStatusBackground, buildStatus, 100, Vector2(0, -10)); + else{ + buildStatusBackground->setVisible(false); + buildStatusForeground->setVisible(false); + } } } diff --git a/structure.h b/structure.h index a31deb0..412d861 100644 --- a/structure.h +++ b/structure.h @@ -3,14 +3,20 @@ #include "unit.h" +namespace vb01{ + class Node; +} + namespace battleship{ class Structure : public Unit{ public: Structure(Player*, int, vb01::Vector3, vb01::Quaternion, int = 0); inline int getBuildStatus(){return buildStatus;} inline void incrementBuildStatus(){buildStatus++;} + virtual void update(); private: int buildStatus = 0; + vb01::Node *buildStatusBackground, *buildStatusForeground; }; } diff --git a/unit.cpp b/unit.cpp index 03140a3..a8cfaf2 100755 --- a/unit.cpp +++ b/unit.cpp @@ -111,28 +111,26 @@ namespace battleship{ orientUnit(rot); } - void Unit::initUnitStats(){ + Node* Unit::createBar(float initLen, Vector4 color){ Root *root = Root::getSingleton(); + Material *mat = new Material(root->getLibPath() + "gui"); + mat->addBoolUniform("texturingEnabled", false); + mat->addVec4Uniform("diffuseColor", color); - hpBackground = new Quad(Vector3(lenHpBar, 10, 0), false); - string libPath = root->getLibPath(); - Material *hpBackgroundMat = new Material(libPath + "gui"); - hpBackgroundMat->addBoolUniform("texturingEnabled", false); - hpBackgroundMat->addVec4Uniform("diffuseColor", Vector4(0, 0, 0, 1)); - hpBackground->setMaterial(hpBackgroundMat); - hpBackgroundNode = new Node(); - hpBackgroundNode->attachMesh(hpBackground); - Node *guiNode = root->getGuiNode(); - guiNode->attachChild(hpBackgroundNode); + Quad *quad = new Quad(Vector3(initLen, 10, 0), false); + quad->setMaterial(mat); - hpForeground = new Quad(Vector3(lenHpBar, 10, 0), false); - Material *hpForegroundMat = new Material(libPath + "gui"); - hpForegroundMat->addBoolUniform("texturingEnabled", false); - hpForegroundMat->addVec4Uniform("diffuseColor", Vector4(0, 1, 0, 1)); - hpForeground->setMaterial(hpForegroundMat); - hpForegroundNode = new Node(); - hpForegroundNode->attachMesh(hpForeground); - guiNode->attachChild(hpForegroundNode); + Node *node = new Node(); + node->attachMesh(quad); + node->setVisible(false); + root->getGuiNode()->attachChild(node); + + return node; + } + + void Unit::initUnitStats(){ + hpBackgroundNode = createBar(lenHpBar, Vector4(0, 0, 0, 1)); + hpForegroundNode = createBar(lenHpBar, Vector4(0, 1, 0, 1)); } void Unit::update() { @@ -149,11 +147,7 @@ namespace battleship{ executeOrders(); screenPos = spaceToScreen(pos); - hpBackgroundNode->setVisible(selected); - hpForegroundNode->setVisible(selected); - - if (selected) - displayUnitStats(); + displayUnitStats(hpForegroundNode, hpBackgroundNode, health, maxHealth); if (health <= 0) blowUp(); @@ -163,14 +157,23 @@ namespace battleship{ working = false; } - void Unit::displayUnitStats() { - float shiftedX = screenPos.x - 0.5 * lenHpBar; - hpForegroundNode->setPosition(Vector3(shiftedX, screenPos.y, 0)); - hpForeground->setSize(Vector3(health / maxHealth * lenHpBar, 10, 0)); - hpBackgroundNode->setPosition(Vector3(shiftedX, screenPos.y, .1)); + void Unit::displayUnitStats(Node *foreground, Node *background, int currVal, int maxVal, Vector2 offset) { + foreground->setVisible(selected); + background->setVisible(selected); + + if(selected){ + Vector3 offset3d = Vector3(offset.x, offset.y, 0); + float shiftedX = screenPos.x - 0.5 * lenHpBar; + background->setPosition(Vector3(shiftedX, screenPos.y, .1) + offset3d); + + + Quad *fgQuad = (Quad*)foreground->getMesh(0); + fgQuad->setSize(Vector3((float)currVal / maxVal * lenHpBar, 10, 0)); + fgQuad->updateVerts(fgQuad->getMeshBase()); + foreground->setPosition(Vector3(shiftedX, screenPos.y, 0) + offset3d); + } } - //TODO factor out non mutual order methods void Unit::executeOrders() { if (orders.size() > 0) { Order order = orders[0]; diff --git a/unit.h b/unit.h index d4dc7f5..cc6f9ac 100755 --- a/unit.h +++ b/unit.h @@ -88,15 +88,12 @@ namespace battleship{ inline vb01::Vector3 getUpVec() {return upVec;} private: void updateScreenCoordinates(); - void displayUnitStats(); inline bool canDisplayOrderLine(){return vb01::getTime() - orderLineDispTime < orderVecDispLength;} const int orderVecDispLength = 2000; sf::SoundBuffer *selectionSfxBuffer; sf::Sound *selectionSfx = nullptr; - vb01::Quad *hpBackground = nullptr, *hpForeground = nullptr; vb01::Node *hpBackgroundNode = nullptr, *hpForegroundNode = nullptr; - int lenHpBar = 200; protected: Player *player; UnitClass unitClass; @@ -105,13 +102,14 @@ namespace battleship{ std::vector orders; vb01::Vector3 pos = vb01::Vector3(0, 0, 0), upVec = vb01::Vector3(0, 1, 0), dirVec = vb01::Vector3(0, 0, 1), leftVec = vb01::Vector3(1, 0, 0), corners[8]; vb01::Quaternion rot = vb01::Quaternion::QUAT_W; - int health, maxHealth, cost, id, playerId; + int health, maxHealth, cost, id, playerId, lenHpBar = 200; s64 orderLineDispTime = 0; vb01::Model *model; bool selected = false, selectable, debugging = false, working = true; float lineOfSight, range, width, height, length; void removeOrder(int); + void displayUnitStats(vb01::Node*, vb01::Node*, int, int, vb01::Vector2 = vb01::Vector2::VEC_ZERO); virtual void initProperties(); virtual void destroyModel(); virtual void initModel(); @@ -124,6 +122,7 @@ namespace battleship{ virtual void move(Order){} virtual void patrol(Order){} virtual void launch(Order){} + vb01::Node* createBar(float, vb01::Vector4); }; } diff --git a/unitButton.cpp b/unitButton.cpp index c9aaf30..3a0d8ba 100644 --- a/unitButton.cpp +++ b/unitButton.cpp @@ -10,7 +10,7 @@ namespace battleship{ using namespace vb01Gui; using namespace gameBase; - UnitButton::UnitButton(Vector2 pos, Vector2 size, string name, string fontPath, int trigger, string imagePath) : Button(pos, size, name, fontPath, true, trigger, imagePath){ + UnitButton::UnitButton(Vector2 pos, Vector2 size, string name, string fontPath, int trigger, string imagePath) : Button(pos, size, name, fontPath, trigger, true, imagePath){ StateManager *stateManager = GameManager::getSingleton()->getStateManager(); ActiveGameState *activeState = (ActiveGameState*)stateManager->getAppStateByType((int)AppStateType::ACTIVE_STATE);