zh_cn:tutorial:entity
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
zh_cn:tutorial:entity [2019/12/19 10:30] – created lightcolour | zh_cn:tutorial:entity [2023/01/15 01:23] (current) – [创建一个实体] solidblock | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Adding an Entity ====== | + | ===== 创建一个实体 |
- | ==== Introduction ==== | + | // |
- | Entities are the next step to take after adding an item and block to your game. | + | 实体(Entity)是游戏世界里的一种可以根据附加的逻辑移动的物体,包括: |
+ | * 矿车 | ||
+ | * 箭 | ||
+ | * 船 | ||
+ | |||
+ | 生物实体(Living Entity)是拥有生命值,并且可以造成伤害的实体。 | ||
+ | 为了实现不同的功能,生物实体有着不同的分支类型,其中有: | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | 具体继承、使用哪个取决于你的需求和目的。 | ||
+ | 随着开发进度的推进,对于特定的任务,实体的逻辑变得越来越具体、简明。有以下两种继承自 '' | ||
+ | * '' | ||
+ | * '' | ||
+ | |||
+ | '' | ||
- | To add an entity, you will need 3 primary classes: | + | 在这篇教程中,我们将研究创建一个立方体实体,并继承 '' |
+ | ===== 创建并注册一个实体 ===== | ||
- | * an Entity class, which gives your creature logic/AI | + | 创建一个继承自 '' |
- | * a Renderer class, which allows you to connect your entity to a model | + | |
- | * a Model class, which is what your player sees in the game | + | |
- | We're going to be creating a cookie creeper that launches cookies everywhere when it explodes. | ||
- | ==== Registering an Entity ==== | + | <code java [enable_line_numbers=" |
- | + | /* | |
- | Unlike blocks & items, you basically always want a class fully dedicated to your entity. We're making a creeper clone, so we'll make our entity class extend CreeperEntity: | + | * 我们创建的实体继承自 PathAwareEntity, 它继承自 MobEntity, 而 MobEntity 继承自 LivingEntity. |
- | <code java> | + | * |
- | public class CookieCreeperEntity | + | * LivingEntity 拥有生命值,并且可以造成伤害。 |
- | | + | * MobEntity 具有AI逻辑和移动控制。 |
+ | * PathAwareEntity 提供额外的寻路系统,很多AI任务都需要用到寻路。 | ||
+ | */ | ||
+ | public class CubeEntity | ||
+ | | ||
+ | public CubeEntity(EntityType<? | ||
+ | super(entityType, | ||
+ | } | ||
} | } | ||
</ | </ | ||
- | Your IDE should instruct you to create a constructor matching the super-- do that now. | + | 你可以在 '' |
- | + | ||
- | To register your entity, we'll use Registry.ENTITY_TYPE. To get the required registry instance, you can either use EntityType.Builder | + | |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public static final EntityType< | + | public class EntityTesting implements ModInitializer { |
- | | + | |
- | | + | /* |
- | new Identifier(" | + | * 使用“entitytesting: |
- | FabricEntityTypeBuilder.create(EntityCategory.AMBIENT, CookieCreeperEntity::new).size(EntityDimensions.fixed(1, 2)).build() | + | * |
+ | * 这个实体注册在了 SpawnGroup# | ||
+ | * 它有一个 0.75 × 0.75(或12个像素宽,即一个方块的3/ | ||
+ | */ | ||
+ | | ||
+ | | ||
+ | new Identifier(" | ||
+ | FabricEntityTypeBuilder.create(SpawnGroup.CREATURE, CubeEntity::new).dimensions(EntityDimensions.fixed(0.75f, 0.75f)).build() | ||
); | ); | ||
+ | | ||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | | ||
+ | } | ||
+ | } | ||
</ | </ | ||
- | The size() method allows you to set the hitbox of your entity. A creeper is 1 block wide and 2 blocks tall, so we'll use (1, 2). | + | 实体需要// |
- | If you load up your game at this point, you will be able to use /summon to see your creation. If all went right, it should appear as a normal creeper. I would not recommend going into survival. | + | ===== 注册实体的属性 ===== |
- | ==== Creating a renderer ==== | + | **属性**定义了一个生物拥有的基本数值:它有多少生命值?它能造成多少伤害?它有默认的装备栏吗? |
- | Our Cookie creeper automatically has a model because it extended the Creeper class. We're going to change the skin to a cookie skin instead of the normal green camo color. | + | 大部分的原版实体都有一个静态方法用于返回它们的属性(比如说 '' |
+ | 我们的自制实体没有任何独立的数值,目前,我们可以直接使用 '' | ||
- | First, create a MobEntityRenderer class. MobEntityRenderer has 2 generic types: the entity & model. Because we're using the Creeper model to start, we'll also have to tell Creeper model this is not a Creeper Entity by giving it a type as well. | + | 原版里有一个 |
+ | 想要使用它并不容易,所以Fabric提供一个 '' | ||
+ | 你应该在你的Mod的初始化部分的某处注册默认的属性: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public class CookieCreeperRenderer extends MobEntityRenderer< | + | public class EntityTesting implements ModInitializer |
- | [...] | + | |
+ | public static final EntityType< | ||
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | /* | ||
+ | * 注册我们方块实体的默认属性。 | ||
+ | * 属性是一个生物当前状态的数值,其中有攻击伤害和生命值等。 | ||
+ | * 如果实体没有及时注册适当的属性,则游戏将崩溃。 | ||
+ | * | ||
+ | * 在1.15中,它是通过重写实体类内部的方法来完成的。 | ||
+ | * 大部分的原版实体都有一个静态方法(例如, | ||
+ | */ | ||
+ | FabricDefaultAttributeRegistry.register(CUBE, | ||
+ | } | ||
} | } | ||
</ | </ | ||
- | You'll need to override the getTexture method as well as adding in the constructor. The constructor, | ||
- | <code java> | + | ===== 注册实体的渲染器 ===== |
- | public | + | |
- | { | + | 最后一个需要注册的是实体的**渲染器**。渲染器一般通过提供模型来决定实体的 // |
- | super(entityRenderDispatcher_1, new CreeperEntityModel<> | + | '' |
+ | * '' | ||
+ | * '' | ||
+ | * 实体阴影的大小,'' | ||
+ | |||
+ | 下面的代码展示了一个简单的实体渲染器,阴影大小是 0.5f,纹理的路径为 '' | ||
+ | 注意:用到的纹理和模型将在下一步创建。 | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | /* | ||
+ | * 一个用来提供模型、阴影大小和纹理的渲染器 | ||
+ | */ | ||
+ | public | ||
+ | |||
+ | public CubeEntityRenderer(EntityRenderDispatcher | ||
+ | super(entityRenderDispatcher, new CubeEntityModel(), 0.5f); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public Identifier getTexture(CubeEntity entity) { | ||
+ | return new Identifier(" | ||
+ | } | ||
} | } | ||
</ | </ | ||
- | |||
- | For the getTexture method, you need to return your model' | ||
- | The default entity texture folder convention is: textures/entity/entity_name/ | + | 在**客户端初始化类**中使用 '' |
+ | <code java [enable_line_numbers=" | ||
+ | @Environment(EnvType.CLIENT) | ||
+ | public class EntityTestingClient implements ClientModInitializer { | ||
+ | |||
+ | @Override | ||
+ | public void onInitializeClient() { | ||
+ | | ||
+ | * 方块实体渲染器的注册,提供模型、阴影大小和纹理的渲染器。 | ||
+ | * | ||
+ | * 实体渲染器也可以在实体基于上下文进行渲染前(EndermanEntityRenderer# | ||
+ | */ | ||
+ | EntityRendererRegistry.INSTANCE.register(EntityTesting.CUBE, | ||
+ | return new CubeEntityRenderer(dispatcher); | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 创建模型和纹理 ===== | ||
+ | |||
+ | 完成实体创建的最后一步是创建模型和纹理。模型定义了实体的// | ||
+ | |||
+ | 标准的模型在类的顶部提供并在构造方法中初始化“部位(parts)”,即 '' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | @Override | + | public class CubeEntityModel extends EntityModel< |
- | protected Identifier getTexture(CookieCreeperEntity cookieCreeperEntity) | + | |
- | { | + | private final ModelPart base; |
- | | + | |
+ | public CubeEntityModel(ModelPart modelPart) { | ||
+ | base = modelPart.getChild(EntityModelPartNames.CUBE); | ||
+ | } | ||
+ | |||
+ | [...] | ||
} | } | ||
</ | </ | ||
+ | 在创建一个部位之后,我们需要添加一个形状(shape)。为此,我们必须为根部添加一个子形状。新部分的纹理位置在 '' | ||
- | with the file being stored at resources/assets/wiki-entity/ | + | <code java [enable_line_numbers=" |
+ | public class CubeEntityModel extends EntityModel< | ||
+ | |||
+ | private final ModelPart base; | ||
+ | |||
+ | public CubeEntityModel() { | ||
+ | [...] | ||
+ | } | ||
+ | |||
+ | | ||
+ | public static TexturedModelData getTexturedModelData() { | ||
+ | ModelData modelData = new ModelData(); | ||
+ | ModelPartData modelPartData = modelData.getRoot(); | ||
+ | modelPartData.addChild(EntityModelPartNames.CUBE, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
- | Finally, you'll need to connect your entity to your renderer. As rendering only happens client-side, you should always do this kind of work in a ClientModInitializer: | + | 我们的实体模型现在有了一个 12x12x12 的方块(大约一个方块的75%),并以0, 0, 0为中心。'' |
- | <code java> | + | <code java [enable_line_numbers=" |
- | EntityRendererRegistry.INSTANCE.register(CookieCreeperEntity.class, (entityRenderDispatcher, context) -> new CookieCreeperRenderer(entityRenderDispatcher)); | + | public class CubeEntityModel extends EntityModel< |
+ | |||
+ | private final ModelPart base; | ||
+ | |||
+ | public CubeEntityModel() [...] | ||
+ | |||
+ | @Override | ||
+ | public void setAngles(CubeEntity entity, float limbAngle, float limbDistance, | ||
+ | |||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void render(MatrixStack matrices, VertexConsumer vertices, int light, int overlay, float red, float green, float blue, float alpha) { | ||
+ | ImmutableList.of(this.base).forEach((modelRenderer) -> { | ||
+ | modelRenderer.render(matrices, vertices, light, overlay, red, green, blue, alpha); | ||
+ | }); | ||
+ | } | ||
+ | } | ||
</ | </ | ||
+ | 要完成我们的模型,我们还需要一个纹理文件。默认的纹理大小是64宽、32高的;你可以通过在 '' | ||
- | This links our entity to our new renderer | + | {{https:// |
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | public | ||
+ | |||
+ | private final ModelPart base; | ||
+ | |||
+ | public static TexturedModelData getTexturedModelData() { | ||
+ | [...] | ||
+ | return TexturedModelData.of(modelData, 64, 64); | ||
+ | } | ||
+ | |||
+ | [...] | ||
+ | } | ||
+ | </ | ||
- | https://i.imgur.com/8Gfc2sV.jpg | + | ===== 生成你的实体 ===== |
+ | 记得将客户端入口点添加到 fabric.mod.json 中,像这样: | ||
+ | <code json> | ||
+ | |||
+ | " | ||
+ | " | ||
+ | "mod.fabricmc.entitytesting.EntityTesting" | ||
+ | ], | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | }, | ||
+ | </ | ||
- | If you wanted to use your own model, you could create a new class that extends EntityModel and exchange the Creeper model in our renderer for it. This is fairly complex and will be covered in a separate tutorial. | + | 你可以在游戏内使用 ''/ |
+ | {{https://i.imgur.com/ | ||
+ | **注意:**如果你的实体没有继承 '' |
zh_cn/tutorial/entity.1576751436.txt.gz · Last modified: 2019/12/19 10:30 by lightcolour