Revert "Fixed Minimap Updating"

This commit is contained in:
devZoGok
2026-08-21 06:45:45 +00:00
committed by GitHub
parent e08ac9a58d
commit b5ff653c72
9 changed files with 14 additions and 94 deletions
+6 -36
View File
@@ -42,14 +42,11 @@ namespace battleship{
return minimap;
}
// Minimap constructor
Map::Minimap::Minimap(){
// Gets map size
Vector3 mapSize = Map::getSingleton()->getMapSize();
float ratio = mapSize.x / mapSize.z;
sol::state_view SOL_LUA_VIEW = generateView();
// Set the minimap size
SOL_LUA_VIEW.script_file(GameManager::getSingleton()->getPath() + "Scripts/Gui/activeGameState.lua");
sol::table sizeTbl = SOL_LUA_VIEW["minimapSize"];
float initSizeX = sizeTbl["x"], initSizeY = sizeTbl["y"];
@@ -76,8 +73,7 @@ namespace battleship{
string camIconFile = SOL_LUA_VIEW["eyeIcon"];
camIcon = initIcon(Root::getSingleton()->getCamera()->getPosition(), basePath + camIconFile);
}
// Minimap destructor
Map::Minimap::~Minimap(){
Node *guiNode = Root::getSingleton()->getGuiNode();
@@ -92,13 +88,7 @@ namespace battleship{
delete camIcon;
}
/// @brief Initialize an icon for the minimap by passing its path and poition on the map
/// @param posOnMap
/// @param iconPath
/// @return
Node* Map::Minimap::initIcon(Vector3 posOnMap, string iconPath){
// Get the icon image, its size, and texture
string p[]{iconPath};
ImageAsset *asset = (ImageAsset*)AssetManager::getSingleton()->getAsset(iconPath);
Vector3 iconSize = Vector3(asset->width, asset->height, 0);
@@ -109,7 +99,6 @@ namespace battleship{
mat->addBoolUniform("texturingEnabled", true);
mat->addTexUniform("diffuseMap", tex, false);
// Get the minimap size and position on the screen
sol::state_view SOL_LUA_VIEW = generateView();
sol::table posTbl = SOL_LUA_VIEW["minimapPos"],
_posTbl = SOL_LUA_VIEW["_minimapPos"],
@@ -120,16 +109,14 @@ namespace battleship{
Vector3 minimapPos = Vector3((float)_posTbl["x"], (float)posTbl["y"] + (float)_posTbl["y"], (float)posTbl["z"]);
Vector3 mapSize = Map::getSingleton()->getMapSize();
// Use the minimap size and the map size to place the icon on the minimap in a position corresponding to its position in the real map
Vector2 iconPos = Vector2(
minimapSize.x * (posOnMap.x + .5 * mapSize.x) / mapSize.x,
minimapSize.y * (posOnMap.z + .5 * mapSize.z) / mapSize.z
);
// Create a quad rendering the icon UI element
Quad *quad = new Quad(iconSize, false);
quad->setMaterial(mat);
// Attach the quad to a GUI node of the icon's size and position
Node *node = new Node(minimapPos + Vector3(iconPos.x, iconPos.y, .05) - .5 * iconSize);
node->attachMesh(quad);
root->getGuiNode()->attachChild(node);
@@ -138,29 +125,24 @@ namespace battleship{
}
//TODO add a flag to Player::getUnits* whether to include garrisoned units
/// @brief Updates the minimap according to the real map
void Map::Minimap::updateImage(){
GameManager *gm = GameManager::getSingleton();
Map *map = Map::getSingleton();
// Get the entire minimap image
string imagePath = gm->getPath() + "Models/Maps/" + map->getMapName() + "/minimap.jpg";
ImageAsset *asset = (ImageAsset*)AssetManager::getSingleton()->getAsset(imagePath);
int width = asset->width, height = asset->height;
Vector3 mapSize = map->getMapSize();
// Stores the positions of the friendly units
vector<pair<Unit*, Vector2>> unitMinimapPos;
// Get the current game state
ActiveGameState *activeState = (ActiveGameState*)gm->getStateManager()->getAppStateByType(AppStateType::ACTIVE_STATE);
// Go through all the units on the player's team and get their map position to convert to minimap coordinates to store in the minimap positions
for(Player *pl : Game::getSingleton()->getPlayers(true))
if(pl->getTeam() == activeState->getPlayer()->getTeam()){
vector<Unit*> units = pl->getUnits();
for(Unit *u : units){
// Skip adding all the garrisonable units
if(u->isVehicle() && ((Vehicle*)u)->getGarrisonable()) continue;
Vector2 coords = Vector2(
@@ -174,7 +156,6 @@ namespace battleship{
int pxId = 0, numChannels = asset->numChannels, size = width * height * numChannels;
float losFactor = .6;
// Darkens the entire minimap
for(u8 *p = asset->image; p != asset->image + size; p += numChannels, pxId += numChannels){
*(p + 0) = losFactor * oldImageData[pxId + 0];
*(p + 1) = losFactor * oldImageData[pxId + 1];
@@ -183,13 +164,11 @@ namespace battleship{
int unitPxRadius = 1;
// Go through all the minimap positions and
for(pair<Unit*, Vector2> unitPair : unitMinimapPos){
Unit *losUnit = unitPair.first;
Vector2 losUnitCoords = unitPair.second;
float minimapLos = losUnit->getLineOfSight() / mapSize.x * width;
// Restore brightness based on if the units are in the LOS radius
for(int x = max(-.5f * width, losUnitCoords.x - minimapLos); x < min(.5f * width, losUnitCoords.x + minimapLos); x++){
for(int y = max(-.5f * height, losUnitCoords.y - minimapLos); y < min(.5f * height, losUnitCoords.y + minimapLos); y++){
int pxId = width * (y + .5 * height) + (x + .5 * width);
@@ -199,8 +178,7 @@ namespace battleship{
asset->image[numChannels * pxId + 0] = oldImageData[numChannels * pxId + 0];
asset->image[numChannels * pxId + 1] = oldImageData[numChannels * pxId + 1];
asset->image[numChannels * pxId + 2] = oldImageData[numChannels * pxId + 2];
// Color the areas inside visible regions near the friendly unit minimap positions with the current player's color
// This only shows friendly units in the line of sight, should that be the case?
for(pair<Unit*, Vector2> addUnitPair : unitMinimapPos)
if(fabs(addUnitPair.second.x - coords.x) < unitPxRadius && fabs(addUnitPair.second.y - coords.y) < unitPxRadius){
Vector3 unitCol = addUnitPair.first->getPlayer()->getColor();
@@ -215,15 +193,12 @@ namespace battleship{
}
}
// Load the minimap image data
Node *rectNode = ConcreteGuiManager::getSingleton()->getButton("minimap")->getRectNode();
Material *mat = rectNode->getMesh(0)->getMaterial();
Texture *tex = ((Material::TextureUniform*)mat->getUniform("diffuseMap"))->value;
tex->loadImageData(asset, false);
}
/// @brief Updates the position of the camera positiion icon GUI that goes on the minimap to where the camera is currently in the real map
/// @param minimapButton
void Map::Minimap::updateCamFrame(Button *minimapButton){
Vector3 camPos = Root::getSingleton()->getCamera()->getPosition();
Vector3 mapSize = Map::getSingleton()->getMapSize();
@@ -233,18 +208,14 @@ namespace battleship{
minimapSize.y * (camPos.z + .5 * mapSize.z) / mapSize.z
);
// Overlay the icon on the minimap by using the minimap's position
camIcon->setPosition(minimapButton->getPos() + Vector3(iconPos.x, iconPos.y, .06));
}
/// @brief Updates the minimap unit positions and cam frame icon as long as the application state is active
void Map::Minimap::update(){
Button *mb = ConcreteGuiManager::getSingleton()->getButton("minimap");
if(!(GameManager::getSingleton()->getStateManager()->getAppStateByType(AppStateType::ACTIVE_STATE) && mb)) return;
updateImage();
updateCamFrame(mb);
}
@@ -308,7 +279,6 @@ namespace battleship{
return edges;
}
/// @brief Update the minimap
void Map::update(){
Minimap::getSingleton()->update();
}