我制作游戏,所有内容都是从文本文件中获取的。武器,房间,球员等...但我的问题是如何从武器的文本文件中获取单一武器:
ID: 1.
Weapon Name: Katana.
Damage: 20.
Weight: 6.
ID: 2.
Weapon Name: Longsword.
Damage: 17.
Weight: 9.
ID: 3.
Weapon Name: WarAxe.
Damage: 22.
Weight: 20.
ID: 4.
Weapon Name: Staff.
Damage: 9.
Weight: 6.
并将其添加到播放器类构造函数中。 这是我到目前为止的代码:
void Weapons :: getWeapon()
{
string fileName = "Weapons\\Weapons.txt";
ifstream infile(fileName);
string garbage;
int id;
string weapon;
int damage;
int weight;
while(infile>>garbage,infile>>id,infile>>garbage,
infile>>garbage,infile>>garbage,infile>>weapon,
infile>>garbage,infile>>damage,infile>>garbage,
infile>>garbage,infile>>weight,infile>>garbage)
{
/*infile>>garbage;
infile>>id;
infile>>garbage;
infile>>garbage;
infile>>garbage;
infile>>weapon;
infile>>garbage;
infile>>damage;
infile>>garbage;
infile>>garbage;
infile>>weight;
infile>>garbage;*/
cout << "ID: \t\t\t"<< id << "\n";
cout << "Weapon Name: \t\t"<< weapon << "\n";
cout << "Damage: \t\t" << damage <<"\n";
cout << "Weight: \t\t" << weight << "\n";
Weapons w1 (id,weapon,damage,weight);
weaponsList.Append(w1);//Appends to a LinkedList
}
}
Player :: Player(string firstName,string secondName,int level,int experience,int strength,Weapons weapons)
{
this->firstName = firstName;
this->secondName = secondName;
this->level = level;
this->experience = experience;
this->strength = strength;
this->weapons = weapons;
}
Player :: Player(string firstName,string secondName,int level,int experience,int strength,Weapons weapons)
{
this->firstName = firstName;
this->secondName = secondName;
this->level = level;
this->experience = experience;
this->strength = strength;
this->weapons = weapons;
}
就像我如何将玩家对象设置为从文本文件中加载的特定武器然后放入列表中。
提前致谢。