game_common.location
简介
组件接口
类
Location
占用多格坐标的位置
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|---|---|---|---|
list | array | nil | 可选 | 坐标列表([ x1, y1, x2, y2, ... ]) 为了减少坐标操作的处理,这里记录的是具体坐标 |
成员方法
LocationSet
一组位置的集合
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|---|---|---|---|
list | array | nil | 可选 | 位置集合 |
成员方法
函数原型 | 函数作用 |
---|---|
int count() | 获取位置数量 |
Location merge() | 合并位置集合里的所有位置生成一个新的位置(重叠坐标只保留一个) |
void add_location(Location location) | 添加一个位置 |
bool remove_location(Location location) | 移除一个位置 |
bool is_intersecting(LocationSet other) | 是否和目标位置集合相交 |
bool contains_location(Location location) | 是否包含目标位置(位置集合里有至少有一个跟目标位置相等) |
bool contains_xy_in_location(Location location) | 是否包含目标位置里的所有坐标 |
bool contains_xy(int x, int y) | 是否包含目标坐标 |
Location find_location_by_xy(int x, int y) | 通过坐标找到包含它的位置(第一个满足的) |
void iterate_location(function func) | 遍历所有位置 |
bool iterate_location_until_true(function func) | 遍历所有位置直到满足条件 |
样例
Location location1 = Location.new_by_base_offset(1, 2, [ 0, 0, 0, 1, 1, 0, 1, 1 ]);
test_equal(location1.count(), 4);
Location location2 = Location.new_by_base_offset(3, 4, [ 0, 0, 0, 1, 1, 0, 1, 1 ]);
test_equal(location2.count(), 4);
Location location3 = Location.new_by_base_offset(2, 3, [ 0, 0, 0, 1, 1, 0, 1, 1 ]);
test_equal(location3.count(), 4);
test_equal(location1.is_equal(location2), false);
test_equal(location1.is_equal(location1.shift(0, 0)), true);
test_equal(location1.is_congruent(location1.shift(10, 10)), true);
test_equal(location1.contains_location(location2), false);
test_equal(location1.is_congruent(location2), true);
test_equal(location1.iterate_xy_until_true((int x, int y) {
return x == 2 && y == 3;
}), true);
test_equal(location1.iterate_xy_until_true((int x, int y) {
return x == 3 && y == 4;
}), false);
location1.add_xy(4, 5);
test_equal(location1.count(), 5);
location1.iterate_xy((int x, int y) {
printf("Location1->(%d, %d)\n", x, y);
});
test_equal(location1.remove_xy(4, 5), true);
test_equal(location1.remove_xy(4, 5), false);
location1.iterate_diff_xy(location3, (int x, int y) {
printf("Location1 diff location3->(%d, %d)\n", x, y);
});
location3.iterate_diff_xy(location1, (int x, int y) {
printf("Location3 diff location1->(%d, %d)\n", x, y);
});
location1.iterate_intersect_xy(location3, (int x, int y) {
printf("Location1 intersect location3->(%d, %d)\n", x, y);
});
location3.iterate_intersect_xy(location1, (int x, int y) {
printf("Location1 intersect location1->(%d, %d)\n", x, y);
});
printf("------locationset------\n");
LocationSet s1 = LocationSet.new();
s1.add_location(location1);
s1.add_location(location2);
test_equal(s1.count() , 2);
test_equal(s1.find_location_by_xy(2, 3), location1);
test_equal(s1.find_location_by_xy(4, 5), location2);
LocationSet s2 = LocationSet.new();
s2.add_location(location2);
s2.add_location(location3);
test_equal(s2.count() , 2);
test_equal(s1.is_intersecting(s2), true);
Location location4 = s1.merge();
Location location5 = s1.merge();
test_equal(location4.is_equal(location5), true);
test_equal(location4.is_congruent(location5.shift(10, 10)), true);