Skip to content

How to animate an entity

Timmypote edited this page Jun 16, 2017 · 2 revisions

We are going to see how to animate an entity with the CraftStutioAPI.


Requirements

You are going to need a simple entity ready to be animated.


The Entity Class

public class EntityTest extends EntityAnimal implements IAnimated
{
    protected static AnimationHandler animHandler = CraftStudioApi.getNewAnimationHandler(EntityTest.class);

    static {
        EntityTest.animHandler.addAnim(Mod_Test.MODID, "your_animation", "your_model", false);
    }

    public EntityTest2(World par1World) {
        super(par1World);
        EntityTest.animHandler.addAnimated(this);
    }

    @Override
    public AnimationHandler getAnimationHandler() {
        return EntityTest.animHandler;
    }

    @Override
    public UUID getUUID() {
        return this.getPersistentID();
    }
}

You'll need to implement the IAnimated interface to your entity class. And so the two functions getAnimationHandler() and getUUID()

Then create a static variable of type AnimationHandler, name it as you wish, animHandler for example, and set it with the getNewAnimationHandler() function from the CraftStudioApi class, with YourEntityClass.class, the class of the entities that will be animated, as the argument.

You should have something like this:

protected static AnimationHandler animHandler = CraftStudioApi.getNewAnimationHandler(EntityTest.class);

Don't forget to return this variable with the getAnimationHandler() function.

    @Override
    public AnimationHandler getAnimationHandler() {
        return EntityTest.animHandler;
    }

We now need to define the UUID of the IAnimated by getting the one of the entity:

    @Override
    public UUID getUUID() {
        return this.getPersistentID();
    }

We have everything needed to register and start animations with our entity.


Adding animations to the entity is done through a static block:

You just need to call the AnimationHandler and use its addAnim() method. This method has different variant, the JavDoc can help you with them!

The addAnim() methods always start with the ID of your mod (modid)

EntityTest.animHandler.addAnim(Mod_Test.MODID, "your_animation", "model_to_animate", false);

The rest of the parameters are:

"modid" is the ID of your mod.
"your_animation" the name of the registered animation that you want to add.
"model_to_animate" the name of the registered model that correspond to your animation.
false is a boolean to know if your animation should loop or not, in that case it won't (false)


You can also to add a reverse animation to your entity.

EntityTest.animHandler.addAnim(Mod_Test.MODID, "new_animation", "animation_to_reverse");

For example, the second parameters is the name of the new animation, which will be the other animation backward.

EntityTest.animHandler.addAnim(Mod_Test.MODID, "close_door", "open_door");

For this example, I created the animation "close_door" which is the registered animation "open_door" played backward.


You have also the possibility to add an hard-coded animation, which mean that you write the animation directly in the code !

For example, if you want to add an animation were the entity look around.

You are lucky because we've donne it for you and the animation is in the animation pack!

EntityTest.animHandler.addAnim(Mod_Test.MODID, "lootAt", "model_to_animate", new AnimationLootAt(this, "Head"));

We use the process as before, we give the modid, the name of the animation, the model that must be animate and finaly a new instance of our custom animation: new AnimationLookAt("Head")
"Head" is the name of the block that reprensent the head of your model.

If you want more information, you can check the JavaDoc


The last part is to launch the animation on your entity !

You just have to call the startAnimation() method of the AnimationHandler, for example:

this.getAnimationHandler().startAnimation(Mod_Test.MODID, "my_registered_animation");

The call can be donne wherever you want !

You could put it in the onUpdate() methods if you want your animation to be always running or to launch in special situations, or in the processInteract(EntityPlayer player, EnumHand hand) to launch an animation when the players interact with your entity.
The sky is (almost) the only limit !

You could also need this method:

if(this.getAnimationHandler().isAnimationActive(Mod_Test.MODID, "my_registered_animation")) {}

To know if an animation is currently running.

Or:

this.getAnimationHandler().stopAnimation(Mod_Test.MODID, "my_registered_animation");

To stop a running animation.


That's the end of this serie of tutorial to animate an entity with the CraftStudioAPI ! Enjoy !

Clone this wiki locally