Last active
October 22, 2022 02:30
-
-
Save hugmanrique/1bd273e3b20b419f625c437db61b7ff9 to your computer and use it in GitHub Desktop.
Small ArmorStand animation created with the Bukkit API
This file contains hidden or 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
public class SwordAnimation { | |
// Config variables | |
private static final float SEPARATOR = 2; | |
private static final float RAD_PER_SEC = 1.5F; | |
private static final float RAD_PER_TICK = RAD_PER_SEC / 20F; | |
private Location center; | |
private double radius; | |
private List<ArmorStand> swords; | |
public SwordAnimation(Location center, double radius) { | |
this.center = center; | |
this.radius = radius; | |
swords = Lists.newArrayList(); | |
} | |
public void start(JavaPlugin plugin) { | |
for (double angle = 0; angle < Math.PI * 2; angle += SEPARATOR) { | |
spawnStand(angle); | |
} | |
new BukkitRunnable(){ | |
int tick = 0; | |
public void run() { | |
++tick; | |
for (int i = 0; i < swords.size(); i++) { | |
ArmorStand stand = swords.get(i); | |
Location loc = getLocationInCircle(RAD_PER_TICK * tick + SEPARATOR * i); | |
// Entity falling bug | |
stand.setVelocity(new Vector(1, 0, 0)); | |
stand.teleport(loc); | |
} | |
} | |
}.runTaskTimer(plugin, 0L, 1L); | |
} | |
private void spawnStand(double angle) { | |
Location loc = getLocationInCircle(angle); | |
ArmorStand stand = loc.getWorld().spawn(loc, ArmorStand.class); | |
// TODO Customize by hiding, adding items, rotation... | |
swords.add(stand); | |
} | |
private Location getLocationInCircle(double angle) { | |
double x = center.getX() + radius * Math.cos(angle); | |
double z = center.getZ() + radius * Math.sin(angle); | |
return new Location(center.getWorld(), x, center.getY(), z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Considering the context of this gist, I don't know whether "between" means (1) the location whose distance to two other locations is equal, or, (2) given two angles in a circle, the location in the circle whose argument is the mean of both angles.
For (1), we can compute the arithmetic mean of both locations (say
a, b
) asNote that this method doesn't account for overflow; this is unlikely to happen in a Minecraft world and is left as an exercise.
For (2), given the center
c
and the two locationsa, b
, find the midpoint as above, obtain the segment from the center to the midpoint, and scale it by theradius
of the circle:This method throws if the two points are antipodal (the normalize call divides by zero).
I wrote this on the fly and it isn't optimized, but it should work fine. If performance is a huge concern, avoid
Location
andVector
objects and handle the math ondouble
s yourself.