User Tools

Site Tools


zh_cn:tutorial:features
no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


Next revision
zh_cn:tutorial:features [2020/01/08 12:18] – created lightcolour
Line 1: Line 1:
 +===== 在您的世界中生成Feature =====
 +岩石,树木,矿石和池塘都是Feature的示例。 它们是对世界的简单补充,它们的生成取决于它们的配置方式。 在本教程中,我们将研究如何在我们的世界中随机生成一个简单的石螺旋Feature。
 +==== 创建Feature类 ====
 +一个简单的Feature如下所示:
 +<code java [enable_line_numbers="true"]>
 +public class StoneSpiralFeature extends Feature<DefaultFeatureConfig> {
  
 +    public StoneSpiralFeature(Function<Dynamic<?>, ? extends DefaultFeatureConfig> config) {
 +        super(config);
 +    }
 +
 +    @Override
 +    public boolean generate(IWorld world, ChunkGenerator<? extends ChunkGeneratorConfig> chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig config) {
 +        BlockPos topPos = world.getTopPosition(Heightmap.Type.WORLD_SURFACE, pos);
 +        Direction offset = Direction.NORTH;
 +
 +        for (int y = 1; y < 16; y++) {
 +            offset = offset.rotateYClockwise();
 +            world.setBlockState(topPos.up(y).offset(offset), Blocks.STONE.getDefaultState(), 3);
 +        }
 +
 +        return true;
 +    }
 +}
 +</code>
 +
 +构造函数采用''Function<Dynamic<? extends DefaultFeatureConfig>>'',这是数据修复程序配置实例的工厂。 您可以直接在超级调用中或在实例化功能时为默认配置功能传递''DefaultFeatureConfig :: deserialize''
 +
 +当块决定生成Feature时,将调用``generate``。 如果将功能配置为产生每个块,则也会为正在生成的每个块调用此功能。 在将功能配置为以每个生物群落以一定速率生成的情况下,仅在世界想要生成结构的情况下才调用``generate``。
 +
 +在我们的实现中,我们将从世界的最高位置开始构建一个简单的16块高的石头螺旋:
 +
 +<code java>
 +@Override
 +public boolean generate(IWorld world, ChunkGenerator<? extends ChunkGeneratorConfig> chunkGenerator, Random random, BlockPos pos, DefaultFeatureConfig config) {
 +    BlockPos topPos = world.getTopPosition(Heightmap.Type.WORLD_SURFACE, pos);
 +    Direction offset = Direction.NORTH;
 +
 +    for (int y = 1; y < 16; y++) {
 +        offset = offset.rotateYClockwise();
 +        world.setBlockState(topPos.up(y).offset(offset), Blocks.STONE.getDefaultState(), 3);
 +    }
 +
 +    return true;
 +}
 +</code>
 +
 +==== 注册一个Feature ====
 +可以像注册游戏中的其他大多数内容一样注册Feature,而且您不必担心任何特殊的构建器或机制。
 +<code java>
 +private static final Feature<DefaultFeatureConfig> LAVA_HOLE = Registry.register(
 + Registry.FEATURE,
 + new Identifier("tutorial", "stone_spiral"),
 + new StoneSpiralFeature(DefaultFeatureConfig::deserialize)
 +);
 +</code>
 +
 +==== 向生物群落添加Feature ====
 +生物群系有一种称为''addFeature''的方法,用于将Feature添加到生物群落的生成过程中。 您可以在每个生物群落类(例如''ForestBiome''或''SavannaBiome'')中查看此方法的更详细用法。
 +
 +我们可以遍历''Registry.BIOME''以将我们的Feature添加到每个生物群系中。
 +<code java>
 +Registry.BIOME.forEach(biome -> biome.addFeature(
 +        GenerationStep.Feature.RAW_GENERATION,
 + Biome.configureFeature(
 + LAVA_HOLE,
 + new DefaultFeatureConfig(),
 + Decorator.CHANCE_HEIGHTMAP,
 + new ChanceDecoratorConfig(100)
 + )
 +));
 +</code>
 +
 +''addFeature''的第一个参数有助于确定何时生成结构。 对于地上的房屋,您可以使用''SURFACE_STRUCTURES'',对于洞穴,您可以使用'' RAW_GENERATION''
 +
 +''addFeature''的第二个参数是ConfiguredFeature,可以通过''Biome.configureFeature''创建。 后者接收功能的实例,功能的config类,装饰器和装饰器配置的实例。
 +
 +装饰器代表世界如何选择放置要素。 ''CHANCE_HEIGHTMAP''可通过查看高度图来工作,而''NOISE_HEIGHTMAP_32''可使用噪声来工作。 要选择正确的装饰器,请签出与您自己风格相似的香草功能。 装饰器配置将分支此分支; 对于''CHANCE_HEIGHTMAP'',您将传入''ChanceHeightmapDecorator的实例。
 +
 +=== 结果 ===
 +{{https://i.imgur.com/Kr59o0B.png}}
zh_cn/tutorial/features.txt · Last modified: 2022/08/18 03:37 by solidblock