diff --git a/Assets/Scripts/Gui/activeGameState.lua b/Assets/Scripts/Gui/activeGameState.lua index 3db570f..9e7ce9d 100644 --- a/Assets/Scripts/Gui/activeGameState.lua +++ b/Assets/Scripts/Gui/activeGameState.lua @@ -14,7 +14,7 @@ gui = { guiType = GuiType.BUTTON, buttonType = ButtonType.MINIMAP, name = 'minimap', - pos = {x = res.x - minimapSize.x, y = 0, z = 0}, + pos = {x = res.x - minimapSize.x, y = 0, z = .9}, size = minimapSize, color = {x = .5, y = .5, z = .5, w = 1} }, diff --git a/inGameAppState.cpp b/inGameAppState.cpp index c69a406..d05c086 100755 --- a/inGameAppState.cpp +++ b/inGameAppState.cpp @@ -100,6 +100,7 @@ namespace battleship{ void InGameAppState::update() { Game::getSingleton()->update(); + Map::getSingleton()->update(); } void InGameAppState::onAction(int bind, bool isPressed) { diff --git a/map.cpp b/map.cpp index dd14bb6..bbe16ec 100755 --- a/map.cpp +++ b/map.cpp @@ -6,25 +6,171 @@ #include #include +#include + #include +#include #include "map.h" #include "game.h" #include "player.h" #include "gameObject.h" -#include "gameObjectFactory.h" #include "pathfinder.h" #include "gameManager.h" #include "defConfigs.h" +#include "activeGameState.h" +#include "concreteGuiManager.h" +#include "gameObjectFactory.h" -using namespace std; -using namespace vb01; -using namespace gameBase; namespace battleship{ + using namespace std; + using namespace vb01; + using namespace vb01Gui; + using namespace gameBase; using namespace configData; - Map *map = nullptr; + static Map *map = nullptr; + static Map::Minimap *minimap = nullptr; + + Map::Minimap* Map::Minimap::getSingleton(){ + if(!minimap) minimap = new Map::Minimap(); + + return minimap; + } + + Map::Minimap::Minimap(){ + Root *root = Root::getSingleton(); + + Material *mat = new Material(root->getLibPath() + "gui"); + mat->addBoolUniform("lightingEnabled", false); + mat->addBoolUniform("texturingEnabled", false); + mat->addVec4Uniform("diffuseColor", Vector4(0, 0, 0, 1)); + + Quad *mesh = new Quad(Vector3(50, 50, 0), false); + mesh->setMaterial(mat); + mesh->setWireframe(true); + + camFrame = new Node(); + camFrame->attachMesh(mesh); + root->getGuiNode()->attachChild(camFrame); + } + + void Map::Minimap::updateImage(Button *minimapButton){ + string imagePath = GameManager::getSingleton()->getPath() + "Models/Maps/" + Map::getSingleton()->getMapName() + "/minimap.jpg"; + ImageAsset *asset = (ImageAsset*)AssetManager::getSingleton()->getAsset(imagePath); + int width = asset->width, height = asset->height, numChannels = asset->numChannels; + + Vector3 mapSize = Map::getSingleton()->getMapSize(); + vector> unitMinimapPos; + + for(Player *pl : Game::getSingleton()->getPlayers()) + for(Unit *un : pl->getUnits()){ + Vector2 coords = Vector2( + int(un->getPos().x / mapSize.x * width), + int(un->getPos().z / mapSize.z * height) + ); + + unitMinimapPos.push_back(make_pair(un, coords)); + } + + ActiveGameState *activeState = (ActiveGameState*)GameManager::getSingleton()->getStateManager()->getAppStateByType(AppStateType::ACTIVE_STATE); + Player *mainPlayer = activeState->getPlayer(); + vector units = mainPlayer->getUnits(); + + int size = width * height * numChannels, unitPxRadius = 1; + int pxId = 0, numIter = 0; + + for(u8 *p = asset->image; p != asset->image + size; p += numChannels, pxId += numChannels, numIter++){ + Vector2 coords = Vector2( + -(numIter % asset->width - .5 * asset->width), + numIter / asset->width - .5 * asset->height + ); + float losFactor = .6, pxCol[3]{ + losFactor * oldImageData[pxId + 0], + losFactor * oldImageData[pxId + 1], + losFactor * oldImageData[pxId + 2] + }; + + for(pair unitPair : unitMinimapPos){ + Vector2 coordDiff = unitPair.second - coords; + + if(unitPair.first->getPlayer() == mainPlayer && fabs(coordDiff.x) <= unitPxRadius && fabs(coordDiff.y) <= unitPxRadius){ + Vector3 plCol = mainPlayer->getColor(); + pxCol[0] = plCol.x * 255; + pxCol[1] = plCol.y * 255; + pxCol[2] = plCol.z * 255; + break; + } + + float minimapLos = unitPair.first->getLineOfSight() / mapSize.x * width; + + if(unitPair.first->getPlayer() == mainPlayer && coords.getDistanceFrom(unitPair.second) < minimapLos){ + bool foreignUnit = false; + Player *pl = nullptr; + + for(pair up : unitMinimapPos){ + Vector2 coordDiff = up.second - coords; + + if(up.first->getPlayer() != mainPlayer && fabs(coordDiff.x) <= unitPxRadius && fabs(coordDiff.y) <= unitPxRadius){ + foreignUnit = true; + pl = up.first->getPlayer(); + break; + } + } + + if(foreignUnit){ + Vector3 plCol = pl->getColor(); + pxCol[0] = plCol.x * 255; + pxCol[1] = plCol.y * 255; + pxCol[2] = plCol.z * 255; + } + else{ + pxCol[0] = oldImageData[pxId + 0]; + pxCol[1] = oldImageData[pxId + 1]; + pxCol[2] = oldImageData[pxId + 2]; + } + + break; + } + } + + *p = pxCol[0]; + *(p + 1) = pxCol[1]; + *(p + 2) = pxCol[2]; + } + + Node *rectNode = minimapButton->getRectNode(); + Material *mat = rectNode->getMesh(0)->getMaterial(); + Texture *tex = ((Material::TextureUniform*)mat->getUniform("diffuseMap"))->value; + tex->loadImageData(asset); + } + + void Map::Minimap::updateCamFrame(Button *minimapButton){ + } + + void Map::Minimap::update(){ + Button *mb = ConcreteGuiManager::getSingleton()->getButton("minimap"); + updateImage(mb); + updateCamFrame(mb); + } + + void Map::Minimap::load(){ + AssetManager *am = AssetManager::getSingleton(); + string minimapPath = GameManager::getSingleton()->getPath() + "Models/Maps/" + Map::getSingleton()->getMapName() + "/minimap.jpg"; + + am->load(minimapPath); + ImageAsset *asset = (ImageAsset*)am->getAsset(minimapPath); + int imgSize = asset->width * asset->height * asset->numChannels; + oldImageData = new u8[imgSize]; + + for(int i = 0; i < imgSize; i++) + oldImageData[i] = asset->image[i]; + } + + void Map::Minimap::unload(){ + delete[] oldImageData; + } Map* Map::getSingleton(){ if(!map) @@ -65,6 +211,7 @@ namespace battleship{ } void Map::update(){ + Minimap::getSingleton()->update(); } void Map::loadSkybox(){ @@ -313,19 +460,6 @@ namespace battleship{ } } - void Map::loadMinimap(){ - AssetManager *am = AssetManager::getSingleton(); - string minimapPath = GameManager::getSingleton()->getPath() + "Models/Maps/" + mapName + "/minimap.jpg"; - - am->load(minimapPath); - ImageAsset *asset = (ImageAsset*)am->getAsset(minimapPath); - int imgSize = asset->width * asset->height * asset->numChannels; - oldImageData = new u8[imgSize]; - - for(int i = 0; i < imgSize; i++) - oldImageData[i] = asset->image[i]; - } - void Map::load(string mapName){ this->mapName = mapName; @@ -336,7 +470,7 @@ namespace battleship{ SOL_LUA_STATE.script_file(path + "Models/Maps/" + mapName + "/" + mapName + ".lua"); preprareScene(false); - loadMinimap(); + Minimap::getSingleton()->load(); loadSpawnPoints(); loadSkybox(); loadCells(); @@ -419,7 +553,7 @@ namespace battleship{ } void Map::unload(){ - delete[] oldImageData; + Minimap::getSingleton()->unload(); unloadSkybox(); unloadLights(); diff --git a/map.h b/map.h index c8b2ce8..7bf7329 100755 --- a/map.h +++ b/map.h @@ -14,6 +14,10 @@ namespace vb01{ class Node; } +namespace vb01Gui{ + class Button; +} + namespace battleship{ class Player; class Unit; @@ -41,6 +45,23 @@ namespace battleship{ Cell(vb01::Vector3 p, Type t, std::vector e = std::vector{}, std::vector uc = std::vector{}): pos(p), type(t), edges(e), underWaterCellIds(uc){} }; + class Minimap{ + public: + static Minimap* getSingleton(); + ~Minimap(){} + void update(); + void load(); + void unload(); + inline vb01::u8* getOldMinimapImage(){return oldImageData;} + private: + Minimap(); + void updateCamFrame(vb01Gui::Button*); + void updateImage(vb01Gui::Button*); + + vb01::u8 *oldImageData = nullptr; + vb01::Node *camFrame = nullptr; + }; + static Map* getSingleton(); ~Map(){} static std::vector generateAdjacentNodeEdges(int, int, int, int, int); @@ -63,7 +84,6 @@ namespace battleship{ inline std::vector& getCells(){return cells;} inline vb01::Node* getLight(int i){return lights[i];} inline std::vector getLights(){return lights;} - inline vb01::u8* getOldMinimapImage(){return oldImageData;} inline float getBaseHeight(){return baseHeight;} inline void setBaseHeight(float bh){this->baseHeight = bh;} private: @@ -76,10 +96,8 @@ namespace battleship{ std::vector cells; float baseHeight; std::vector lights; - vb01::u8 *oldImageData = nullptr; Map(){} - void loadMinimap(); void preprareScene(bool); void loadSpawnPoints(); void loadLights(); diff --git a/minimapButton.cpp b/minimapButton.cpp index 7e285b2..e282a24 100644 --- a/minimapButton.cpp +++ b/minimapButton.cpp @@ -18,104 +18,6 @@ namespace battleship{ using namespace vb01; using namespace std; - MinimapButton::MinimapButton(Vector3 pos, Vector2 size, string ip) : Button(pos, size, "minimap", "", -1, true, ip){} - - MinimapButton::~MinimapButton(){} - - void MinimapButton::update(){ - Button::update(); - - Material *mat = rectNode->getMesh(0)->getMaterial(); - Texture *tex = ((Material::TextureUniform*)mat->getUniform("diffuseMap"))->value; - ImageAsset *asset = (ImageAsset*)AssetManager::getSingleton()->getAsset(imagePath); - int width = asset->width, height = asset->height, numChannels = asset->numChannels; - - Map *map = Map::getSingleton(); - Vector3 mapSize = map->getMapSize(); - vector> unitMinimapPos; - - for(Player *pl : Game::getSingleton()->getPlayers()) - for(Unit *un : pl->getUnits()){ - Vector2 coords = Vector2( - int(un->getPos().x / mapSize.x * width), - int(un->getPos().z / mapSize.z * height) - ); - - unitMinimapPos.push_back(make_pair(un, coords)); - } - - ActiveGameState *activeState = (ActiveGameState*)GameManager::getSingleton()->getStateManager()->getAppStateByType(AppStateType::ACTIVE_STATE); - Player *mainPlayer = activeState->getPlayer(); - vector units = mainPlayer->getUnits(); - - int size = width * height * numChannels; - int pxId = 0, numIter = 0; - - u8 *oldImageData = map->getOldMinimapImage(); - int unitPxRadius = 1; - - for(u8 *p = asset->image; p != asset->image + size; p += numChannels, pxId += numChannels, numIter++){ - Vector2 coords = Vector2( - -(numIter % asset->width - .5 * asset->width), - numIter / asset->width - .5 * asset->height - ); - float losFactor = .6, pxCol[3]{ - losFactor * oldImageData[pxId + 0], - losFactor * oldImageData[pxId + 1], - losFactor * oldImageData[pxId + 2] - }; - - for(pair unitPair : unitMinimapPos){ - Vector2 coordDiff = unitPair.second - coords; - - if(unitPair.first->getPlayer() == mainPlayer && fabs(coordDiff.x) <= unitPxRadius && fabs(coordDiff.y) <= unitPxRadius){ - Vector3 plCol = mainPlayer->getColor(); - pxCol[0] = plCol.x * 255; - pxCol[1] = plCol.y * 255; - pxCol[2] = plCol.z * 255; - break; - } - - float minimapLos = unitPair.first->getLineOfSight() / mapSize.x * width; - - if(unitPair.first->getPlayer() == mainPlayer && coords.getDistanceFrom(unitPair.second) < minimapLos){ - bool foreignUnit = false; - Player *pl = nullptr; - - for(pair up : unitMinimapPos){ - Vector2 coordDiff = up.second - coords; - - if(up.first->getPlayer() != mainPlayer && fabs(coordDiff.x) <= unitPxRadius && fabs(coordDiff.y) <= unitPxRadius){ - foreignUnit = true; - pl = up.first->getPlayer(); - break; - } - } - - if(foreignUnit){ - Vector3 plCol = pl->getColor(); - pxCol[0] = plCol.x * 255; - pxCol[1] = plCol.y * 255; - pxCol[2] = plCol.z * 255; - } - else{ - pxCol[0] = oldImageData[pxId + 0]; - pxCol[1] = oldImageData[pxId + 1]; - pxCol[2] = oldImageData[pxId + 2]; - } - - break; - } - } - - *p = pxCol[0]; - *(p + 1) = pxCol[1]; - *(p + 2) = pxCol[2]; - } - - tex->loadImageData(asset); - } - void MinimapButton::onClick(){ Vector2 clickPos = getCursorPos(); float posXRatio = 1. - (clickPos.x - pos.x) / size.x; diff --git a/minimapButton.h b/minimapButton.h index e9eafee..92f0802 100644 --- a/minimapButton.h +++ b/minimapButton.h @@ -10,10 +10,9 @@ namespace vb01{ namespace battleship{ class MinimapButton : public vb01Gui::Button{ public: - MinimapButton(vb01::Vector3, vb01::Vector2, std::string); - ~MinimapButton(); + MinimapButton(vb01::Vector3 pos, vb01::Vector2 size, std::string ip) : vb01Gui::Button(pos, size, "minimap", "", -1, true, ip){} + ~MinimapButton(){} void onClick(); - void update(); private: }; }