Repository navigation
JSON Based Entity Animations (1.15.2)
For animation to be used you don't need to write it manually, it will be loaded from the mod's resources.
Full example can be seen in this package: ru.timeconqueror.timecore.animation_example
One of the most convenient way to do them in my opinion is Blockbench. The JSON models that are needed for animations are nothing more than the Bedrock Entity Model, which can also be done in Blockbench (Link).
In order to load animations, they must be located along this path: resources/data/<your_mod_id>/path, so they should be in a mod's datapack.
You should load animations during FMLCommonSetupEvent.
To load animations, you need to use one of the provided methods in the AnimationAPI, depending on how many animations are contained in one file:
-
Animation loadAndRegisterAnimation(ResourceLocation animationFileLocation)- Creates and registers animation, which is a single one in the file with provided path. -
Map<String, Animation> loadAndRegisterAnimations(ResourceLocation animationFileLocation)- Creates a map of all animations in the file with provided path and registers all retrieved animations. Key in this map is the name of the animation, that is indicated in the file.
animationFileLocation is a path to the file, it should contain path to the file under the data/ folder.
Example: new ResourceLocation(TimeCore.MODID, "animations/zombie_hit.json")) will result in data/timecore/animations/zombie_hit.json.
The system can also reverse the animation, i.e. make it play in the opposite direction.
This can be done using AnimationAPI.register (Animation animation) during the FMLCommonSetupEvent event. It is important to register this animation (during the same event) with the AnimationAPI.register (Animation animation) method.
Example:
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class Animations {
public static Animation floroWalk;
public static Animation floroShoot;
public static Animation floroReveal;
public static Animation floroHidden;
public static Animation floroHide;
@SubscribeEvent
public static void registerAnimations(FMLCommonSetupEvent event) {
floroWalk = AnimationAPI.loadAndRegisterAnimation(new ResourceLocation(TimeCore.MODID, "animations/floro.walk.json"));
floroShoot = AnimationAPI.loadAndRegisterAnimation(new ResourceLocation(TimeCore.MODID, "animations/floro.shoot.json"));
floroReveal = AnimationAPI.loadAndRegisterAnimation(new ResourceLocation(TimeCore.MODID, ("animations/floro.showing.json"));
floroHidden = AnimationAPI.loadAndRegisterAnimation(new ResourceLocation(TimeCore.MODID, "animations/floro.hidden.json"));
floroHide = AnimationAPI.register(AnimationAPI.reverse(floroReveal));
}
}For the animations to work, the entity must have a JSON model.
To do this, we also need to load them from a file. It's better to do this on the FMLClientSetupEvent event. Use the TimeModelLoader.loadJsonEntityModel method to load them. More details about the method can be found in its javadoc.
Example:
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class EntityRegistry {
public static TimeEntityModel<FloroEntity> floroModel;
@SubscribeEvent
public static void registerRenders(FMLClientSetupEvent event) {
floroModel = TimeModelLoader.loadJsonEntityModel(new ResourceLocation(TimeCore.MODID, "models/entity/floro.json"));
}
}The JSON model has a setScaleMultiplier method that can be used to change the initial scale of the model.
The Action Manager is the entry point for everything related to animations, and it also manages animated actions.
Using its enableAction and disableAction methods, you can enable and disable delayed actions (for example, on a tick or if something happened), which immediately start the animation, and then at a specified time (while this animation is being performed) perform some tasks. After the animation ends, the action will turn itself off.
From the ActionManager, we can refer to the AnimationManager with getAnimationManager(). It allows you to directly control the animations themselves.
When the animation is played, it's divided into so-called layers. They are needed in order to be able to simultaneously play several animations at once for one entity. Layer contains:
-
blendType. It determines how animation on this layer will be applied to the entity depending on the previous ones.BLENDING: Animation on this layer will overwrite all positions, rotations, scales that were applied by the animations on the previous layers;ADDING: Animation on this layer will add all positions, rotations, scales to the corresponding ones from the animations on the previous layers. -
weight. The number from 0 to 1, which determines, how much the animation will overlap with the ones from the previous layers. It's like a percentage. For example, if animation rotates model piece by 90 degrees and weight is 0.5, then model piece will be rotated by 45 degrees (50% from 90 degrees).
To start the animation, we need to create a Starter. That can be achieved by creating a new instance of AnimationStarter: new AnimationStarter (YOUR_ANIMATION). The Starter itself is an animation wrapper that allows you to customize one animation for many situations.
Its optional settings ( which you can change with setters):
-
ignorability. When you start this animation on the layer, which is playing the same animation, it won't be rerun. Useful for walking animations, so you don't need to worry how to control animation endings. Default: true. -
transitionTimedefines the time (in milliseconds) of the transition animation between the previous animation and the one we want to run. ** Default: AnimationConstants.BASIC_TRANSITION_TIME **. -
speed- a factor that will speed up or slow down the animation. Default: 1F. -
nextAnimation- setting with which you can make a chain of played animations. As soon as one ends, the next one will start immediately. This setting will avoid unpleasant flickering when moving from one animation to another. Default: null.
So for the animations to work, we need to make the entity class implement the interface AnimationProvider <YourEntityClass>.
Now you need a field containing the Action Manager of this creature: private final ActionManager <FloroEntity> actionManager; and this is the field that should be returned in the getActionManager method of the AnimationProvider interface.
Result:
public class FloroEntity extends MonsterEntity implements IRangedAttackMob, AnimationProvider<FloroEntity> {
private final ActionManager<FloroEntity> actionManager;
...
@Override
public @NotNull ActionManager<FloroEntity> getActionManager() {
return actionManager;
}
}Next, you need to initialize ActionManager. This must be done in the Entity constructor with ActionManagerBuilder.create.
As a param ActionManagerBuilder.create requires AnimationManagerBuilder, which can be created via AnimationManagerBuilder.create.
AnimationManagerBuilder's settings:
-
addLayer- adds layer to the animation manager with providedlayerName,blendTypeandweight, which are described earlier. -
addMainLayer- adds the layer with theAnimationConstants.MAIN_LAYER_NAMEname,OVERRIDEblend type and weight, which is equal 1. ussually you don't need it. It will be added automatically as a single layer, if you don't add any layer by yourselves. -
addWalkingAnimationHandling. This is a built-in way of handling walking animations. Here you need to provide an animation, which will be started, when entity walks and the name of the layer, where this animation will be played. Animation should be looped!
public class FloroEntity extends MonsterEntity implements IRangedAttackMob, AnimationProvider<FloroEntity> {
private static final String LAYER_SHOWING = "showing";
private static final String LAYER_WALKING = "walking";
private static final String LAYER_ATTACK = "attack";
private final ActionManager<FloroEntity> actionManager;
public FloroEntity(EntityType<? extends FloroEntity> type, World world) {
super(type, world);
actionManager = ActionManagerBuilder.<FloroEntity>create(
AnimationManagerBuilder.create()
.addLayer(LAYER_SHOWING, BlendType.OVERRIDE, 1F)
.addLayer(LAYER_WALKING, BlendType.ADDING, 1F)
.addLayer(LAYER_ATTACK, BlendType.ADDING, 0.9F)
.addWalkingAnimationHandling(new AnimationStarter(Animations.floroWalk).setSpeed(3F), LAYER_WALKING)
).build(this, world);
}
@Override
public @NotNull ActionManager<FloroEntity> getActionManager() {
return actionManager;
}
}The following methods can be used to start the animation (they do the same thing):
AnimationAPI.startAnimationAnimationStarter.startAtAnimationManager.setAnimation
To find out which animation is currently playing on a particular layer, you can use
AnimationManager.getLayer(YOUR_LAYER_NAME).getCurrentAnimation().
To remove the animation that is currently playing on the layer, you can use the following methods (they do the same thing):
AnimationAPI.removeAnimationAnimationManager.removeAnimation
Animations can be manipulated on either side, be it client or server, they will sync.
You can run them at any time in the existence of a entity, for example, on tick.
Delayed Actions are needed in order to play some tasks at some point while the animation is running. They are very useful for animated versions of Entity's AI.
General Information: They are Entity-independent, so you don't need to create them every time the entity is created, just make them static.
private static final DelayedAction<FloroEntity, Void> HIDING_ACTION;
The first generic type determines the entity, for which Action can be started. The second generic type represents the extra information, that can be accessed later in setOnCall.
private static final DelayedAction<FloroEntity, Void> HIDING_ACTION =
new DelayedAction<FloroEntity, Void>(new ResourceLocation("timecore:floro/hiding"), new AnimationStarter(Animations.floroHide), LAYER_SHOWING)
.setDelayPredicate(StandardDelayPredicates.onEnd())
.setOnCall((floroEntity, nothing) -> floroEntity.setHidden(true));DelayedAction constructor:
DelayedAction(ResourceLocation id, AnimationStarter animationStarter, String animationLayer)
-
id- ID of action. By this ID they will be compared for deletion and addition. -
animationStarter- animation, which will be played when action is started. -
animationLayer- layer, where animation will be played.
-
setDelayPredicate()determines in which moment of time (relatively to the playing animation, in milliseconds) task, which is set bysetOnCall()will be run. SeeStandardDelayPredicatesfor common predicates. Default:StandardDelayPredicates.onStart. -
setOnCall()- task, that will be run, when the predicate from.setDelayPredicate()will return true. Default:() -> {}.
So far, task from setOnCall can only be played once per animation.
TimeCore also has some built-in animated Entity AI Goals/Tasks, which you can use. They are located in ru.timeconqueror.timecore.animation.entityai package.
To run the action, use ActionManager.enableAction().
To remove the action use ActionManager.disableAction().
To check if the action with the same id is already enabled use ActionManager.isActionEnabled.