Witam!
Od jakiegoś czasu szukam najlepszego sposobu na przeciwników. Zastanawiam się w jaki sposób można by wczytywać z pliku pozycje x,y i moveSpeed w zależności od ID, tzn.
ID:1
x = 100
y = 200
ms = 1
ID:2
x = 200
y = 300
ms = 1.5
Renderuje się 2 przeciwników z różnymi wartościami.
- Obsługę wczytywania stringów znam.
Próbowałem zrobić na takiej zasadzie:
//FileManager class
void FileManager::LoadFromFile(std::string fileName, std::vector<std::vector<std::string>>&variables, std::vector<std::vector<std::string>>& contents, std::string name, std::string id)
{
fileName = "Files/Config/" + fileName;
std::vector<std::string> tempVariables;
std::vector<std::string> tempContents;
variables.clear();
contents.clear();
std::ifstream openFile(fileName.c_str());
bool idFound = false;
if (openFile.is_open())
{
while (!openFile.eof())
{
std::string line;
std::getline(openFile, line);
if (line.find("ID:" + id) != std::string::npos)
{
idFound = true;
continue;
}
else if(line.find("eID:") != std::string::npos && line.find(id) != std::string::npos)
{
idFound = false;
break;
}
if (idFound)
{
if (line.find(name + ":") != std::string::npos)
{
state = VARIABLES;
line.erase(0, line.find(':') + 1);
tempVariables.clear();
}
else
{
state = CONTENTS;
tempContents.clear();
}
line.erase(std::remove(line.begin(), line.end(), '{'), line.end());
line.erase(std::remove(line.begin(), line.end(), '}'), line.end());
std::stringstream str(line);
while (str)
{
std::getline(str, line, ',');
if (line != "")
{
if (state == VARIABLES)
{
tempVariables.push_back(line);
}
else
{
tempContents.push_back(line);
}
std::cout << line << std::endl;
}
}
if (state == CONTENTS && tempContents.size() > 0)
{
variables.push_back(tempVariables);
contents.push_back(tempContents);
}
}
}
}
openFile.clear();
openFile.close();
}
//Entity class
void Entity::LoadFromFile(std::string fileName, std::string name, std::string id)
{
std::vector<std::vector<std::string>> variables;
std::vector<std::vector<std::string>> contents;
Config::getFM()->LoadFromFile(fileName, variables, contents, name, id);
for (int i = 0; i < variables.size(); i++)
{
for (int j = 0; j < variables[i].size(); j++)
{
std::string var = variables[i][j];
std::string con = contents[i][j];
float conf = atof(contents[i][j].c_str());
float coni = atoi(contents[i][j].c_str());
if (var == "x") posX = conf;
else if (var == "y") posY = conf;
else if (var == "ms") velX = conf;
//Tutaj próbowałem dodawać różne rzeczy, żeby program wykrywał id, ale coś nie wyszyło
}
}
}
// Klasa Przeciwnika - konstruktor
Human::Human(int moveDirection, SDL_Renderer *renderer, std::string id)
{
Entity::LoadFromFile("Human.txt", "Human", id);
...
...
}
I w pliku z którego mamy czytać :
ID:1 // Start id
//name, variables
Human:{x, y, ms}
100, 450, 1 // Contents
eID:1 // stop id
ID:2 // start id
//name, variables
Human:{x, y, ms}
200, 500, 1.5 //Contents
eID:2 // stop id
- Zapis
Jakiś pomysł mam, jednak nie jest to jakiś bardzo super profesjonalny pomysł. Prosiłbym o jakieś przykłady kodów. - Dodawanie
Posiadam manager przeciwników, który korzysta ze smart pointerów, nie wiem czy to też jest dobry pomysł, bo w niektórych przykładach kiedy zmieniłem z normalnych na smart odnotowałem większe użycie procesora