1

我有一个我一直在做的游戏。这是一款基于文本的RPG冒险游戏。我希望让玩家使用简单的文本命令来导航世界。我已将其设置为将世界划分为“区域”或数组。每个区域都是自定义数据类型 Location 并由一个二维数组组成。我将使用两个变量来跟踪玩家位置,它们实际上是“区域”网格上的点位置的两个索引值。当玩家输入诸如“北”、“南”、“东”或“西”之类的方向命令时,它将增加或减少这些值以“移动”玩家。当他们到达过渡区时,它将把他们移动到另一个“区域”。我在想我可以有一个包含所有区域的数组。过渡区域只会提高或降低主阵列的索引以“过渡”到下一个区域。我当然会让过渡空间有一个值来存储玩家最终会在下一个区域的网格上的位置。我只是想知道如何制作这个“主阵列”来保存区域阵列。只是问我是否有什么我没有解释得不够好。先感谢您。

struct Location
{
    int type, destX, destY;
    // 1 = battlefield
    //  areas where random encounters will at some future date occur
    // 2 = town
    //  areas where hopefully the foolhardy adventurer will be able to speak
    // to merchants and towns folk
    // 3 = dungeon
    //  areas with long travel times and the promise of an end boss
    //  but more importantly really awesome loot
    // 4 = transition points
    //
    string name, desc;
};

这就是我的设想。

EgForest

[][][]
[][][]
[][][]
^过渡区将使用存储的值 destX 和 destY SRavine
将玩家移动到以下“区域”中的目的地, 该地点是目的地 V [][ ][][][] [][][][][][][] [][][][] [][][][]









4

3 回答 3

0

Just regarding the headline of your question, this is you create one dimensional arrays of two-dimensional arrays in C++:

std::vector<std::vector<std::vector<Entity>>> v;

That's a multidimensional where each sub-array can have a size that is independent of each other.

Note that C++ has also fixed-size-arrays (one-dimensional, too):

std::array<std::array<std::array<int,16>,16,16>> 
         array_16_of_array_16_of_array_16_of_int;

Generally, prefer standard-containers over direct arrays. This opens you up for a world of algorithms (e.g. the algorithm header) and helper methods (.size()).

Unfortunately, C++ has no builtin multidimensional containers, but some libraries provide them, or you could make your own. A basic implementation may look like this:

template <typename T>
class surface {
public:
    typedef size_t size_type;

    surface(size_type w, size_type h) : width_(w), height_(h), data_(w*h) {}

    size_type width()  const { return width_;  }
    size_type height() const { return height_; }
    size_type size()   const { return width_*height_; }

    // These are for when you need to loop over all elements
    // without interest in coordinates.
    T  operator[] (size_type i) const { return data_[i]; }
    T& operator[] (size_type i)       { return data_[i]; }

    T  operator() (size_type x, size_type y) const { return data_[y*width_+x]; }
    T& operator() (size_type x, size_type y)       { return data_[y*width_+x]; }

    T at(size_type i) const {
        if (i>=size()) throw std::out_of_range(....);
        return (*this)[i];
    }
    T& at(size_type i) {
        if (i>=size()) throw std::out_of_range(....);
        return (*this)[i];
    }
    T at(size_type x, size_type y) const {
        if (x>=width_ || y>=height_) throw std::out_of_range(....);
        return (*this)(x,y);
    }
    T& at(size_type x, size_type y) {
        if (x>=width_ || y>=height_) throw std::out_of_range(....);
        return (*this)(x,y);
    }

private:
    size_type width_, height_;
    std::vector<T> data_;
};

...

surface<Foo> x(256,256);

x(16,12) = Foo();
x[16+12*256] = Foo();

Depending on the size and access ordering on your array, you may use other indexers (e.g. Morton/Z-Order indexing).

template <typename T, typename Indexer=LinearIndexer>
...
    T  operator() (size_type x, size_type y) const { return data_[Indexer()(x,y)]; }

...

surface<int, Morton> foo(102400, 102400);

And possibly you even templatize the container too allow for exotic cases like containers that can just-in-time-load/save from/to disk.

于 2012-07-26T08:18:44.970 回答
0

3 dimensional array is the answer.

int world[4][5][6];

4 is the amount of regions, 5 and 6 the 2D array. If you need variable sizes for the areas, use pointers and allocate the amount you need.

于 2012-07-26T08:18:57.997 回答
0

您可以将所有矩阵简化为一个大的一维数组。对于每个区域,您都知道它在数组中的偏移量,以及它的宽度和高度。区域内的导航很简单,+ 1/- 表示东/西,+ region_width/- 表示南/北。遍历区域边界时需要特殊代码,以防止它或跳转到另一个区域。

我建议你把世界从一堆矩阵变成一张图。图中的一个位置至少有 4 个(但可能更多)出口槽。插槽将有一个方向标识符(除了 n、s、e、w,可能有“上”、“下”、“门”、“楼梯”和任何你喜欢的东西),或者是隐式的(以插槽而闻名) ) 或显式(与插槽一起存储),以及此插槽通向的位置的标识符。该标识符可以是“一个大的一维数组”中的位置索引。这种方法的缺点是更多的簿记。这样做的好处是你有更多的自由来创造你的世界。该图在特殊情况下会发光。您可能有超过 4 个出口、单向通道或令人难以置信的区域,例如下水道,您认为您会往北走,但它会带您向西,或类似的地方。

于 2012-07-26T09:33:21.013 回答