Skip to content

Commit

Permalink
customEntityAnimation
Browse files Browse the repository at this point in the history
  • Loading branch information
BeiShanair committed Feb 21, 2024
1 parent 5e26cba commit b98e59a
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.besson.tutorialmod.entity.animation;

import net.minecraft.client.render.entity.animation.Animation;
import net.minecraft.client.render.entity.animation.AnimationHelper;
import net.minecraft.client.render.entity.animation.Keyframe;
import net.minecraft.client.render.entity.animation.Transformation;

public class TigerAnimation {

public static final Animation IDLE = Animation.Builder.create(2f).looping()
.addBoneAnimation("head",
new Transformation(Transformation.Targets.TRANSLATE,
new Keyframe(0f, AnimationHelper.createTranslationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createTranslationalVector(0f, -0.5f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createTranslationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR)))
.addBoneAnimation("body",
new Transformation(Transformation.Targets.TRANSLATE,
new Keyframe(0f, AnimationHelper.createTranslationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createTranslationalVector(0f, -0.5f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createTranslationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR)))
.addBoneAnimation("tail",
new Transformation(Transformation.Targets.TRANSLATE,
new Keyframe(0f, AnimationHelper.createTranslationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createTranslationalVector(0f, -0.5f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createTranslationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR)))
.addBoneAnimation("tail",
new Transformation(Transformation.Targets.ROTATE,
new Keyframe(0f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(0.5f, AnimationHelper.createRotationalVector(0f, -20f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1.5f, AnimationHelper.createRotationalVector(0f, 20f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR))).build();
public static final Animation WALK = Animation.Builder.create(2f).looping()
.addBoneAnimation("left_front_leg",
new Transformation(Transformation.Targets.ROTATE,
new Keyframe(0f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(0.5f, AnimationHelper.createRotationalVector(-15f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1.5f, AnimationHelper.createRotationalVector(15f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR)))
.addBoneAnimation("right_front_leg",
new Transformation(Transformation.Targets.ROTATE,
new Keyframe(0f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(0.5f, AnimationHelper.createRotationalVector(15f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1.5f, AnimationHelper.createRotationalVector(-15f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR)))
.addBoneAnimation("left_back_leg",
new Transformation(Transformation.Targets.ROTATE,
new Keyframe(0f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(0.5f, AnimationHelper.createRotationalVector(15f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1.5f, AnimationHelper.createRotationalVector(-15f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR)))
.addBoneAnimation("right_back_leg",
new Transformation(Transformation.Targets.ROTATE,
new Keyframe(0f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(0.5f, AnimationHelper.createRotationalVector(-15f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1.5f, AnimationHelper.createRotationalVector(15f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR)))
.addBoneAnimation("tail",
new Transformation(Transformation.Targets.ROTATE,
new Keyframe(0f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(0.5f, AnimationHelper.createRotationalVector(0f, -20f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1.5f, AnimationHelper.createRotationalVector(0f, 20f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR))).build();
public static final Animation ATTACK = Animation.Builder.create(2f).looping()
.addBoneAnimation("head",
new Transformation(Transformation.Targets.ROTATE,
new Keyframe(0f, AnimationHelper.createRotationalVector(0f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1f, AnimationHelper.createRotationalVector(20f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(1.0416767f, AnimationHelper.createRotationalVector(-16.67f, 0f, 0f),
Transformation.Interpolations.LINEAR),
new Keyframe(2f, AnimationHelper.createRotationalVector(-0.83f, 0f, 0f),
Transformation.Interpolations.LINEAR))).build();
}
17 changes: 17 additions & 0 deletions src/main/java/com/besson/tutorialmod/entity/client/TigerModel.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.besson.tutorialmod.entity.client;

import com.besson.tutorialmod.entity.animation.TigerAnimation;
import com.besson.tutorialmod.entity.custom.TigerEntity;
import net.minecraft.client.model.*;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.entity.model.SinglePartEntityModel;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.MathHelper;

// Made with Blockbench 4.9.3
// Exported for Minecraft version 1.17+ for Yarn
// Paste this class into your mod and generate all required imports
public class TigerModel<T extends TigerEntity> extends SinglePartEntityModel<T> {
private final ModelPart tiger;
private final ModelPart head;
public TigerModel(ModelPart root) {
this.tiger = root.getChild("tiger");
this.head = tiger.getChild("head");
}
public static TexturedModelData getTexturedModelData() {
ModelData modelData = new ModelData();
Expand All @@ -38,7 +42,20 @@ public static TexturedModelData getTexturedModelData() {
}
@Override
public void setAngles(TigerEntity entity, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
this.getPart().traverse().forEach(ModelPart::resetTransform);
this.setHeadAngles(netHeadYaw,headPitch);

this.animateMovement(TigerAnimation.WALK,limbSwing,limbSwingAmount,2f,2.5f);
this.updateAnimation(entity.idleAnimationState,TigerAnimation.IDLE,ageInTicks,1f);
}

private void setHeadAngles(float headAngles, float headPitch) {
headAngles = MathHelper.clamp(headAngles,-30.0F,30.0F);
headPitch = MathHelper.clamp(headPitch, -25.0F, 45.0F);
this.head.yaw = headAngles * 0.017453292F;
this.head.pitch = headPitch * 0.017453292F;
}

@Override
public void render(MatrixStack matrices, VertexConsumer vertexConsumer, int light, int overlay, float red, float green, float blue, float alpha) {
tiger.render(matrices, vertexConsumer, light, overlay, red, green, blue, alpha);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.besson.tutorialmod.entity.custom;

import com.besson.tutorialmod.entity.ModEntities;
import net.minecraft.entity.AnimationState;
import net.minecraft.entity.EntityPose;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ai.goal.*;
import net.minecraft.entity.attribute.DefaultAttributeContainer;
Expand All @@ -17,13 +19,33 @@
import org.jetbrains.annotations.Nullable;

public class TigerEntity extends AnimalEntity {
public final AnimationState idleAnimationState = new AnimationState();
private int idleAnimationTimeOut = 0;

private void setUpAnimationState(){
if (this.idleAnimationTimeOut <= 0){
this.idleAnimationTimeOut = this.random.nextInt(40) + 80;
this.idleAnimationState.start(this.age);
}else {
--this.idleAnimationTimeOut;
}
}
public TigerEntity(EntityType<? extends AnimalEntity> entityType, World world) {
super(entityType, world);
}

@Override
public void tick() {
super.tick();
if (this.getWorld().isClient()){
setUpAnimationState();
}
}

@Override
protected void updateLimbs(float posDelta) {
float f = this.getPose() == EntityPose.STANDING ? Math.min(posDelta * 6.0f,1.0f) :0.0f;
this.limbAnimator.updateLimbs(f,0.2f);
}

@Override
Expand Down

0 comments on commit b98e59a

Please sign in to comment.