fix: Bow & Arrow partially working

This commit is contained in:
Laureηt 2021-04-09 16:30:24 +02:00
parent c70d2beace
commit 6a95fe7d3a
5 changed files with 84 additions and 92 deletions

View file

@ -8,7 +8,8 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.Sagittarius;
import sagittarius.SagittariusGame;
import sagittarius.view.GameScreen;
public class Arrow extends Actor {
@ -19,11 +20,8 @@ public class Arrow extends Actor {
private Vector2 force = new Vector2();
private float TTL = 20;
private final float length = 100;
private boolean active = true;
private Planet crash;
private Vector2 offset = new Vector2();
private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ----------
@ -51,55 +49,29 @@ public class Arrow extends Actor {
*/
private Vector2 computeForce() {
Vector2 force = new Vector2();
for (Planet attractor : Sagittarius.planetList) {
Vector2 diff = attractor.position.cpy().sub(this.position);
Vector2 attraction = diff.scl( Sagittarius.G * attractor.mass / diff.len2() );
force.add(attraction);
}
for (Moon attractor : Sagittarius.moonList) {
Vector2 diff = attractor.position.cpy().sub(this.position);
Vector2 attraction = diff.scl( Sagittarius.G * attractor.mass / diff.len2() );
force.add(attraction);
for (Actor actor : GameScreen.planets.getChildren()) {
float dx = actor.getX() - this.getX();
float dy = actor.getX() - this.getX();
float len2 = dx*dx + dy*dy;
float coeff = SagittariusGame.G * ((Planet) actor).getMass() * (float) Math.pow(len2, -3/2);
float gravityX = coeff * dx;
float gravityY = coeff * dy;
force.add(gravityX, gravityY);
}
return force;
}
/**
* Verify whether or not the Arrow contacts an Entity.
*/
private void verifyActivity() {
for (Planet planet : Sagittarius.planetList) {
if (this.distanceTo(planet) < planet.getRadius()) {
this.active = false;
this.crash = planet;
this.offset = this.position.cpy().sub(planet.position);
}
}
for (Moon moon : Sagittarius.moonList) {
if (this.distanceTo(moon) < moon.getRadius()) {
this.active = false;
this.crash = moon;
this.offset = this.position.cpy().sub(moon.position);
}
}
}
@Override
public void act(float dt) {
/**
* Updates the physical attributes of the Arrow,
* must be called in the main update loop.
*
* @param dt time elapsed between 2 frames.
*/
public void update(float dt) {
if (this.active) {
verifyActivity();
integrationVerlet(dt);
this.TTL -= dt;
this.angle = this.velocity.angleDeg();
} else {
this.position = crash.position.cpy().add(offset);
}
this.setRotation(this.velocity.angleDeg());
}
/**
@ -114,8 +86,8 @@ public class Arrow extends Actor {
this.acceleration = this.force.cpy();
this.position.x += dt * ( this.velocity.x + dt * this.acceleration.x / 2 );
this.position.y += dt * ( this.velocity.y + dt * this.acceleration.y / 2 );
this.moveBy(dt * ( this.velocity.x + dt * this.acceleration.x / 2 ),
dt * ( this.velocity.y + dt * this.acceleration.y / 2 ));
this.force = computeForce();
@ -126,21 +98,19 @@ public class Arrow extends Actor {
// ---------- GRAPHICAL METHODs ----------
public void render(ShapeRenderer shapeRenderer) {
Vector2 tail = new Vector2(-this.length, 0).rotateDeg(this.angle).add(this.position);
shapeRenderer.line(this.position, tail);
// Vector2 tail = new Vector2(-this.length, 0).rotateDeg(this.getRotation()).add(this.position);
// shapeRenderer.line(this.getX(), this.getY(), tail.x, tail.y);
}
public void renderDebug(Batch batch, BitmapFont font) {
// TODO : dirty, do it in an other way ?
if (active) {
font.draw(batch, "TTL = " + this.TTL, this.position.x, this.position.y + font.getCapHeight()*5);
font.draw(batch, "pos = " + this.position, this.position.x, this.position.y + font.getCapHeight()*4);
font.draw(batch, "speed = " + this.velocity, this.position.x, this.position.y + font.getCapHeight()*3);
font.draw(batch, "accel = " + this.acceleration, this.position.x, this.position.y + font.getCapHeight()*2);
font.draw(batch, "force = " + this.force, this.position.x, this.position.y + font.getCapHeight()*1);
font.draw(batch, "angle = " + this.angle, this.position.x, this.position.y);
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
fontDebug.draw(batch, "TTL=" + this.TTL, getX(), getY() + fontDebug.getCapHeight()*5);
fontDebug.draw(batch, "pos=" + getX() + "," + getY(), getX(), getY() + fontDebug.getCapHeight()*4);
fontDebug.draw(batch, "speed=" + this.velocity, getX(), getY() + fontDebug.getCapHeight()*3);
fontDebug.draw(batch, "accel=" + this.acceleration, getX(), getY() + fontDebug.getCapHeight()*2);
fontDebug.draw(batch, "force=" + this.force, getX(), getY() + fontDebug.getCapHeight()*1);
fontDebug.draw(batch, "angle=" + getRotation(), getX(), getY());
}
// ---------- STATIC METHODs ----------
@ -157,11 +127,9 @@ public class Arrow extends Actor {
ArrayList<Float> path = new ArrayList<Float>();
Arrow dummyArrow = new Arrow(angle, power, shooter);
for (int i = 0; i < iterations; i++) {
dummyArrow.verifyActivity();
if (!dummyArrow.active) { break; }
dummyArrow.integrationVerlet(timeStep);
path.add(dummyArrow.position.x);
path.add(dummyArrow.position.y);
path.add(dummyArrow.getX());
path.add(dummyArrow.getY());
}
// TODO : optimize, lots of useless stuff + change name
@ -176,4 +144,8 @@ public class Arrow extends Actor {
return arr;
}
public Vector2 getPosition() {
return new Vector2( getX(), getY() );
}
}

View file

@ -7,7 +7,7 @@ import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import sagittarius.Sagittarius;
import sagittarius.view.GameScreen;
class Bow extends Actor {
@ -18,8 +18,8 @@ class Bow extends Actor {
private boolean pressed = false;
private float angle;
private Vector2 anchor;
private Vector2 aim;
private Vector2 anchor = new Vector2();
private Vector2 aim = new Vector2();
private float power;
@ -36,47 +36,55 @@ class Bow extends Actor {
@Override
public void act(float dt) {
// TODO: probably can do better with an eventListener
if (Gdx.input.isButtonJustPressed(Buttons.LEFT) && !pressed) {
this.anchor = Sagittarius.worldCursor.cpy();
this.anchor = GameScreen.worldCursor.cpy();
pressed = true;
} else if (Gdx.input.isButtonPressed(Buttons.LEFT) && pressed) {
computeArrow();
} else if (pressed) {
Sagittarius.arrowList.add(getArrow());
// Sagittarius.arrowList.add(getArrow());
pressed = false;
}
}
/**
* Generates an Arrow according to the Bow's attributes.
*
* @return an Arrow.
*/
private Arrow getArrow() {
return new Arrow(angle, power, shooter);
}
// /**
// * Generates an Arrow according to the Bow's attributes.
// *
// * @return an Arrow.
// */
// private Arrow getArrow() {
// return new Arrow(angle, power, shooter);
// }
/**
* Converts the Bow's attributes to apply it
* to the Arrow's constructor.
*/
private void computeArrow() {
aim = this.anchor.cpy().sub(Sagittarius.worldCursor);
aim = this.anchor.cpy().sub(GameScreen.worldCursor);
angle = aim.angleDeg();
power = MathUtils.clamp(aim.len(), 0, 1000);
}
void render(ShapeRenderer shapeRenderer) {
@Override
public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes);
if (pressed) {
shapeRenderer.line(this.anchor, Sagittarius.worldCursor);
if (aimAssist) {
float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f);
if (traj.length > 2) {
shapeRenderer.polyline(traj);
}
}
shapes.line(this.anchor, GameScreen.worldCursor);
}
}
// void render(ShapeRenderer shapeRenderer) {
// if (pressed) {
// if (aimAssist) {
// float[] traj = Arrow.traj(angle, power, shooter, 400, 0.05f);
// if (traj.length > 2) {
// shapeRenderer.polyline(traj);
// }
// }
// }
// })
}

View file

@ -55,4 +55,12 @@ public class Planet extends Group {
this.addActor(moon);
}
public float getMass() {
return mass;
}
public Vector2 getPosition() {
return new Vector2( getX(), getY() );
}
}

View file

@ -15,7 +15,7 @@ public class Player extends Actor {
// ---------- ATTRIBUTEs ----------
private Planet home;
// private Bow bow;
private Bow bow;
private BitmapFont fontDebug = new BitmapFont();
// ---------- CONSTRUCTORs ----------
@ -27,7 +27,7 @@ public class Player extends Actor {
this.setColor(color);
this.setOrigin(0, getHeight()/2);
this.home = home;
//this.bow = new Bow(this, true);
this.bow = new Bow(this, true);
}
// ---------- METHODs ----------
@ -55,6 +55,7 @@ public class Player extends Actor {
getScaleX(), getScaleY(),
getRotation());
shapes.line(home.getX(), home.getY(), getX(), getY());
bow.drawDebug(shapes);
}
@Override
@ -70,6 +71,8 @@ public class Player extends Actor {
setX(home.getX() + home.getRadius()*MathUtils.cosDeg(getRotation()));
setY(home.getY() + home.getRadius()*MathUtils.sinDeg(getRotation()));
bow.act(dt);
}
/**

View file

@ -55,4 +55,5 @@ public abstract class BaseScreen implements Screen {
public Stage getUIStage() {
return uiStage;
}
}