4 New Animations

RowAnimation
ColumnAnimation
HorizontalSnakeAnimation
VerticalSnakeAnimation

Split BaseAnimation to BaseAnimation and SoundAnimation, all default Animations now extend SoundAnimation
This commit is contained in:
NichtStudioCode 2021-01-27 19:29:09 +01:00
parent d0138f476e
commit 83f1da5ed8
9 changed files with 166 additions and 8 deletions

@ -2,8 +2,8 @@ package de.studiocode.invgui.animation.impl;
import de.studiocode.invgui.InvGui;
import de.studiocode.invgui.animation.Animation;
import de.studiocode.invgui.util.SlotUtils;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import org.jetbrains.annotations.NotNull;
@ -26,11 +26,8 @@ public abstract class BaseAnimation implements Animation {
private BukkitTask task;
private int frame;
public BaseAnimation(int tickDelay, boolean sound) {
public BaseAnimation(int tickDelay) {
this.tickDelay = tickDelay;
if (sound) addShowHandler((frame, index) -> getPlayer().playSound(getPlayer().getLocation(),
Sound.ENTITY_ITEM_PICKUP, 1, 1));
}
protected abstract void handleFrame(int frame);
@ -78,6 +75,13 @@ public abstract class BaseAnimation implements Animation {
}, 0, tickDelay);
}
protected int convToIndex(int x, int y) {
if (x >= width || y >= height)
throw new IllegalArgumentException("Coordinates out of bounds");
return SlotUtils.convertToIndex(x, y, width);
}
public void cancel() {
task.cancel();
}

@ -0,0 +1,30 @@
package de.studiocode.invgui.animation.impl;
public class ColumnAnimation extends SoundAnimation {
private int column;
public ColumnAnimation(int tickDelay, boolean sound) {
super(tickDelay, sound);
}
@Override
protected void handleFrame(int frame) {
boolean showedSomething = false;
while (!showedSomething || column == getWidth() - 1) {
for (int y = 0; y != getHeight(); y++) {
int index = convToIndex(column, y);
if (getSlots().contains(index)) {
show(index);
showedSomething = true;
}
}
column++;
}
if (column == getHeight() - 1) finish();
}
}

@ -0,0 +1,40 @@
package de.studiocode.invgui.animation.impl;
public class HorizontalSnakeAnimation extends SoundAnimation {
private int x;
private int y;
private boolean left;
public HorizontalSnakeAnimation(int tickDelay, boolean sound) {
super(tickDelay, sound);
}
@Override
protected void handleFrame(int frame) {
boolean slotShown = false;
while (!slotShown) {
int slotIndex = convToIndex(x, y);
if (slotShown = getSlots().contains(slotIndex)) show(slotIndex);
if (left) {
if (x <= 0) {
y++;
x = 0;
left = false;
} else x--;
} else {
if (x >= getWidth() - 1) {
y++;
left = true;
} else x++;
}
if (y >= getHeight()) {
finish();
return;
}
}
}
}

@ -8,7 +8,7 @@ import java.util.Random;
/**
* Lets the {@link Item}s pop up randomly.
*/
public class RandomAnimation extends BaseAnimation {
public class RandomAnimation extends SoundAnimation {
private final Random random = new Random();

@ -0,0 +1,30 @@
package de.studiocode.invgui.animation.impl;
public class RowAnimation extends SoundAnimation {
private int row;
public RowAnimation(int tickDelay, boolean sound) {
super(tickDelay, sound);
}
@Override
protected void handleFrame(int frame) {
boolean showedSomething = false;
while (!showedSomething || row == getHeight() - 1) {
for (int x = 0; x != getWidth(); x++) {
int index = convToIndex(x, row);
if (getSlots().contains(index)) {
show(index);
showedSomething = true;
}
}
row++;
}
if (frame == getHeight() - 1) finish();
}
}

@ -7,7 +7,7 @@ import java.util.List;
/**
* Lets the {@link Item}s pop up index after index.
*/
public class SequentialAnimation extends BaseAnimation {
public class SequentialAnimation extends SoundAnimation {
public SequentialAnimation(int tickDelay, boolean sound) {
super(tickDelay, sound);

@ -0,0 +1,14 @@
package de.studiocode.invgui.animation.impl;
import org.bukkit.Sound;
public abstract class SoundAnimation extends BaseAnimation {
public SoundAnimation(int tickDelay, boolean sound) {
super(tickDelay);
if (sound) addShowHandler((frame, index) -> getPlayer().playSound(getPlayer().getLocation(),
Sound.ENTITY_ITEM_PICKUP, 1, 1));
}
}

@ -2,7 +2,7 @@ package de.studiocode.invgui.animation.impl;
import java.util.List;
public class SplitSequentialAnimation extends BaseAnimation {
public class SplitSequentialAnimation extends SoundAnimation {
public SplitSequentialAnimation(int tickDelay, boolean sound) {
super(tickDelay, sound);

@ -0,0 +1,40 @@
package de.studiocode.invgui.animation.impl;
public class VerticalSnakeAnimation extends SoundAnimation {
private int x;
private int y;
private boolean up;
public VerticalSnakeAnimation(int tickDelay, boolean sound) {
super(tickDelay, sound);
}
@Override
protected void handleFrame(int frame) {
boolean slotShown = false;
while (!slotShown) {
int slotIndex = convToIndex(x, y);
if (slotShown = getSlots().contains(slotIndex)) show(slotIndex);
if (up) {
if (y <= 0) {
x++;
y = 0;
up = false;
} else y--;
} else {
if (y >= getHeight() - 1) {
x++;
up = true;
} else y++;
}
if (x >= getWidth()) {
finish();
return;
}
}
}
}