feat: Player class bound to a planet
This commit is contained in:
parent
f02f41e2e7
commit
caa6c614eb
|
@ -20,13 +20,9 @@ abstract class Entity {
|
||||||
this.mass = mass;
|
this.mass = mass;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Entity(Vector2 position, float mass, Color color) {
|
|
||||||
this(position, mass);
|
|
||||||
this.color = color;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------- GETs ----------
|
// ---------- GETs ----------
|
||||||
|
|
||||||
|
|
||||||
Vector2 getPosition() {
|
Vector2 getPosition() {
|
||||||
return this.position;
|
return this.position;
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,17 @@ class GameScreen extends ScreenAdapter {
|
||||||
// ---------- batch ----------
|
// ---------- batch ----------
|
||||||
batch.setProjectionMatrix(camera.combined);
|
batch.setProjectionMatrix(camera.combined);
|
||||||
batch.begin();
|
batch.begin();
|
||||||
|
|
||||||
|
// planets
|
||||||
|
for (Planet planet : SagittariusGame.planetList) {
|
||||||
|
planet.renderDebug(batch, font);
|
||||||
|
}
|
||||||
|
|
||||||
|
// players
|
||||||
|
for (Player player : SagittariusGame.playerList) {
|
||||||
|
player.renderDebug(batch, font);
|
||||||
|
}
|
||||||
|
|
||||||
batch.end();
|
batch.end();
|
||||||
|
|
||||||
// ---------- shapeRenderer ----------
|
// ---------- shapeRenderer ----------
|
||||||
|
@ -67,6 +78,11 @@ class GameScreen extends ScreenAdapter {
|
||||||
planet.render(shapeRenderer);
|
planet.render(shapeRenderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// players
|
||||||
|
for (Player player : SagittariusGame.playerList) {
|
||||||
|
player.render(shapeRenderer);
|
||||||
|
}
|
||||||
|
|
||||||
shapeRenderer.end();
|
shapeRenderer.end();
|
||||||
|
|
||||||
// HUD
|
// HUD
|
||||||
|
|
|
@ -20,8 +20,8 @@ class Planet extends Entity {
|
||||||
}
|
}
|
||||||
|
|
||||||
Planet(Vector2 position, float mass, float radius, Color color) {
|
Planet(Vector2 position, float mass, float radius, Color color) {
|
||||||
super(position, mass, color);
|
this(position, mass, radius);
|
||||||
this.radius = radius;
|
this.color = color;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ---------- METHODs ----------
|
// ---------- METHODs ----------
|
||||||
|
|
50
core/src/bzh/fainsin/sagittarius/Player.java
Normal file
50
core/src/bzh/fainsin/sagittarius/Player.java
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package bzh.fainsin.sagittarius;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class Player extends Entity {
|
||||||
|
|
||||||
|
// ---------- ATTRIBUTEs ----------
|
||||||
|
|
||||||
|
private Planet home;
|
||||||
|
private final float width = 50;
|
||||||
|
private final float height = 100;
|
||||||
|
|
||||||
|
// ---------- CONSTRUCTORs ----------
|
||||||
|
|
||||||
|
Player(Planet home) {
|
||||||
|
super(new Vector2(), 1);
|
||||||
|
this.home = home;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------- METHODs ----------
|
||||||
|
|
||||||
|
void render(ShapeRenderer shapeRenderer) {
|
||||||
|
shapeRenderer.setColor(color);
|
||||||
|
shapeRenderer.rect(position.x, position.y, width/2, 0, width, height, 1, 1, angle-90);
|
||||||
|
}
|
||||||
|
|
||||||
|
void renderDebug(Batch batch, BitmapFont font) {
|
||||||
|
font.draw(batch, "x = " + (int) position.x + ", y = " + (int) position.y, position.x, position.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update(float delta) {
|
||||||
|
|
||||||
|
computePosition();
|
||||||
|
|
||||||
|
this.angle += 100.0f / home.getRadius();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void computePosition() {
|
||||||
|
Vector2 homePosition = this.home.getPosition();
|
||||||
|
float homeRadius = this.home.getRadius();
|
||||||
|
this.position.x = homePosition.x + homeRadius*MathUtils.cosDeg(angle) - width/2;
|
||||||
|
this.position.y = homePosition.y + homeRadius*MathUtils.sinDeg(angle);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -16,8 +16,9 @@ public class SagittariusGame extends Game {
|
||||||
static Vector2 screenCursor;
|
static Vector2 screenCursor;
|
||||||
static Vector2 worldCursor;
|
static Vector2 worldCursor;
|
||||||
|
|
||||||
// Planets
|
// Entities
|
||||||
static ArrayList<Planet> planetList;
|
static ArrayList<Planet> planetList;
|
||||||
|
static ArrayList<Player> playerList;
|
||||||
|
|
||||||
// ---------- METHODs ----------
|
// ---------- METHODs ----------
|
||||||
|
|
||||||
|
@ -27,7 +28,11 @@ public class SagittariusGame extends Game {
|
||||||
|
|
||||||
planetList = new ArrayList<Planet>();
|
planetList = new ArrayList<Planet>();
|
||||||
planetList.add( new Planet(new Vector2(100, 100), 1, 50) );
|
planetList.add( new Planet(new Vector2(100, 100), 1, 50) );
|
||||||
planetList.add( new Planet(new Vector2(1500, 1000), 1, 200, Color.CYAN) );
|
planetList.add( new Planet(new Vector2(400, 400), 1, 100, Color.CYAN) );
|
||||||
|
|
||||||
|
playerList = new ArrayList<Player>();
|
||||||
|
playerList.add( new Player(planetList.get(0)) );
|
||||||
|
playerList.add( new Player(planetList.get(1)) );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update(float delta) {
|
static void update(float delta) {
|
||||||
|
@ -37,6 +42,11 @@ public class SagittariusGame extends Game {
|
||||||
Vector3 unprojectedCursor = GameScreen.camera.unproject(new Vector3(screenCursor, 0));
|
Vector3 unprojectedCursor = GameScreen.camera.unproject(new Vector3(screenCursor, 0));
|
||||||
worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);
|
worldCursor = new Vector2(unprojectedCursor.x, unprojectedCursor.y);
|
||||||
|
|
||||||
|
// players
|
||||||
|
for (Player player : playerList) {
|
||||||
|
player.update(delta);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue