Created
May 11, 2017 22:41
-
-
Save AL1L/dbe6e5825c02db5462fbc1e701de1c10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.diamonddev.craftoblo.animation; | |
import me.diamonddev.craftoblo.Craftoblo; | |
import org.bukkit.entity.ArmorStand; | |
import org.bukkit.scheduler.BukkitRunnable; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class AnimationPlayer { | |
private final boolean haltPlayer = false; | |
private ArmorStand stand = null; | |
private Animation animation = null; | |
private AnimationType animationType = AnimationType.DEFAULT_RUN; | |
private List<Frame> frames = new ArrayList<>(); | |
private boolean playing = false; | |
public AnimationPlayer(AnimationType type, ArmorStand stand) { | |
this.stand = stand; | |
this.animationType = type; | |
this.animation = AnimationProssesor.getAnimation(type); | |
this.frames = this.animation.getAllFrames(); | |
} | |
public AnimationPlayer(Animation animation, AnimationType type, ArmorStand stand) { | |
this.stand = stand; | |
this.animationType = type; | |
this.animation = animation; | |
this.frames = animation.getAllFrames(); | |
} | |
public void play(final int times) { | |
if (haltPlayer) | |
return; | |
this.playing = true; | |
new BukkitRunnable() { | |
int currentFrame = 1; | |
int ran = 0; | |
public void run() { | |
if (playing && ran <= times) { | |
playFrame(currentFrame); | |
currentFrame++; | |
if (currentFrame > frames.size()) { | |
currentFrame = 1; | |
ran++; | |
} | |
} else { | |
this.cancel(); | |
} | |
} | |
}.runTaskTimer(Craftoblo.gi(), 0, 3); | |
} | |
public void playFrame(int frameNum) { | |
if (haltPlayer) | |
return; | |
stand.teleport(stand); | |
Frame frame = frames.get(frameNum - 1); | |
if (frame.getHead() != null) | |
stand.setHeadPose(frame.getHead()); | |
if (frame.getBody() != null) | |
stand.setBodyPose(frame.getBody()); | |
if (frame.getLeftArm() != null) | |
stand.setLeftArmPose(frame.getLeftArm()); | |
if (frame.getRightArm() != null) | |
stand.setRightArmPose(frame.getRightArm()); | |
if (frame.getLeftLeg() != null) | |
stand.setLeftLegPose(frame.getLeftLeg()); | |
if (frame.getRightLeg() != null) | |
stand.setRightLegPose(frame.getRightLeg()); | |
} | |
public void playLoop() { | |
if (haltPlayer) | |
return; | |
this.playing = true; | |
new BukkitRunnable() { | |
int currentFrame = 1; | |
public void run() { | |
if (playing) { | |
playFrame(currentFrame); | |
currentFrame++; | |
if (currentFrame > frames.size()) { | |
currentFrame = 1; | |
} | |
} else { | |
this.cancel(); | |
} | |
} | |
}.runTaskTimer(Craftoblo.gi(), 0, 3); | |
} | |
public void stop() { | |
if (haltPlayer) | |
return; | |
this.playing = false; | |
} | |
public AnimationType getAnimationType() { | |
return this.animationType; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment