2023-01-12 19:32:40 -05:00
|
|
|
package noppes.npcs.controllers.data;
|
|
|
|
|
|
2024-03-24 17:03:58 -05:00
|
|
|
import cpw.mods.fml.common.FMLCommonHandler;
|
2023-02-06 23:17:05 -06:00
|
|
|
import cpw.mods.fml.relauncher.Side;
|
|
|
|
|
import cpw.mods.fml.relauncher.SideOnly;
|
2023-01-12 20:34:07 -05:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2024-03-24 17:03:58 -05:00
|
|
|
import noppes.npcs.EventHooks;
|
2023-01-16 13:53:15 -06:00
|
|
|
import noppes.npcs.api.handler.data.IAnimation;
|
2024-03-24 17:03:58 -05:00
|
|
|
import noppes.npcs.api.handler.data.IAnimationData;
|
2023-01-16 13:53:15 -06:00
|
|
|
import noppes.npcs.api.handler.data.IFrame;
|
|
|
|
|
import noppes.npcs.constants.EnumAnimationPart;
|
2023-01-26 14:14:13 -06:00
|
|
|
import noppes.npcs.controllers.AnimationController;
|
2023-01-12 20:34:07 -05:00
|
|
|
|
2023-01-12 19:32:40 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public class Animation implements IAnimation {
|
2023-12-22 17:25:40 -06:00
|
|
|
public AnimationData parent; //Client-sided only
|
2023-11-05 21:28:49 -05:00
|
|
|
public int id = -1; // Only for internal usage
|
2023-11-01 21:39:08 -04:00
|
|
|
|
2023-02-06 23:17:05 -06:00
|
|
|
public ArrayList<Frame> frames = new ArrayList<>();
|
2023-01-16 13:53:15 -06:00
|
|
|
public int currentFrame = 0;
|
|
|
|
|
public int currentFrameTime = 0;
|
|
|
|
|
|
2023-11-01 21:39:08 -04:00
|
|
|
public String name = "";
|
2023-01-12 19:32:40 -05:00
|
|
|
public float speed = 1.0F;
|
2023-01-16 13:53:15 -06:00
|
|
|
public byte smooth = 0;
|
2023-01-25 22:05:11 -06:00
|
|
|
public int loop = -1; //If greater than 0 and less than the amount of frames, the animation will begin looping when it reaches this frame.
|
2024-03-23 23:34:01 -05:00
|
|
|
public int tickDuration = 50;
|
2023-01-16 13:53:15 -06:00
|
|
|
|
|
|
|
|
public boolean whileStanding = true;
|
|
|
|
|
public boolean whileAttacking = true;
|
|
|
|
|
public boolean whileMoving = true;
|
2023-01-12 19:32:40 -05:00
|
|
|
|
2023-02-06 23:17:05 -06:00
|
|
|
//Client-sided
|
|
|
|
|
public boolean paused;
|
|
|
|
|
|
2023-01-12 19:40:06 -05:00
|
|
|
public Animation(){}
|
|
|
|
|
|
2023-11-01 21:39:08 -04:00
|
|
|
public Animation(int id, String name){
|
2023-01-12 19:32:40 -05:00
|
|
|
this.name = name;
|
2023-11-01 21:39:08 -04:00
|
|
|
this.id = id;
|
2023-01-12 19:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 21:39:08 -04:00
|
|
|
public Animation(int id, String name, float speed, byte smooth){
|
2023-01-12 19:32:40 -05:00
|
|
|
this.name = name;
|
|
|
|
|
this.speed = speed;
|
|
|
|
|
this.smooth = smooth;
|
2023-11-01 21:39:08 -04:00
|
|
|
|
|
|
|
|
this.id = id;
|
2023-01-12 19:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
2024-03-24 17:03:58 -05:00
|
|
|
public IAnimationData getParent() {
|
|
|
|
|
return this.parent;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public IFrame currentFrame() {
|
|
|
|
|
return currentFrame < frames.size() ? frames.get(currentFrame) : null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IFrame[] getFrames() {
|
|
|
|
|
return frames.toArray(new Frame[0]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation setFrames(IFrame[] frames) {
|
|
|
|
|
this.clearFrames();
|
|
|
|
|
for (IFrame frame : frames) {
|
|
|
|
|
this.frames.add((Frame) frame);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation clearFrames() {
|
|
|
|
|
this.frames.clear();
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation addFrame(IFrame frame) {
|
|
|
|
|
this.frames.add((Frame) frame);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation addFrame(int index, IFrame frame) {
|
|
|
|
|
this.frames.add(index, (Frame) frame);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation removeFrame(IFrame frame) {
|
|
|
|
|
this.frames.remove((Frame) frame);
|
|
|
|
|
return this;
|
2023-01-12 19:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public IAnimation setName(String name) {
|
|
|
|
|
this.name = name;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 19:32:40 -05:00
|
|
|
public String getName() {
|
2023-01-16 13:53:15 -06:00
|
|
|
return this.name;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-23 23:34:01 -05:00
|
|
|
public int tickDuration() {
|
|
|
|
|
return this.tickDuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation setTickDuration(int tickDuration) {
|
|
|
|
|
this.tickDuration = tickDuration;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public IAnimation setSpeed(float speed) {
|
|
|
|
|
this.speed = speed;
|
|
|
|
|
return this;
|
2023-01-12 19:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getSpeed() {
|
2023-01-16 13:53:15 -06:00
|
|
|
return this.speed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation setSmooth(byte smooth) {
|
|
|
|
|
this.smooth = smooth;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte isSmooth() {
|
|
|
|
|
return this.smooth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation doWhileStanding(boolean whileStanding) {
|
|
|
|
|
this.whileStanding = whileStanding;
|
|
|
|
|
return this;
|
2023-01-12 19:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public boolean doWhileStanding() {
|
|
|
|
|
return this.whileStanding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation doWhileMoving(boolean whileMoving) {
|
|
|
|
|
this.whileMoving = whileMoving;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean doWhileMoving() {
|
|
|
|
|
return this.whileMoving;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IAnimation doWhileAttacking(boolean whileAttacking) {
|
|
|
|
|
this.whileAttacking = whileAttacking;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean doWhileAttacking() {
|
|
|
|
|
return this.whileAttacking;
|
2023-01-12 19:32:40 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-25 22:05:11 -06:00
|
|
|
public IAnimation setLoop(int loopAtFrame) {
|
|
|
|
|
this.loop = loopAtFrame;
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int loop() {
|
|
|
|
|
return this.loop;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 14:14:13 -06:00
|
|
|
public IAnimation save() {
|
2023-06-05 22:12:48 -04:00
|
|
|
return AnimationController.Instance.saveAnimation(this);
|
2023-01-26 14:14:13 -06:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 21:39:08 -04:00
|
|
|
@Override
|
|
|
|
|
public int getID() {
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setID(int newID) {
|
|
|
|
|
id = newID;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 20:34:07 -05:00
|
|
|
public void readFromNBT(NBTTagCompound compound){
|
2023-11-06 20:53:21 -05:00
|
|
|
if(compound.hasKey("ID")){
|
|
|
|
|
id = compound.getInteger("ID");
|
|
|
|
|
}
|
2024-03-08 00:28:55 -06:00
|
|
|
else if (AnimationController.Instance != null) {
|
2023-11-06 20:53:21 -05:00
|
|
|
id = AnimationController.Instance.getUnusedId();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 20:34:07 -05:00
|
|
|
name = compound.getString("Name");
|
|
|
|
|
speed = compound.getFloat("Speed");
|
2023-01-16 13:53:15 -06:00
|
|
|
smooth = compound.getByte("Smooth");
|
2023-01-25 22:05:11 -06:00
|
|
|
loop = compound.getInteger("Loop");
|
2023-01-16 13:53:15 -06:00
|
|
|
|
2024-03-24 03:56:14 -05:00
|
|
|
if (compound.hasKey("TickDuration")) {
|
|
|
|
|
this.tickDuration = compound.getInteger("TickDuration");
|
|
|
|
|
} else if(compound.hasKey("RenderTicks")){
|
|
|
|
|
this.tickDuration = compound.getBoolean("RenderTicks") ? 20 : 50;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 20:34:07 -05:00
|
|
|
ArrayList<Frame> frames = new ArrayList<Frame>();
|
|
|
|
|
NBTTagList list = compound.getTagList("Frames", 10);
|
|
|
|
|
for (int i = 0; i < list.tagCount(); i++) {
|
|
|
|
|
NBTTagCompound item = list.getCompoundTagAt(i);
|
|
|
|
|
Frame frame = new Frame();
|
2023-01-16 13:53:15 -06:00
|
|
|
frame.parent = this;
|
2023-01-12 20:34:07 -05:00
|
|
|
frame.readFromNBT(item);
|
|
|
|
|
frames.add(frame);
|
|
|
|
|
}
|
|
|
|
|
this.frames = frames;
|
2023-01-16 13:53:15 -06:00
|
|
|
|
|
|
|
|
this.whileStanding = compound.getBoolean("WhileStanding");
|
|
|
|
|
this.whileMoving = compound.getBoolean("WhileWalking");
|
|
|
|
|
this.whileAttacking = compound.getBoolean("WhileAttacking");
|
2023-01-12 20:34:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public NBTTagCompound writeToNBT(){
|
|
|
|
|
NBTTagCompound compound = new NBTTagCompound();
|
2023-11-01 21:39:08 -04:00
|
|
|
compound.setInteger("ID", id);
|
2023-01-12 20:34:07 -05:00
|
|
|
compound.setString("Name", name);
|
|
|
|
|
compound.setFloat("Speed", speed);
|
2023-01-16 13:53:15 -06:00
|
|
|
compound.setByte("Smooth", smooth);
|
2023-01-25 22:05:11 -06:00
|
|
|
compound.setInteger("Loop",loop);
|
2024-03-24 03:56:14 -05:00
|
|
|
compound.setInteger("TickDuration", tickDuration);
|
2023-01-16 13:53:15 -06:00
|
|
|
|
2023-01-12 20:34:07 -05:00
|
|
|
NBTTagList list = new NBTTagList();
|
|
|
|
|
for(Frame frame : frames){
|
|
|
|
|
NBTTagCompound item = frame.writeToNBT();
|
|
|
|
|
list.appendTag(item);
|
|
|
|
|
}
|
|
|
|
|
compound.setTag("Frames", list);
|
2023-01-16 13:53:15 -06:00
|
|
|
|
|
|
|
|
compound.setBoolean("WhileStanding", whileStanding);
|
|
|
|
|
compound.setBoolean("WhileWalking", whileMoving);
|
|
|
|
|
compound.setBoolean("WhileAttacking", whileAttacking);
|
2023-01-12 20:34:07 -05:00
|
|
|
return compound;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
public void increaseTime() {
|
2023-02-06 23:17:05 -06:00
|
|
|
if (paused)
|
|
|
|
|
return;
|
|
|
|
|
|
2023-12-22 17:25:40 -06:00
|
|
|
if (this.parent != null && this.currentFrame < this.frames.size()) {
|
|
|
|
|
this.parent.finishedFrame = this.currentFrame;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
this.currentFrameTime++;
|
|
|
|
|
if (this.currentFrameTime == this.currentFrame().getDuration()) {
|
2024-03-24 17:03:58 -05:00
|
|
|
EventHooks.onAnimationFrameExited(this, this.currentFrame());
|
|
|
|
|
|
2023-01-16 13:53:15 -06:00
|
|
|
Frame prevFrame = (Frame) this.currentFrame();
|
2023-01-25 22:05:11 -06:00
|
|
|
Frame nextFrame = null;
|
2023-01-16 13:53:15 -06:00
|
|
|
this.currentFrameTime = 0;
|
|
|
|
|
this.currentFrame++;
|
|
|
|
|
if (this.currentFrame < this.frames.size()) {
|
2023-01-25 22:05:11 -06:00
|
|
|
nextFrame = this.frames.get(this.currentFrame);
|
|
|
|
|
} else if (this.loop >= 0 && this.loop < this.frames.size()) {
|
|
|
|
|
this.currentFrame = this.loop;
|
|
|
|
|
}
|
2024-03-24 17:03:58 -05:00
|
|
|
|
|
|
|
|
if (this.currentFrame() != null) {
|
|
|
|
|
EventHooks.onAnimationFrameEntered(this, this.currentFrame());
|
|
|
|
|
} else {
|
|
|
|
|
EventHooks.onAnimationEnded(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 22:05:11 -06:00
|
|
|
if (nextFrame != null) {
|
2023-01-16 13:53:15 -06:00
|
|
|
for (EnumAnimationPart part : EnumAnimationPart.values()) {
|
|
|
|
|
if (prevFrame.frameParts.containsKey(part) && nextFrame.frameParts.containsKey(part)) {
|
|
|
|
|
nextFrame.frameParts.get(part).prevRotations = prevFrame.frameParts.get(part).prevRotations;
|
|
|
|
|
nextFrame.frameParts.get(part).prevPivots = prevFrame.frameParts.get(part).prevPivots;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-22 17:25:40 -06:00
|
|
|
} else if (this.parent != null && this.currentFrame > this.loop) {
|
|
|
|
|
this.parent.finishedTime = this.parent.animationEntity.getAge();
|
2023-01-16 13:53:15 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-09 23:07:52 -06:00
|
|
|
|
|
|
|
|
@SideOnly(Side.CLIENT)
|
2023-11-10 07:58:15 -06:00
|
|
|
public void jumpToCurrentFrame() {
|
2024-03-24 18:59:27 -05:00
|
|
|
this.currentFrameTime = 0;
|
|
|
|
|
Frame frame = (Frame) this.currentFrame();
|
|
|
|
|
if(frame != null){
|
|
|
|
|
for (EnumAnimationPart part : EnumAnimationPart.values()) {
|
|
|
|
|
if(part != null){
|
|
|
|
|
if (frame.frameParts.containsKey(part)) {
|
|
|
|
|
frame.frameParts.get(part).jumpToCurrentFrame();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-09 23:07:52 -06:00
|
|
|
}
|
2023-01-12 19:32:40 -05:00
|
|
|
}
|