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.Bukkit;
import org.bukkit.Location; 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.Plugin;
import org.bukkit.plugin.java.JavaPlugin; 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 ArrayList<Location> curve;
public static List<Location> controlPoints; public static List<Location> controlPoints;
@ -34,23 +43,49 @@ public class Camera extends JavaPlugin {
this.getCommand("reset").setExecutor(new ClearPoints()); this.getCommand("reset").setExecutor(new ClearPoints());
this.getCommand("exec").setExecutor(new ExecuteTraveling()); this.getCommand("exec").setExecutor(new ExecuteTraveling());
this.getCommand("show").setExecutor(new ShowCurve()); this.getCommand("show").setExecutor(new ShowCurve());
// Eventhandlers
getServer().getPluginManager().registerEvents(this, this);
} }
public static void compute() { public static void compute() {
int nControlPoints = controlPoints.size();
curve = new ArrayList<>(); curve = new ArrayList<>();
for (float t = 0; t < 1; t += dt) { if (nControlPoints <= 4) {
ArrayList<Location> P = new ArrayList<>(Camera.controlPoints); for (float t = 0; t < 1; t += dt) {
ArrayList<Location> P = new ArrayList<>(controlPoints);
int N = P.size(); int N = P.size();
for (int k = N - 1; k > 0; k--) { for (int k = N - 1; k > 0; k--) {
for (int i = 0; i < k; i++) { for (int i = 0; i < k; i++) {
P.set(i, P.get(i).clone().multiply(1 - t) P.set(i, P.get(i).clone().multiply(1 - t)
.add(P.get(i + 1).clone().multiply(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() { public void onDisable() {
ClearPoints.clear(); 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() { public static void clear() {
Camera.broadlog("Clearing points:"); Camera.broadlog("Clearing points and path:");
ListPoints.listPoints(); ListPoints.listPoints();
Camera.controlPoints = new ArrayList<>(); Camera.controlPoints = new ArrayList<>();
Camera.broadlog("Points cleared !"); Camera.broadlog("Points cleared !");
Camera.curve = new ArrayList<>();
Camera.broadlog("Path cleared !");
for (World world : Bukkit.getWorlds()) { for (World world : Bukkit.getWorlds()) {
for (Entity e : world.getEntities()) { for (Entity e : world.getEntities()) {
if (e.getScoreboardTags().contains("controlPoint")) { if (e.getScoreboardTags().contains("controlPoint")) {

View file

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