From d664987ff8b420b0f445ada7303a3b2c735872d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Wed, 12 May 2021 22:09:38 +0200 Subject: [PATCH] feat: optimized computeTrajectory --- core/src/sagittarius/SagittariusGame.java | 2 +- core/src/sagittarius/model/Arrow.java | 19 +++++------------ core/src/sagittarius/model/Bow.java | 6 ++---- core/src/sagittarius/model/Trajectory.java | 24 ++++++++++++++++++++++ 4 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 core/src/sagittarius/model/Trajectory.java diff --git a/core/src/sagittarius/SagittariusGame.java b/core/src/sagittarius/SagittariusGame.java index 5e417c1..f07989a 100644 --- a/core/src/sagittarius/SagittariusGame.java +++ b/core/src/sagittarius/SagittariusGame.java @@ -9,7 +9,7 @@ public class SagittariusGame extends Game { // Constants public static float G = 100; - public static boolean debugMode = false; + public static boolean debugMode = true; private static Game game; diff --git a/core/src/sagittarius/model/Arrow.java b/core/src/sagittarius/model/Arrow.java index 4cfce7c..73938ce 100644 --- a/core/src/sagittarius/model/Arrow.java +++ b/core/src/sagittarius/model/Arrow.java @@ -1,5 +1,6 @@ package sagittarius.model; +import java.security.PrivilegedExceptionAction; import java.util.ArrayList; import java.util.Random; @@ -215,26 +216,16 @@ public class Arrow extends EntityQuad { * @param timeStep time period used for the integration. * @return an array of vertices describing the trajectory of the Arrow. */ - static float[] traj(float angle, float power, Player shooter, int iterations, float timeStep) { - ArrayList path = new ArrayList(); + static Trajectory computeTrajectory(float angle, float power, Player shooter, int iterations, float timeStep) { + Trajectory traj = new Trajectory(iterations); Arrow dummyArrow = new Arrow(angle, power, shooter, true); for (int i = 0; i < iterations; i++) { dummyArrow.integrationVerlet(timeStep); - path.add(dummyArrow.getX()); - path.add(dummyArrow.getY()); + traj.add(dummyArrow.getPosition()); if ( dummyArrow.hasLanded() || dummyArrow.hasHit() ) { break; } } - // TODO : optimize, lots of useless stuff + change name - // or ignore and use adapters - - final float[] arr = new float[path.size()]; // utiliser le count de la fonction, faire tab size iteration*2, et draw avec count - int index = 0; // TODO: créer objet Trajectory, avec un float[iteration*2] et un actualSize; - for (final Float value: path) { - arr[index++] = value; - } - - return arr; + return traj; } @Override diff --git a/core/src/sagittarius/model/Bow.java b/core/src/sagittarius/model/Bow.java index 79fb42d..61cda43 100644 --- a/core/src/sagittarius/model/Bow.java +++ b/core/src/sagittarius/model/Bow.java @@ -60,10 +60,8 @@ public class Bow extends Actor { if (power > 50) shapes.setColor(Color.RED); shapes.line(this.anchor, GameScreen.worldCursor); if (aimAssist) { - float[] traj = Arrow.traj(angle, power, GameScreen.playerCurrent, 500, 0.03f); - if (traj.length > 2) { - shapes.polyline(traj); - } + Trajectory traj = Arrow.computeTrajectory(angle, power, GameScreen.playerCurrent, 500, 0.03f); + shapes.polyline(traj.path, 0, traj.actualSize); } } } diff --git a/core/src/sagittarius/model/Trajectory.java b/core/src/sagittarius/model/Trajectory.java new file mode 100644 index 0000000..8491db0 --- /dev/null +++ b/core/src/sagittarius/model/Trajectory.java @@ -0,0 +1,24 @@ +package sagittarius.model; + +import com.badlogic.gdx.math.Vector2; + +public class Trajectory { + + float[] path; + int actualSize; + + public Trajectory(int iterations) { + path = new float[2*iterations]; + } + + public void add(float x, float y) { + path[actualSize++] = x; + path[actualSize++] = y; + } + + public void add(Vector2 vec) { + path[actualSize++] = vec.x; + path[actualSize++] = vec.y; + } + +}