tutorial:entity
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:entity [2020/06/11 20:18] – created draylar | tutorial:entity [2022/01/11 16:31] – Clarify why you wouldn't use WaterCreatureEntity for fish as well simplycmd | ||
---|---|---|---|
Line 1: | Line 1: | ||
===== Creating an Entity ===== | ===== Creating an Entity ===== | ||
+ | |||
+ | //The source code for this project can be found [[https:// | ||
Entities are a movable object in a world with logic attached to them. A few examples include: | Entities are a movable object in a world with logic attached to them. A few examples include: | ||
Line 8: | Line 10: | ||
Living Entities are Entities that have health and can deal damage. | Living Entities are Entities that have health and can deal damage. | ||
There are various classes that branch off `LivingEntity` for different purposes, including: | There are various classes that branch off `LivingEntity` for different purposes, including: | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
* '' | * '' | ||
- | * '' | + | * '' |
- | What you extend depends on your needs and goals are. | + | What you extend depends on what your needs and goals are. |
As you get further down the chain, the entity logic becomes more specific and curated to certain tasks. | As you get further down the chain, the entity logic becomes more specific and curated to certain tasks. | ||
The two generic entity classes that come after '' | The two generic entity classes that come after '' | ||
* '' | * '' | ||
- | * '' | + | * '' |
- | '' | + | '' |
for pathfinding favor, and various AI tasks require this to operate. | for pathfinding favor, and various AI tasks require this to operate. | ||
- | In this tutorial, we will look at creating a cube entity that extends '' | + | In this tutorial, we will look at creating a cube entity that extends '' |
This entity will have a model & texture. Movement and mechanics will be covered in a future tutorial. | This entity will have a model & texture. Movement and mechanics will be covered in a future tutorial. | ||
===== Creating & Registering an Entity ===== | ===== Creating & Registering an Entity ===== | ||
- | Create a class that extends '' | + | Create a class that extends '' |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
/* | /* | ||
- | * Our Cube Entity extends | + | * Our Cube Entity extends |
* | * | ||
* LivingEntity has health and can deal damage. | * LivingEntity has health and can deal damage. | ||
* MobEntity has movement controls and AI capabilities. | * MobEntity has movement controls and AI capabilities. | ||
- | | + | |
*/ | */ | ||
- | public class CubeEntity extends | + | public class CubeEntity extends |
- | public CubeEntity(EntityType<? | + | public CubeEntity(EntityType<? |
super(entityType, | super(entityType, | ||
} | } | ||
Line 123: | Line 125: | ||
public class CubeEntityRenderer extends MobEntityRenderer< | public class CubeEntityRenderer extends MobEntityRenderer< | ||
- | public CubeEntityRenderer(EntityRenderDispatcher entityRenderDispatcher) { | + | public CubeEntityRenderer(EntityRendererFactory.Context context) { |
- | super(entityRenderDispatcher, new CubeEntityModel(), | + | super(context, new CubeEntityModel(context.getPart(EntityTestingClient.MODEL_CUBE_LAYER)), 0.5f); |
} | } | ||
Line 138: | Line 140: | ||
@Environment(EnvType.CLIENT) | @Environment(EnvType.CLIENT) | ||
public class EntityTestingClient implements ClientModInitializer { | public class EntityTestingClient implements ClientModInitializer { | ||
+ | public static final EntityModelLayer MODEL_CUBE_LAYER = new EntityModelLayer(new Identifier(" | ||
@Override | @Override | ||
public void onInitializeClient() { | public void onInitializeClient() { | ||
Line 146: | Line 148: | ||
* Entity Renderers can also manipulate the model before it renders based on entity context (EndermanEntityRenderer# | * Entity Renderers can also manipulate the model before it renders based on entity context (EndermanEntityRenderer# | ||
*/ | */ | ||
- | EntityRendererRegistry.INSTANCE.register(EntityTesting.CUBE, | + | EntityRendererRegistry.INSTANCE.register(EntityTesting.CUBE, |
- | return new CubeEntityRenderer(dispatcher); | + | return new CubeEntityRenderer(context); |
}); | }); | ||
+ | // In 1.17, use EntityRendererRegistry.register (seen below) instead of EntityRendererRegistry.INSTANCE.register (seen above) | ||
+ | EntityRendererRegistry.register(EntityTesting.CUBE, | ||
+ | return new CubeEntityRenderer(context); | ||
+ | }); | ||
+ | | ||
+ | EntityModelLayerRegistry.registerModelLayer(MODEL_CUBE_LAYER, | ||
} | } | ||
} | } | ||
Line 159: | Line 167: | ||
Standard models define " | Standard models define " | ||
- | initialize them in the constructor, | + | initialize them in the constructor, obtain data in the '' |
Note that '' | Note that '' | ||
- | |||
- | '' | ||
- | * the current model instance | ||
- | * textureOffsetU as an '' | ||
- | * textureOffsetV as an '' | ||
- | |||
- | Texture offsets provide the location of the model' | ||
- | Our entity is a single cube, so the base '' | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 175: | Line 175: | ||
private final ModelPart base; | private final ModelPart base; | ||
- | public CubeEntityModel() { | + | public CubeEntityModel(ModelPart modelPart) { |
- | base = new ModelPart(this, 0, 0); | + | |
} | } | ||
| | ||
Line 183: | Line 183: | ||
</ | </ | ||
- | After creating a part, we need to add a shape to it. | + | After creating a part, we need to add a shape to it. |
+ | To do so, we must add a child to the root. The texture location for the new part is located in '' | ||
+ | the offset for it is located in the first 3 numbers of '' | ||
Note that the origin of a model starts at the corner, so you will need to offset the part to center it: | Note that the origin of a model starts at the corner, so you will need to offset the part to center it: | ||
Line 192: | Line 194: | ||
public CubeEntityModel() { | public CubeEntityModel() { | ||
- | | + | |
- | base.addCuboid(-6, | + | |
} | } | ||
| | ||
- | | + | |
- | } | + | ModelData modelData = new ModelData(); |
+ | ModelPartData modelPartData = modelData.getRoot(); | ||
+ | modelPartData.addChild(EntityModelPartNames.CUBE, | ||
+ | } | ||
</ | </ | ||
Line 211: | Line 215: | ||
| | ||
public CubeEntityModel() [...] | public CubeEntityModel() [...] | ||
+ | | ||
+ | public static TexturedModelData getTexturedModelData() [...] | ||
@Override | @Override | ||
public void setAngles(CubeEntity entity, float limbAngle, float limbDistance, | public void setAngles(CubeEntity entity, float limbAngle, float limbDistance, | ||
- | |||
} | } | ||
@Override | @Override | ||
public void render(MatrixStack matrices, VertexConsumer vertices, int light, int overlay, float red, float green, float blue, float alpha) { | public void render(MatrixStack matrices, VertexConsumer vertices, int light, int overlay, float red, float green, float blue, float alpha) { | ||
- | | + | |
- | matrices.translate(0, 1.125, 0); | + | |
- | + | }); | |
- | // render cube | + | |
- | base.render(matrices, | + | |
} | } | ||
} | } | ||
Line 229: | Line 232: | ||
To complete our model, we need to add a texture file. The default texture size is 64 wide and 32 tall; | To complete our model, we need to add a texture file. The default texture size is 64 wide and 32 tall; | ||
- | you can change this by changing '' | + | you can change this by adding a return of your texturedModelData |
We will set it to 64x64 for our texture: | We will set it to 64x64 for our texture: | ||
Line 239: | Line 242: | ||
private final ModelPart base; | private final ModelPart base; | ||
- | public | + | |
- | this.textureHeight = 64; | + | |
- | this.textureWidth = 64; | + | |
- | + | ||
[...] | [...] | ||
+ | return TexturedModelData.of(modelData, | ||
} | } | ||
Line 252: | Line 255: | ||
===== Spawning your Entity ===== | ===== Spawning your Entity ===== | ||
+ | Be sure to add your client entrypoint to fabric.mod.json. | ||
+ | You can do this like so: | ||
+ | <code json> | ||
+ | |||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ], | ||
+ | " | ||
+ | " | ||
+ | ] | ||
+ | }, | ||
+ | </ | ||
You can spawn your entity by typing ''/ | You can spawn your entity by typing ''/ | ||
{{https:// | {{https:// | ||
+ | |||
+ | **NOTE:** If your entity does not extend '' | ||
+ | ===== Adding tasks & activities ===== | ||
+ | |||
+ | To add activities see [[: | ||
tutorial/entity.txt · Last modified: 2024/04/30 20:42 by ryhon