feat: bezier courbe par courbe

This commit is contained in:
gdamms 2022-05-14 16:21:50 +02:00
parent 8fd4b6c144
commit fd2a06524e
3 changed files with 133 additions and 33 deletions

View file

@ -7,10 +7,19 @@ import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.inventory.ItemStack;
public class Camera extends JavaPlugin {
public class Camera extends JavaPlugin implements Listener {
public static ArrayList<Location> curve;
public static List<Location> controlPoints;
@ -34,23 +43,49 @@ public class Camera extends JavaPlugin {
this.getCommand("reset").setExecutor(new ClearPoints());
this.getCommand("exec").setExecutor(new ExecuteTraveling());
this.getCommand("show").setExecutor(new ShowCurve());
// Eventhandlers
getServer().getPluginManager().registerEvents(this, this);
}
public static void compute() {
int nControlPoints = controlPoints.size();
curve = new ArrayList<>();
for (float t = 0; t < 1; t += dt) {
ArrayList<Location> P = new ArrayList<>(Camera.controlPoints);
if (nControlPoints <= 4) {
for (float t = 0; t < 1; t += dt) {
ArrayList<Location> P = new ArrayList<>(controlPoints);
int N = P.size();
for (int k = N - 1; k > 0; k--) {
for (int i = 0; i < k; i++) {
P.set(i, P.get(i).clone().multiply(1 - t)
.add(P.get(i + 1).clone().multiply(t)));
int N = P.size();
for (int k = N - 1; k > 0; k--) {
for (int i = 0; i < k; i++) {
P.set(i, P.get(i).clone().multiply(1 - t)
.add(P.get(i + 1).clone().multiply(t)));
}
}
curve.add(P.get(0));
}
} else {
int nBezier = (nControlPoints - 1) / 3;
for (int iBezier = 0; iBezier < nBezier; iBezier++) {
for (float t = 0; t < 1; t += dt) {
ArrayList<Location> P = new ArrayList<>();
for (int k = 0; k < 4; k++) {
P.add(controlPoints.get(3 * iBezier + k).clone());
}
int N = P.size();
for (int k = N - 1; k > 0; k--) {
for (int i = 0; i < k; i++) {
P.set(i, P.get(i).clone().multiply(1 - t)
.add(P.get(i + 1).clone().multiply(t)));
}
}
curve.add(P.get(0));
}
}
curve.add(P.get(0));
}
}
@ -69,4 +104,30 @@ public class Camera extends JavaPlugin {
public void onDisable() {
ClearPoints.clear();
}
@EventHandler
public void onInteract(PlayerInteractEvent event) {
Action action = event.getAction();
Player player = event.getPlayer();
if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
ItemStack helditem = player.getPlayer().getInventory().getItemInMainHand();
switch (helditem.getType()) {
case STICK:
player.chat("/point add");
break;
case PAPER:
player.chat("/show");
break;
case SLIME_BALL:
player.chat("/exec");
break;
case BRICK:
player.chat("/reset");
break;
default:
break;
}
}
}
}

View file

@ -21,12 +21,15 @@ public class ClearPoints implements CommandExecutor {
}
public static void clear() {
Camera.broadlog("Clearing points:");
Camera.broadlog("Clearing points and path:");
ListPoints.listPoints();
Camera.controlPoints = new ArrayList<>();
Camera.broadlog("Points cleared !");
Camera.curve = new ArrayList<>();
Camera.broadlog("Path cleared !");
for (World world : Bukkit.getWorlds()) {
for (Entity e : world.getEntities()) {
if (e.getScoreboardTags().contains("controlPoint")) {

View file

@ -1,6 +1,7 @@
package com.tocard.cam;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -8,9 +9,9 @@ import org.bukkit.entity.Player;
public class NewPoint implements CommandExecutor {
private Location location;
private Player player;
private int index;
private static Location location;
private static Player player;
private static int index;
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
@ -19,31 +20,16 @@ public class NewPoint implements CommandExecutor {
player = (Player) sender;
switch (args[0]) {
case "add":
location = player.getLocation();
Camera.controlPoints.add(location);
Camera.broadlog("Point added: " + Camera.prettyLocation(location));
ShowCurve.add(location, player.getWorld());
add(args);
break;
case "ins":
index = Integer.parseInt(args[1]);
location = player.getLocation();
Camera.controlPoints.add(index, location);
Camera.broadlog("Point added: " + Camera.prettyLocation(location));
ShowCurve.insert(index, location, player.getWorld());
ins(args);
break;
case "rm":
index = Integer.parseInt(args[1]);
location = Camera.controlPoints.get(index);
Camera.controlPoints.remove(location);
Camera.broadlog("Point deleted: " + Camera.prettyLocation(location));
ShowCurve.rm(index);
rm(args);
break;
case "set":
index = Integer.parseInt(args[1]);
location = player.getLocation();
Camera.controlPoints.set(index, location);
Camera.broadlog("Point n°" + index + " set: " + Camera.prettyLocation(location));
ShowCurve.set(index, location, player.getWorld());
set(args);
break;
default:
break;
@ -59,4 +45,54 @@ public class NewPoint implements CommandExecutor {
}
return true;
}
public static void addPoint(Location point, World world) {
Camera.controlPoints.add(point);
Camera.broadlog("Point added: " + Camera.prettyLocation(point));
ShowCurve.add(point, world);
}
public static void add(String[] args) {
World world = player.getWorld();
location = player.getLocation();
if (Camera.controlPoints.size() < 4) {
addPoint(location, world);
} else {
int n = Camera.controlPoints.size();
Location P2 = Camera.controlPoints.get(n - 2);
Location P3 = Camera.controlPoints.get(n - 1);
Location P4 = P3.clone().multiply(2).subtract(P2);
Location P5 = location.clone().add(P4).multiply(0.5);
addPoint(P4, world);
addPoint(P5, world);
addPoint(location, world);
}
}
public static void ins(String[] args) {
index = Integer.parseInt(args[1]);
location = player.getLocation();
Camera.controlPoints.add(index, location);
Camera.broadlog("Point added: " + Camera.prettyLocation(location));
ShowCurve.insert(index, location, player.getWorld());
}
public static void rm(String[] args) {
index = Integer.parseInt(args[1]);
location = Camera.controlPoints.get(index);
Camera.controlPoints.remove(location);
Camera.broadlog("Point deleted: " + Camera.prettyLocation(location));
ShowCurve.rm(index);
}
public static void set(String[] args) {
index = Integer.parseInt(args[1]);
location = player.getLocation();
Camera.controlPoints.set(index, location);
Camera.broadlog("Point n°" + index + " set: " + Camera.prettyLocation(location));
ShowCurve.set(index, location, player.getWorld());
}
}