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.Gdx;
import com.badlogic.gdx.Input.Keys; 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.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
public class Player extends Entity { public class Player extends Actor {
// ---------- ATTRIBUTEs ---------- // ---------- ATTRIBUTEs ----------
private Planet home; private Planet home;
private final float width = 50;
private final float height = 100;
private Bow bow; private Bow bow;
private BitmapFont fontDebug;
private Vector2 positionBottom = new Vector2(); // TODO : reorganize, center of mass....
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
public Player(Planet home) { public Player(Planet home, Color color) {
super(home.position, 1); super();
this.setSize(50, 100);
this.setRotation(MathUtils.random(360));
this.setColor(color);
this.setOrigin(25, 50);
this.home = home; this.home = home;
this.bow = new Bow(this, true); this.bow = new Bow(this, true);
this.angle = 45; // TODO : tmp
} }
// ---------- METHODs ---------- // ---------- METHODs ----------
void render(ShapeRenderer shapeRenderer) { @Override
shapeRenderer.setColor(color); public void draw(Batch batch, float parentAlpha) {
shapeRenderer.rect(positionBottom.x - width/2, positionBottom.y, width/2, 0, width, height, 1, 1, angle-90); super.draw(batch, parentAlpha);
this.bow.render(shapeRenderer); fontDebug.draw(batch, "x = " + (int) this.getX() + ", y = " + (int) this.getY(), this.getX(), this.getY());
}
void renderDebug(Batch batch, BitmapFont font) {
font.draw(batch, "x = " + (int) position.x + ", y = " + (int) position.y, position.x, position.y);
} }
/** @Override
* Updates the attributes of the Player, public void drawDebug(ShapeRenderer shapes) {
* must be called in the main update loop. super.drawDebug(shapes);
*
* @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
* Computes the position of the Player according to its attributes. public void act(float deltaTime) {
*/ super.act(deltaTime);
void computePosition() {
Vector2 homePosition = this.home.position; this.setX(home.getX() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation()));
float homeRadius = this.home.getRadius(); this.setY(home.getY() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation()));
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.bow.act(deltaTime);
this.positionBottom.x = homePosition.x + homeRadius*MathUtils.cosDeg(angle);
this.positionBottom.y = homePosition.y + homeRadius*MathUtils.sinDeg(angle);
} }
/** /**