refactor: rewriting of Player, now extends Actor
This commit is contained in:
parent
0c843bdea1
commit
f39d6b48cb
|
@ -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) {
|
@Override
|
||||||
font.draw(batch, "x = " + (int) position.x + ", y = " + (int) position.y, position.x, position.y);
|
public void drawDebug(ShapeRenderer shapes) {
|
||||||
|
super.drawDebug(shapes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Updates the attributes of the Player,
|
public void act(float deltaTime) {
|
||||||
* must be called in the main update loop.
|
super.act(deltaTime);
|
||||||
*
|
|
||||||
* @param deltaTime time elapsed between 2 frames.
|
|
||||||
*/
|
|
||||||
void update(float deltaTime) {
|
|
||||||
|
|
||||||
// TODO : do eventListening instead of polling
|
this.setX(home.getX() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation()));
|
||||||
// TODO : if player moving, add his speed to the arrow
|
this.setY(home.getY() + this.home.getRadius()*MathUtils.cosDeg(this.getRotation()));
|
||||||
|
|
||||||
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
|
this.bow.act(deltaTime);
|
||||||
this.angle += 100.0f / home.getRadius();
|
|
||||||
} else if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
|
|
||||||
this.angle -= 100.0f / home.getRadius();
|
|
||||||
}
|
|
||||||
|
|
||||||
computePosition();
|
|
||||||
|
|
||||||
bow.update(deltaTime);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue