74 lines
2.2 KiB
Java
74 lines
2.2 KiB
Java
package sagittarius;
|
|
|
|
import com.badlogic.gdx.Gdx;
|
|
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;
|
|
|
|
class Player extends Entity {
|
|
|
|
// ---------- 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....
|
|
|
|
// ---------- CONSTRUCTORs ----------
|
|
|
|
Player(Planet home) {
|
|
super(home.position, 1);
|
|
this.home = home;
|
|
this.bow = new Bow(this, false);
|
|
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);
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
Planet getHome() {
|
|
return this.home;
|
|
}
|
|
|
|
}
|