projet-programmation-orient.../core/src/sagittarius/Player.java

74 lines
2.2 KiB
Java
Raw Normal View History

2021-04-06 20:08:14 +00:00
package sagittarius;
2021-04-05 18:42:51 +00:00
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
2021-04-05 18:42:51 +00:00
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 {
2021-04-05 18:42:51 +00:00
// ---------- 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....
2021-04-05 18:42:51 +00:00
// ---------- CONSTRUCTORs ----------
Player(Planet home) {
super(home.position, 1);
2021-04-05 18:42:51 +00:00
this.home = home;
this.bow = new Bow(this, false);
this.angle = 45; // TODO : tmp
2021-04-05 18:42:51 +00:00
}
// ---------- 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);
2021-04-05 18:42:51 +00:00
}
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) {
2021-04-05 18:42:51 +00:00
// 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();
}
2021-04-05 18:42:51 +00:00
computePosition();
bow.update(deltaTime);
2021-04-05 18:42:51 +00:00
}
void computePosition() {
Vector2 homePosition = this.home.position;
2021-04-05 18:42:51 +00:00
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;
2021-04-05 18:42:51 +00:00
}
}