From c323950f07b5214ee737bce906afcfe8034af769 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Wed, 15 May 2024 19:02:18 +0300 Subject: [PATCH] mouse cursor --- Assets/Scripts/Gui/activeGameState.lua | 3 ++ activeGameState.cpp | 67 +++++++++++++++++++++++--- activeGameState.h | 15 +++++- 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/Gui/activeGameState.lua b/Assets/Scripts/Gui/activeGameState.lua index d4c9cf1..a283e27 100644 --- a/Assets/Scripts/Gui/activeGameState.lua +++ b/Assets/Scripts/Gui/activeGameState.lua @@ -3,6 +3,9 @@ Size = {x = 70, y = 70} sz = 210 s = 90 resIconBasePath = 'Icons/Resources/' +pointerTex = 'pointer.jpg' +attackTex = 'attack.jpg' +garrisonTex = 'garrison.jpg' gui = { { diff --git a/activeGameState.cpp b/activeGameState.cpp index 48d87e0..03bd960 100755 --- a/activeGameState.cpp +++ b/activeGameState.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -34,17 +35,14 @@ namespace battleship{ using namespace configData; using namespace gameBase; - ActiveGameState::ActiveGameState(GuiAppState *guiState, int playerId) : AbstractAppState( + ActiveGameState::ActiveGameState(GuiAppState *gs, int plId) : AbstractAppState( AppStateType::ACTIVE_STATE, configData::calcSumBinds(AppStateType::ACTIVE_STATE, true), configData::calcSumBinds(AppStateType::ACTIVE_STATE, false), - GameManager::getSingleton()->getPath() + scripts[(int)ScriptFiles::OPTIONS]){ - this->guiState = guiState; - this->playerId = playerId; - mainPlayer = Game::getSingleton()->getPlayer(playerId); - depth = 1; - + GameManager::getSingleton()->getPath() + scripts[(int)ScriptFiles::OPTIONS]), guiState(gs), playerId(plId), depth(1){ initDragbox(); + + mainPlayer = Game::getSingleton()->getPlayer(playerId); } ActiveGameState::~ActiveGameState() { @@ -67,6 +65,28 @@ namespace battleship{ root->getGuiNode()->attachChild(dragboxNode); } + void ActiveGameState::initCursor(){ + string basePath = GameManager::getSingleton()->getPath() + "Textures/Icons/Cursors/"; + sol::state_view SOL_LUA_VIEW = generateView(); + + string p1[]{basePath + (string)SOL_LUA_VIEW["pointerTex"]}, p2[]{basePath + (string)SOL_LUA_VIEW["attackTex"]}, p3[]{basePath + (string)SOL_LUA_VIEW["garrisonTex"]}; + pointerTex = new Texture(p1, 1, false); + attackTex = new Texture(p2, 1, false); + garrisonTex = new Texture(p3, 1, false); + + Root *root = Root::getSingleton(); + Material *mat = new Material(root->getLibPath() + "gui"); + mat->addBoolUniform("texturingEnabled", true); + mat->addTexUniform("diffuseMap", pointerTex, false); + + Quad *quad = new Quad(Vector3(20, 20, 0), false); + quad->setMaterial(mat); + + cursorNode = new Node(); + cursorNode->attachMesh(quad); + root->getGuiNode()->attachChild(cursorNode); + } + void ActiveGameState::removeDragbox(){ Root::getSingleton()->getRootNode()->dettachChild(dragboxNode); delete dragboxNode; @@ -75,12 +95,43 @@ namespace battleship{ void ActiveGameState::onAttached() { AbstractAppState::onAttached(); ConcreteGuiManager::getSingleton()->readLuaScreenScript("activeGameState.lua"); + + if(!cursorNode) initCursor(); } void ActiveGameState::onDettached() { AbstractAppState::onDettached(); } + void ActiveGameState::updateCursor(){ + Vector2 cursorPos = getCursorPos(); + cursorNode->setPosition(Vector3(cursorPos.x, cursorPos.y, .8)); + + bool canSelect = canSelectHoveredOnGameObj(); + bool ownGameObj = (gameObjHoveredOn && gameObjHoveredOn->getPlayer()->getTeam() == mainPlayer->getTeam()); + + if(controlPressed || (gameObjHoveredOn && !ownGameObj && gameObjHoveredOn->getType() == GameObject::Type::UNIT)) + cursorState = CursorState::ATTACK; + else + cursorState = CursorState::NORMAL; + + Texture *tex = nullptr; + + switch(cursorState){ + case CursorState::ATTACK: + tex = attackTex; + break; + case CursorState::GARRISON: + tex = garrisonTex; + break; + default: + tex = pointerTex; + break; + } + + cursorNode->getMesh(0)->getMaterial()->setTexUniform("diffuseMap", tex, false); + } + void ActiveGameState::update() { ConcreteGuiManager *guiManager = ConcreteGuiManager::getSingleton(); guiManager->getText("depth")->setText(L"Depth: " + to_wstring(depth)); @@ -88,6 +139,8 @@ namespace battleship{ guiManager->getText("wealth")->setText(to_wstring(mainPlayer->getResource(ResourceType::WEALTH))); guiManager->getText("research")->setText(to_wstring(mainPlayer->getResource(ResourceType::RESEARCH))); + updateCursor(); + if(!isSelectionBox && leftMouseClicked && getTime() - lastLeftMouseClicked > 10) isSelectionBox = true; else if (isSelectionBox) diff --git a/activeGameState.h b/activeGameState.h index 6de8d1b..0cf60ef 100755 --- a/activeGameState.h +++ b/activeGameState.h @@ -8,6 +8,7 @@ namespace vb01{ class Node; + class Texture; } namespace vb01Gui{ @@ -31,8 +32,17 @@ namespace battleship{ inline Player* getPlayer(){return mainPlayer;} inline std::vector& getUnitGroup(int i){return unitGroups[i];} private: + enum CursorState{ + NORMAL, + ATTACK, + GARRISON, + SUPPLY + }; + bool selectedUnitsAmongst(std::vector); void updateGameObjHoveredOn(); + void initCursor(); + void updateCursor(); void initDragbox(); void removeDragbox(); void deselectUnits(); @@ -45,8 +55,9 @@ namespace battleship{ void enableUnitState(Unit::State); inline bool canSelectHoveredOnGameObj(){return gameObjHoveredOn && gameObjHoveredOn->isSelectable() && gameObjHoveredOn->getPlayer() == mainPlayer;} - GuiAppState *guiState; + CursorState cursorState = CursorState::NORMAL; Player *mainPlayer; + GuiAppState *guiState; std::string unitGuiScreen = ""; GameObject *gameObjHoveredOn = nullptr; vb01::Node *dragboxNode = nullptr; @@ -59,6 +70,8 @@ namespace battleship{ const int NUM_MAX_ZOOMS = 10; float depth = 1; vb01::s64 lastLeftMouseClicked = 0; + vb01::Node *cursorNode = nullptr; + vb01::Texture *pointerTex = nullptr, *attackTex = nullptr, *garrisonTex = nullptr; }; }