Last active
January 14, 2017 04:40
-
-
Save hugmanrique/e79916c79140b4601c9d638d43651da6 to your computer and use it in GitHub Desktop.
Bukkit API to create smooth cinematics
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 Cinematic { | |
private JavaPlugin plugin; | |
private World world; | |
private List<CinematicView> views; | |
public Cinematic(JavaPlugin plugin, List<CinematicView> views, World world) { | |
this.plugin = plugin; | |
this.views = views; | |
this.world = world; | |
} | |
// Getters here | |
} |
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 CinematicTask implements Runnable { | |
private JavaPlugin plugin; | |
private Player player; | |
private Cinematic cinematic; | |
private int delay = 1; | |
private float timer; | |
private int index; | |
private CinematicView current; | |
private CinematicView next; | |
private Vector pos1; | |
private Vector pos2; | |
private Vector nextPos; | |
private int taskId; | |
public CinematicTask(JavaPlugin plugin, Player player, Cinematic cinematic) { | |
this.plugin = plugin; | |
this.player = player; | |
this.cinematic = cinematic; | |
this.nextPos = new Vector(); | |
} | |
public void start() { | |
this.index = 0; | |
loadView(); | |
Location loc = player.getLocation(); | |
loc.setX(pos1.getX()); | |
loc.setY(pos1.getY()); | |
loc.setZ(pos1.getZ()); | |
loc.setWorld(cinematic.getWorld()); | |
player.teleport(loc); | |
player.setFlySpeed(0F); | |
player.setGameMode(GameMode.SPECTATOR); | |
this.timer = 0; | |
// run() every tick | |
taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this, 0L, 0L); | |
} | |
public void run() { | |
if (!player.isOnline()) { | |
cancel(); | |
return; | |
} else { | |
player.setGameMode(GameMode.SPECTATOR); | |
} | |
player.closeInventory(); | |
timer += current.getSpeed() * delay; | |
lerpNext(); | |
Vector playerPos = player.getLocation().toVector(); | |
float distance = (float) nextPos.distance(playerPos); | |
nextPos.subtract(playerPos).normalize().multiply(distance * current.getSpeed()); | |
player.setVelocity(nextPos); | |
// Arrived at view | |
if (timer >= 1) { | |
player.setFlySpeed(0); | |
timer--; | |
if (++index >= cinematic.getViews().size() - 1) { | |
exit(); | |
return; | |
} | |
loadView(); | |
} | |
} | |
private void loadView() { | |
current = getView(index); | |
next = getView(index + 1); | |
pos1 = current.getPosition(); | |
pos2 = next.getPosition(); | |
} | |
// Utility methods | |
public CinematicView getView(int index) { | |
return cinematic.getViews().get(index); | |
} | |
public void lerpNext() { | |
nextPos.setX(lerp(pos1.getX(), pos2.getX(), timer)); | |
nextPos.setY(lerp(pos1.getY(), pos2.getY(), timer)); | |
nextPos.setZ(lerp(pos1.getZ(), pos2.getZ(), timer)); | |
} | |
private static float lerp(double pos1, double pos2, float timer) { | |
return (float) (pos1 + timer * (pos2 - pos1)); | |
} | |
private void cancel() { | |
Bukkit.getScheduler().cancelTask(taskId); | |
} | |
public void exit() { | |
cancel(); | |
player.setFlySpeed(0.1F); | |
player.setGameMode(GameMode.ADVENTURE); | |
} | |
} |
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 CinematicView { | |
private Vector position; | |
private float speed = 0.01F; | |
public CinematicView(Location location) { | |
this(location.toVector()); | |
} | |
public CinematicView(Vector position) { | |
this.position = position; | |
} | |
// Getters here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment