From 64b487f80d8490ef412ffef1a2f9bbc1d6b5bd42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Tue, 6 Apr 2021 12:58:27 +0200 Subject: [PATCH] feat: added Bow's aimAssist --- core/src/bzh/fainsin/sagittarius/Arrow.java | 31 +++++++++++++++++-- core/src/bzh/fainsin/sagittarius/Bow.java | 5 ++- .../bzh/fainsin/sagittarius/GameScreen.java | 3 ++ core/src/bzh/fainsin/sagittarius/Player.java | 2 -- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/core/src/bzh/fainsin/sagittarius/Arrow.java b/core/src/bzh/fainsin/sagittarius/Arrow.java index 9761aa4..9a7e912 100644 --- a/core/src/bzh/fainsin/sagittarius/Arrow.java +++ b/core/src/bzh/fainsin/sagittarius/Arrow.java @@ -1,5 +1,7 @@ package bzh.fainsin.sagittarius; +import java.util.ArrayList; + import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; @@ -51,7 +53,7 @@ class Arrow extends Entity { if (!this.hasLanded()) { integrationVerlet(deltaTime); this.TTL -= deltaTime; - this.angle = this.velocity.angleRad(); + this.angle = this.velocity.angleDeg(); } } @@ -61,6 +63,8 @@ class Arrow extends Entity { // TODO : vectorialiser + // System.out.println(deltaTime); + this.acceleration = this.force.cpy(); this.position.x += deltaTime * ( this.velocity.x + deltaTime * this.acceleration.x / 2 ); @@ -73,7 +77,7 @@ class Arrow extends Entity { } void render(ShapeRenderer shapeRenderer) { - Vector2 tail = new Vector2(-this.length, 0).rotateRad(this.angle).add(this.position); + Vector2 tail = new Vector2(-this.length, 0).rotateDeg(this.angle).add(this.position); shapeRenderer.line(this.position, tail); } @@ -90,4 +94,27 @@ class Arrow extends Entity { } } +// ---------- STATIC METHODs ---------- + + static float[] traj(float angle, float power, Player shooter, int iterations, float timeStep) { + ArrayList path = new ArrayList(); + Arrow dummyArrow = new Arrow(angle, power, shooter); + for (int i = 0; i < iterations; i++) { + if (dummyArrow.hasLanded()) { break; } + dummyArrow.integrationVerlet(timeStep); + path.add(dummyArrow.position.x); + path.add(dummyArrow.position.y); + } + + // TODO : optimize, lots of useless stuff + + final float[] arr = new float[path.size()]; // TODO : min with 2, Exception in thread "main" java.lang.IllegalArgumentException: Polylines must contain at least 2 points. + int index = 0; + for (final Float value: path) { + arr[index++] = value; + } + + return arr; + } + } diff --git a/core/src/bzh/fainsin/sagittarius/Bow.java b/core/src/bzh/fainsin/sagittarius/Bow.java index f60c6fb..3a44162 100644 --- a/core/src/bzh/fainsin/sagittarius/Bow.java +++ b/core/src/bzh/fainsin/sagittarius/Bow.java @@ -56,7 +56,10 @@ class Bow { public void render(ShapeRenderer shapeRenderer) { if (pressed) { shapeRenderer.line(this.anchor, SagittariusGame.worldCursor); - //shapeRenderer.polyline(traj); + float[] traj = Arrow.traj(angle, power, shooter, 250, 0.05f); + if (traj.length > 2) { + shapeRenderer.polyline(traj); + } } } diff --git a/core/src/bzh/fainsin/sagittarius/GameScreen.java b/core/src/bzh/fainsin/sagittarius/GameScreen.java index f502eaa..8fdd20b 100644 --- a/core/src/bzh/fainsin/sagittarius/GameScreen.java +++ b/core/src/bzh/fainsin/sagittarius/GameScreen.java @@ -93,6 +93,9 @@ class GameScreen extends ScreenAdapter { arrow.render(shapeRenderer); } + // float[] vertices = {100, 100, 200, 200, 1000, 500}; + // shapeRenderer.polyline(vertices); + shapeRenderer.end(); // HUD diff --git a/core/src/bzh/fainsin/sagittarius/Player.java b/core/src/bzh/fainsin/sagittarius/Player.java index 34e25b0..4327695 100644 --- a/core/src/bzh/fainsin/sagittarius/Player.java +++ b/core/src/bzh/fainsin/sagittarius/Player.java @@ -1,14 +1,12 @@ package bzh.fainsin.sagittarius; import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Input.Buttons; import com.badlogic.gdx.Input.Keys; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.scenes.scene2d.ui.Button; class Player extends Entity {