跳到主要内容

game_common.location

简介

组件接口

Location

占用多格坐标的位置

成员变量

变量名类型初始值须初始化描述
listarraynil可选坐标列表([ x1, y1, x2, y2, ... ])
为了减少坐标操作的处理,这里记录的是具体坐标

成员方法

函数原型函数作用
Location new_by_base_offset(int base_y, array offset_list)使用基准坐标和一组相对基准坐标的偏移列表创建位置
Location shift(int x_count = 0, int y_count = 0)移动位置生成新的位置
array get_base_xy()获取基准坐标(以x最小值, y最小值为基准坐标)
array get_base_offset()获取相对于基准坐标(get_base_xy()获得)的偏移列表
array get_offset(int base_x, int base_y)获取相对于指定基准坐标的偏移列表
int count()获取坐标数量
void add_xy(int x, int y)添加坐标
bool remove_xy(int x, int y)移除一个坐标
bool contains_xy(int x0, int y0)判断是否包含坐标
bool contains_location(Location other)判断是否包含目标位置
bool is_intersecting(Location other)判断是否和目标位置相交
bool is_equal(Location other)判断是否和目标位置为同一个位置(相等)
bool is_congruent(Location other)判断是否和目标位置大小和外形一致(不强制要求中心点一致)
void iterate_xy(function func)遍历所有坐标
bool iterate_xy_until_true(function func)遍历所有坐标直到func返回true
void iterate_intersect_xy(Location other, function func)遍历和目标位置相交部分的坐标
bool iterate_intersect_xy_until_true(Location other, function func)遍历和目标位置相交部分的坐标直到坐标满足条件
void iterate_diff_xy(Location other, function func)遍历不在目标位置内的所有坐标
bool iterate_diff_xy_until_true(Location other, function func)遍历不在目标位置内的所有坐标直到坐标满足条件

LocationSet

一组位置的集合

成员变量

变量名类型初始值须初始化描述
listarraynil可选位置集合

成员方法

函数原型函数作用
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);