Map.h:
#pragma once
class Map
{
public:
Map(std::string MapFileName);
~Map(void);
const TileType& GetTile(int PosX, int PosY);
const std::vector>& GetWholeMap();
private:
int MapSizeX;
int MapSizeY;
std::vector> MapVector;
std::ifstream MapFile;
};
Map.cpp:
#include "StdAfx.h"
#include "Map.h"
Map::Map(std::string MapFileName)
{
MapFile.open(MapFileName);
if (MapFile.is_open())
{
MapFile >> MapSizeX >> MapSizeY;
MapVector.resize(MapSizeX);
for (int index = 0; index < MapSizeX; ++index)
{
MapVector[index].resize(MapSizeY);
}
int LoadPositionX = 0;
int LoadPositionY = 0;
while (!MapFile.eof())
{
MapFile >> MapVector[LoadPositionX][LoadPositionY];
++LoadPositionX;
if (LoadPositionX >= MapSizeX)
{
LoadPositionX = 0;
++LoadPositionY;
}
}
}
else
{
std::cout << "Could Not Load Map File:" << MapFileName << std::endl;
}
}
Map::~Map(void)
{
}
const TileType& Map::GetTile(int PosX, int PosY)
{
return (TileType)MapVector[PosX][PosY];
}
const std::vector>& Map::GetWholeMap()
{
return MapVector;
}
So essentially, I have my map.txt files structured as such:
8 4
1 1 1 1 1 1 1 1
4 5 2 3 5 5 7 3
8 5 6 1 5 7 2 9
1 1 1 1 1 1 1 1
The top two numbers are the X size of the file (Number of Columns) and the Y size of the file (Number of rows). Then, the numbers under it specify certain block types! Currently you can request a certain tile in my std::vector of tiles, or you can get the whole map! What do you think of this, seasoned developers? Is it smart, or is there anything I'm forgetting / not planning for?