2015-06-27 47 views
1

我正在構建文本-RPG庫存系統,但我不確定如何正確創建equipable項目。 例如,我可以裝備在玩家庫存中的物品,但我不能識別什麼樣的物品(劍,盾,手套或其他物品),因爲物品應該裝在適當的地方(頭盔,劍在手等)。 有沒有辦法做到這一點?C++文本RPG庫存系統

#include <iostream> 
#include <vector> 
#include <Windows.h> 
#include <string> 

using namespace std; 

void Red() 
{ 
    SetConsoleTextAttribute 
    (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY); 
} //Intensive red console text color. 

void Green() 
{ 
    SetConsoleTextAttribute 
     (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY); 
} //Intensive green console text color. 

void Normal() 
{ 
    SetConsoleTextAttribute 
     (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE); 
} //Default console text color. 

struct Item{ 
    string name; //Item name. 
    int price; //Item price. 
    int purpose; // 0 - Head, 1 - Neck, 2 - Torso, 3 - Hands, 4 - Legs, 5 - Feets. 
    int attribute; //Attack, Defense... 
}; 

int main() 
{ 
    //Isn't the smartest way to do so... 
    Item Sword{ 
     "Sword", //This item name is 'Short Sword'. 
     87, //Cost 87 gold pieces. 
     3, //Use this item with hands. :D. 
     10 //+10 attack. 
    }; 

    string input; // input is for player commands. 
    vector<string> Equipment = { "<Empty>", "<Empty>", "<Empty>", "<Empty>", "<Empty>","<Empty>" }; //Current equipment. 
    vector<string> Inventory = {Sword.name}; //Player Inventory. 
    string InventorySlots[] = { "Head", "Neck", "Torso", "Hands", "Legs", "Feets" }; //Player parts where items can be equiped. 

    while (true){ 
     cin >> input; 
     if (input == "equipment"){ 
      for (int i = 0; i < 6; i++){ 
       Normal(); 
       cout << InventorySlots[i]; 
       if (Equipment[i] == "<Empty>") 
        Red(); 
       cout << " " << Equipment[i] << endl << endl; 
      } 
      Normal(); 
     } 

     if (input == "equip"){ 
      cout << "What do you want to equip? "; 
      cin >> input; 
      for (int i = 0; i < Inventory.size(); i++){ 
       //Search for item player want to equip and equip it in the right place. 
       if (input == Inventory[i]){ 
        //Inventory[i] = input; 
        //But how to identify what kind of item it is? 
        cout << "Successfully equiped!" << endl; 
       } 
      } 
     } 

     if(input == "inventory"){ 
      for (int i = 0; i < Inventory.size(); i++){ 
       cout << "______________________________________________________________" << endl; 
       cout << "| " << Inventory[i] << endl; 
       cout << "| Carried items " << Inventory.size() << "/" << 20 << endl; 
       cout << "|_____________________________________________________________" << endl; 
      } 
     } 

    } 
    system("PAUSE"); // or 'cin.get()' 
    return 0; 
} 
+1

信息添加到您的項目結構... – oobivat

回答

2

有很多可能性來做到這一點。

簡單的方法

首先,你必須保持不串物品的清單,:

vector<Item> Inventory = {Sword, Helmet}; //Player Inventory. 

然後,你必須使用Inventroy[i].name到seacrh和使用inventory[i].purpose找到合適的插槽:

 for(int i = 0; i < Inventory.size(); i++){ 
      //Search for item player want to equip and equip it in the right place. 
      if(input == Inventory[i].name){ 
       Equipment[Inventory[i].purpose] = Inventory[i].name; 
       cout << "Successfully equiped!" << endl; 
      } 
     } 

稍作改進

請注意,對於您的遊戲的下一次改進,您會遇到類似的問題,因爲它是一個字符串矢量,您不會直接訪問該項目的屬性,例如對攻擊的影響或防守裝備。

因此你也應該做一個Equipmentvector<Item>

Item Empty{"<Empty>", 0, 0, 0}; 
vector<Item> Equipment{6, Empty}; //Current equipment: 6 x Empty. 
... 

你也可以考慮把庫存,便於訪問的map<string, Item>按名稱

+0

謝謝。還有一個問題:我如何從庫存中刪除項目?例如,如果玩家想要放置物品或物品是裝備的(如果裝備,物品不應該出現在庫存中) –

+1

那麼,在這種情況下,您可以[擦除](http://www.cplusplus.com/reference/vector/vector/erase /)庫存中使用的元素。根據遊戲規則,您可能還希望在將插槽與另一個插槽配對時將插槽的前一個項目返回到庫存。 – Christophe

+0

我試過_'Inventory [i] .name.erase()'_,但它只從玩家庫存中刪除物品名稱,而不是整個物品。 –