游戏小地图的组装

说明

游戏里随机地图的需求
地图内容不是完全随机的,而是由多张小地图组合而成的
多个小地图的组合规则由点的连接来
做法是先把两张地图逻辑上放到一个超大地图里,再根据需要合并的两个点移动两块合并

地图组装代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
int array_1[][4] = {
{1, 1, 1, 1},
{3, 3, 3, 3},
{5, 5, 5, 7},
};
int array_2[][5] = {
{2, 2, 2, 2, 2},
{4, 4, 4, 4, 4},
{6, 6, 6, 6, 6},
{8, 8, 8, 8, 8},
};
enum array_dir
{
en_left,
en_right,
en_top,
en_down,
};

vector<vector<int> > new_array;

void init_new_array()
{
int _1_x = sizeof(array_1) / sizeof(array_1[0]);
int _1_y = sizeof(array_1[0]) / sizeof(array_1[0][0]);
int _2_x = sizeof(array_2) / sizeof(array_2[0]);
int _2_y = sizeof(array_2[0]) / sizeof(array_2[0][0]);
int max_x = _1_x + _2_x + 1;
int max_y = _1_y + _2_y + 1;
for (x..)
{
vector<int> line;
for (y..)
{
line.push_back(0);
}
new_array.push_back(line);
}
}

void insert_top_left(vector<vector<int> >& _array, int _x, int _y)
{
for (x = 0; x < _x; ++x)
{
for (y = 0; y < _y; ++y)
{
new_array[x][y] = _array[x][y];
}
}
}

void insert_down_right(vector<vector<int> >& _array, int _x, int _y, int _pos_x, int _pos_y)
{
for (x = _pos_x; x < _x + _pos_x; ++x)
{
for (y = _pos_y; y < _y + _pos_y; ++y)
{
new_array[x][y] = _array[x - _pos_x][y - _pos_y];
}
}
}

void mv_top_down(int _x, int _y)
{
for (int x = _x - 1; x >= 0; --x)
{
for (int y = 0; y < _y; ++y)
{
new_array[x + 1][y] = new_array[x][y];
}
}
for (int y = 0; y < _y; ++y)
new_array[0][y] = 0;
}

void mv_top_right(int _x, int _y)
{
for (int x = 0; x < _x; ++x)
{
for (int y = _y - 1; y >= 0; --y)
{
new_array[x][y + 1] = new_array[x][y];
}
}
for (int x = 0; x < _x; ++x)
{
new_array[x][0] = 0;
}
}

void mv_down_top(int _x, int _y, int _max_x, int _max_y)
{
for (int x = _x; x < _max_x; ++x)
{
for (int y = _y; y < _max_y; ++y)
{
new_array[x - 1][y] = new_array[x][y];
}
}
for (int y = _y; y < _max_y; ++y)
new_array[_max_x - 1][y] = 0;
}

void mv_down_left(int _x, int _y, int _max_x, int _max_y)
{
for (int x = _x; x < _max_x; ++x)
{
for (int y = _y; y < _max_y; ++y)
{
new_array[x][y - 1] = new_array[x][y];
}
}
for (int x = _x; x < _max_x; ++x)
new_array[x][_max_y - 1] = 0;
}

生成地图的测试代码

方式1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int _1_check_x = 2;
int _1_check_y = 3;
int _2_check_x = 3;
int _2_check_y = 0;
int _1_curr_check_x = _1_check_x;
int _2_curr_check_x = _2_check_x + _1_x + 1;

bool suit = false;
int mv_top_count = 0;
for (; mv_top_count < _1_x _ 1;)
{
mv_down_top(_1_x + 1 - mv_top_count, _1_y + 1, max_x, max_y);
++mv_top_count;
--_2_curr_check_x;
if(_1_curr_check_x == _2_curr_check_x)
{
suit = true;
break;
}
}
if (suit == false)
{
int mv_down_count = 0;
for (; mv_down_count < _2_x + 1;)
{
mv_top_down(_1_x, _1_y);
++mv_down_count;
++_1_curr_check_x;
if (_1_curr_check_x == _2_curr_check_x)
{
suit = true;
break;
}
}
}
方式2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int _1_check_x = 2;
int _1_check_y = 0;
int _2_check_x = 0;
int _2_check_y = 4;
int _1_curr_check_y = _1_check_y;
int _2_curr_check_y = _2_check_y + _1_y + 1;
int mv_left_count = 0;
for (; mv_left_count < _1_y + 1;)
{
mv_down_left(_1_x + 1, _1_y + 1 - mv_left_count, max_x, max_y);
++mv_left_count;
--_2_curr_check_y;
if (_1_curr_check_y == _2_curr_check_y)
{
suit = true;
break;
}
}
if (suit == false)
{
int mv_right_count = 0;
for (; mv_right_count < _2_y + 1;)
{
mv_top_right(_1_x, _1_y + mv_right_count);
++mv_right_count;
++_1_curr_check_y;
if (_1_curr_check_y == _2_curr_check_y)
{
suit = true;
break;
}
}
}

补充

该方法可以根据逻辑增加合适的策略规则
譬如统计链接方向选择下一个链接的方向,尽量让地图生成为方形而不是长条
如果chunk定义了链接方向,可以加入chunk的旋转和镜像,用于地图链接,避免出现因为定义了方向地图拼接失败的情况….

------ 本文结束 ------
------ 版权声明:转载请注明出处 ------