refactor: rewriting of Player, now extends Actor

This commit is contained in:
Laureηt 2021-04-08 23:09:54 +02:00
parent 0c843bdea1
commit f39d6b48cb

View file

@ -2,77 +2,55 @@ package sagittarius.model;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
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.Actor;
public class Player extends Entity {
public class Player extends Actor {
// ---------- ATTRIBUTEs ----------
private Planet home;
private final float width = 50;
private final float height = 100;
private Bow bow;
private Vector2 positionBottom = new Vector2(); // TODO : reorganize, center of mass....
private BitmapFont fontDebug;
// ---------- CONSTRUCTORs ----------
public Player(Planet home) {
super(home.position, 1);
public Player(Planet home, Color color) {
super();
this.setSize(50, 100);
this.setRotation(MathUtils.random(360));
this.setColor(color);
this.setOrigin(25, 50);
this.home = home;
this.bow = new Bow(this, true);
this.angle = 45; // TODO : tmp
}
// ---------- METHODs ----------
void render(ShapeRenderer shapeRenderer) {
shapeRenderer.setColor(color);
shapeRenderer.rect(positionBottom.x - width/2, positionBottom.y, width/2, 0, width, height, 1, 1, angle-90);
this.bow.render(shapeRenderer);
}
void renderDebug(Batch batch, BitmapFont font) {
font.draw(batch, "x = " + (int) position.x + ", y = " + (int) position.y, position.x, position.y);
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
fontDebug.draw(batch, "x = " + (int) this.getX() + ", y = " + (int) this.getY(), this.getX(), this.getY());
}
/**
* Updates the attributes of the Player,
* must be called in the main update loop.
*
* @param deltaTime time elapsed between 2 frames.
*/
void update(float deltaTime) {
// TODO : do eventListening instead of polling
// TODO : if player moving, add his speed to the arrow
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
this.angle += 100.0f / home.getRadius();
} else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
this.angle -= 100.0f / home.getRadius();
}
computePosition();
bow.update(deltaTime);
@Override
public void drawDebug(ShapeRenderer shapes) {
super.drawDebug(shapes);
}
/**
* Computes the position of the Player according to its attributes.
*/
void computePosition() {
Vector2 homePosition = this.home.position;
float homeRadius = this.home.getRadius();
this.position.x = homePosition.x + (homeRadius + height/2)*MathUtils.cosDeg(angle);
this.position.y = homePosition.y + (homeRadius + height/2)*MathUtils.sinDeg(angle); // TODO, faire des opérations vectorielles ?
this.positionBottom.x = homePosition.x + homeRadius*MathUtils.cosDeg(angle);
this.positionBottom.y = homePosition.y + homeRadius*MathUtils.sinDeg(angle);
@Override
public void act(float deltaTime) {
super.act(deltaTime);
this.setX(home.getX() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation()));
this.setY(home.getY() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation()));
this.bow.act(deltaTime);
}
/**