feat: /point rm i

This commit is contained in:
gdamms 2022-05-14 20:32:48 +02:00
parent 6c7f9f2e94
commit 1aa618865c
3 changed files with 107 additions and 84 deletions

View file

@ -49,6 +49,10 @@ public class Camera extends JavaPlugin implements Listener {
int nControlPoints = controlPoints.size(); int nControlPoints = controlPoints.size();
curve = new ArrayList<>(); curve = new ArrayList<>();
if (nControlPoints == 0) {
return;
}
if (nControlPoints <= 4) { if (nControlPoints <= 4) {
for (float t = 0; t < 1; t += dt) { for (float t = 0; t < 1; t += dt) {
ArrayList<Location> P = new ArrayList<>(controlPoints); ArrayList<Location> P = new ArrayList<>(controlPoints);
@ -63,25 +67,26 @@ public class Camera extends JavaPlugin implements Listener {
curve.add(P.get(0)); curve.add(P.get(0));
} }
} else { return;
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(); int nBezier = (nControlPoints - 1) / 3;
for (int k = N - 1; k > 0; k--) { for (int iBezier = 0; iBezier < nBezier; iBezier++) {
for (int i = 0; i < k; i++) { for (float t = 0; t < 1; t += dt) {
P.set(i, P.get(i).clone().multiply(1 - t) ArrayList<Location> P = new ArrayList<>();
.add(P.get(i + 1).clone().multiply(t))); for (int k = 0; k < 4; k++) {
} P.add(controlPoints.get(3 * iBezier + k).clone());
}
curve.add(P.get(0));
} }
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));
} }
} }
} }

View file

@ -25,10 +25,8 @@ public class ClearPoints implements CommandExecutor {
ListPoints.listPoints(); ListPoints.listPoints();
Camera.controlPoints = new ArrayList<>(); Camera.controlPoints = new ArrayList<>();
Camera.broadlog("Points cleared !"); Camera.compute();
Camera.broadlog("All 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()) {

View file

@ -22,9 +22,6 @@ public class NewPoint implements CommandExecutor {
case "add": case "add":
add(args); add(args);
break; break;
case "ins":
ins(args);
break;
case "rm": case "rm":
rm(args); rm(args);
break; break;
@ -40,9 +37,8 @@ public class NewPoint implements CommandExecutor {
e.printStackTrace(); e.printStackTrace();
} }
if (Camera.controlPoints.size() > 0) { // Update camera path
Camera.compute(); // update camera path Camera.compute();
}
return true; return true;
} }
@ -58,6 +54,12 @@ public class NewPoint implements CommandExecutor {
Camera.broadlog("Point n°" + index + " set: " + Camera.prettyLocation(point)); Camera.broadlog("Point n°" + index + " set: " + Camera.prettyLocation(point));
} }
public static void rmPoint(int index) {
location = Camera.controlPoints.remove(index);
ShowCurve.rm(index);
Camera.broadlog("Point deleted: " + Camera.prettyLocation(location));
}
public static void add(String[] args) { public static void add(String[] args) {
World world = player.getWorld(); World world = player.getWorld();
location = player.getLocation(); location = player.getLocation();
@ -77,6 +79,7 @@ public class NewPoint implements CommandExecutor {
} }
} }
@Deprecated
public static void ins(String[] args) { public static void ins(String[] args) {
index = Integer.parseInt(args[1]); index = Integer.parseInt(args[1]);
location = player.getLocation(); location = player.getLocation();
@ -87,10 +90,25 @@ public class NewPoint implements CommandExecutor {
public static void rm(String[] args) { public static void rm(String[] args) {
index = Integer.parseInt(args[1]); index = Integer.parseInt(args[1]);
location = Camera.controlPoints.get(index); int n = Camera.controlPoints.size();
Camera.controlPoints.remove(location);
Camera.broadlog("Point deleted: " + Camera.prettyLocation(location)); if (n <= 4) {
ShowCurve.rm(index); rmPoint(index);
return;
}
int indMin;
if (index == 0 || index == 1) {
indMin = 0;
} else if (index == n - 1 || index == n - 2) {
indMin = n - 3;
} else {
indMin = (index - 2) / 3 * 3 + 2;
}
for (int i = 0; i < 3; i++) {
rmPoint(indMin);
}
} }
public static void set(String[] args) { public static void set(String[] args) {
@ -101,59 +119,61 @@ public class NewPoint implements CommandExecutor {
if (n <= 4) { if (n <= 4) {
setPoint(index, location, world); setPoint(index, location, world);
} else { return;
switch (index % 3) {
case 0:
// Anchor point
Location shift = location.clone().subtract(Camera.controlPoints.get(index));
for (int i = 0; i < 3; i++) {
try {
setPoint(index + i - 1, Camera.controlPoints.get(index + i - 1).clone().add(shift), world);
} catch (IndexOutOfBoundsException e) {
// First or last anchor
}
}
break;
case 1:
// Control point after an anchor
if (index - 2 >= 0) {
// Get anchor-location direction
Location anchor = Camera.controlPoints.get(index - 1);
double currentDistance = anchor.distance(location);
Location currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance);
// Get anchor-corresponding distance
Location correspondingControl = Camera.controlPoints.get(index - 2);
double correspondingDistance = anchor.distance(correspondingControl);
// Keep correcponding at the same distance
// but along the anchor-location direction
setPoint(index - 2, anchor.clone().add(currentVect.multiply(correspondingDistance)), world);
}
// Move control point
setPoint(index, location, world);
break;
case 2:
// Control point before an anchor
if (index + 2 < n) {
// Get anchor-location direction
Location anchor = Camera.controlPoints.get(index + 1);
double currentDistance = anchor.distance(location);
Location currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance);
// Get anchor-corresponding distance
Location correspondingControl = Camera.controlPoints.get(index + 2);
double correspondingDistance = anchor.distance(correspondingControl);
// Keep correcponding at the same distance
// but along the anchor-location direction
setPoint(index + 2, anchor.clone().add(currentVect.multiply(correspondingDistance)), world);
}
// Move control point
setPoint(index, location, world);
break;
}
} }
switch (index % 3) {
case 0:
// Anchor point
Location shift = location.clone().subtract(Camera.controlPoints.get(index));
for (int i = 0; i < 3; i++) {
try {
setPoint(index + i - 1, Camera.controlPoints.get(index + i - 1).clone().add(shift), world);
} catch (IndexOutOfBoundsException e) {
// First or last anchor
}
}
break;
case 1:
// Control point after an anchor
if (index - 2 >= 0) {
// Get anchor-location direction
Location anchor = Camera.controlPoints.get(index - 1);
double currentDistance = anchor.distance(location);
Location currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance);
// Get anchor-corresponding distance
Location correspondingControl = Camera.controlPoints.get(index - 2);
double correspondingDistance = anchor.distance(correspondingControl);
// Keep correcponding at the same distance
// but along the anchor-location direction
setPoint(index - 2, anchor.clone().add(currentVect.multiply(correspondingDistance)), world);
}
// Move control point
setPoint(index, location, world);
break;
case 2:
// Control point before an anchor
if (index + 2 < n) {
// Get anchor-location direction
Location anchor = Camera.controlPoints.get(index + 1);
double currentDistance = anchor.distance(location);
Location currentVect = anchor.clone().subtract(location).multiply(1 / currentDistance);
// Get anchor-corresponding distance
Location correspondingControl = Camera.controlPoints.get(index + 2);
double correspondingDistance = anchor.distance(correspondingControl);
// Keep correcponding at the same distance
// but along the anchor-location direction
setPoint(index + 2, anchor.clone().add(currentVect.multiply(correspondingDistance)), world);
}
// Move control point
setPoint(index, location, world);
break;
}
} }
} }