C ++文本-RPG库存系统 [英] C++ Text-RPG Inventory system

查看:52
本文介绍了C ++文本-RPG库存系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建text-rpg库存系统,但我不太确定如何正确创建合适的物品.例如,我可以装备玩家存货中的物品,但是我不能确定是哪种物品(剑,盾,手套或其他东西..),因为应该将物品放在适当的位置(头上的头盔,手中的剑)等等).有什么办法吗?

I'm building text-rpg inventory system but I'm not really sure how to properly create equipable items. For example, I can equip item which is in player inventory but I cannot identify what kind of item that is(Sword, Shield, Gloves or something else..) because item should be equiped in proper place(Helmet on head, sword in hands and so on). Is there any way to do so?

#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;   
}

推荐答案

有很多方法可以做到这一点.

There are plenty of possibilities to do that.

首先,您必须保持项目清单,而不是字符串清单:​​

First, you have to keep an inventory of items, not of strings:

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

然后,您必须使用 Inventroy [i] .name 进行搜索,然后使用 inventory [i] .purpose 查找正确的插槽:

Then you have to seacrh using Inventroy[i].name and use inventory[i].purpose to find the right slot:

        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;
            }
        }

轻微改进

请注意,为了游戏的下一个改进,您的装备也会遇到类似的问题:由于它是字符串的向量,因此您将无法直接访问该物品的属性,例如对物品的影响.攻击或防御设备.

Slight improvements

Note that for the next improvement of your game, you'll experience a similar problem with your equipement: as it is a vector of strings, you won't get a direct access to the item's properties, such as the effect on attack or defense the equipment has.

因此,您还应该将 Equipment 设为 vector< Item> :

Therefore you should also make Equipment a vector<Item>:

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

您也可以考虑将清单设置为 map< string,Item> 以便按名称轻松访问

You could also consider making Inventory a map<string, Item> for easy access by name

这篇关于C ++文本-RPG库存系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆