feat: added revolving planets (and moons)

This commit is contained in:
Laureηt 2021-04-23 16:17:09 +02:00
parent 5b591e3f8e
commit ed9e805c88
6 changed files with 34 additions and 14 deletions

View file

@ -4,6 +4,8 @@ import com.badlogic.gdx.Game;
import com.kotcrab.vis.ui.VisUI; import com.kotcrab.vis.ui.VisUI;
import sagittarius.view.BaseScreen; import sagittarius.view.BaseScreen;
import sagittarius.view.GameScreen;
import sagittarius.view.SettingsScreen;
import sagittarius.view.StartScreen; import sagittarius.view.StartScreen;
public class SagittariusGame extends Game { public class SagittariusGame extends Game {
@ -29,7 +31,7 @@ public class SagittariusGame extends Game {
@Override @Override
public void create() { public void create() {
VisUI.load(); VisUI.load();
game.setScreen(new StartScreen()); game.setScreen(new GameScreen());
} }
} }

View file

@ -46,13 +46,13 @@ public abstract class EntityCircle extends Entity {
@Override @Override
protected float[] getHitbox() { // peut être optimisé protected float[] getHitbox() { // peut être optimisé
int segments = 128; int segments = 64;
float[] vertices = new float[2*segments]; float[] vertices = new float[2*segments];
float theta = 360.0f / segments; float theta = 360.0f / segments;
for (int i = 0; i < segments; i++) { for (int i = 0; i < segments; i++) {
vertices[2*i] = getX() + MathUtils.cosDeg(theta * (i+1))*radius; vertices[2*i] = getX() + MathUtils.cosDeg(theta * (i+1) + getRotation())*radius;
vertices[2*i+1] = getY() + MathUtils.sinDeg(theta * (i+1))*radius; vertices[2*i+1] = getY() + MathUtils.sinDeg(theta * (i+1) + getRotation())*radius;
} }
return vertices; return vertices;

View file

@ -14,6 +14,7 @@ public class Moon extends Planet {
public Moon(Planet sun, float mass, float radius, float altitude, Color color) { public Moon(Planet sun, float mass, float radius, float altitude, Color color) {
super(new Vector2(), mass, radius, color); super(new Vector2(), mass, radius, color);
this.angle = MathUtils.random(360);
this.sun = sun; this.sun = sun;
this.altitude = altitude; this.altitude = altitude;
} }
@ -31,9 +32,9 @@ public class Moon extends Planet {
@Override @Override
public void act(float dt) { public void act(float dt) {
this.rotateBy(10.0f / this.altitude); this.angle += 10.0f / this.altitude;
this.setX(sun.getX() + this.altitude*MathUtils.cosDeg(this.getRotation())); this.setX(sun.getX() + this.altitude*MathUtils.cosDeg(this.angle));
this.setY(sun.getY() + this.altitude*MathUtils.sinDeg(this.getRotation())); this.setY(sun.getY() + this.altitude*MathUtils.sinDeg(this.angle));
super.act(dt); super.act(dt);
} }

View file

@ -1,10 +1,15 @@
package sagittarius.model; package sagittarius.model;
import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
public class Planet extends EntityCircle { public class Planet extends EntityCircle {
// ---------- ATTRIBUTEs ----------
float revolutionRate = MathUtils.randomTriangular(1) + 1;
// ---------- CONSTRUCTORs ---------- // ---------- CONSTRUCTORs ----------
public Planet(Vector2 position, float mass, float radius, Color color) { public Planet(Vector2 position, float mass, float radius, Color color) {
@ -13,6 +18,12 @@ public class Planet extends EntityCircle {
// ---------- METHODs ---------- // ---------- METHODs ----------
@Override
public void act(float dt) {
super.act(dt);
this.rotateBy(revolutionRate / this.radius);
}
@Override @Override
protected String getInfo() { protected String getInfo() {
return return

View file

@ -17,7 +17,7 @@ public class Player extends EntityQuad {
public Player(Planet home, Color color) { public Player(Planet home, Color color) {
super(MathUtils.random(360), 0, color, home.getPosition()); super(MathUtils.random(360), 0, color, home.getPosition());
this.setRotation(angle-90); this.setRotation(home.getRotation() + angle - 90);
this.setSize(50, 100); this.setSize(50, 100);
this.setOrigin(getWidth()/2, getHeight()/2); this.setOrigin(getWidth()/2, getHeight()/2);
@ -47,10 +47,10 @@ public class Player extends EntityQuad {
} }
} }
setX(home.getX() + (home.getRadius() + getHeight()/2)*MathUtils.cosDeg(angle)); setX(home.getX() + (home.getRadius() + getHeight()/2)*MathUtils.cosDeg(home.getRotation() + angle));
setY(home.getY() + (home.getRadius() + getHeight()/2)*MathUtils.sinDeg(angle)); setY(home.getY() + (home.getRadius() + getHeight()/2)*MathUtils.sinDeg(home.getRotation() + angle));
this.setRotation(angle-90); this.setRotation(home.getRotation() + angle - 90);
} }
@Override @Override

View file

@ -37,15 +37,18 @@ public class GameScreen extends BaseScreen {
// planets & moons // planets & moons
attractors = new Group(); attractors = new Group();
Planet planet1 = new Planet(new Vector2(400, 400), 1000, 50, Color.BLUE); Planet planet1 = new Planet(new Vector2(300, 200), 1000, 50, Color.BLUE);
attractors.addActor(planet1); attractors.addActor(planet1);
Planet planet2 = new Planet(new Vector2(1400, 700), 1000, 100, Color.ORANGE); Planet planet2 = new Planet(new Vector2(1200, 700), 4000, 200, Color.ORANGE);
attractors.addActor(planet2); attractors.addActor(planet2);
Moon moon2_1 = new Moon(planet2, 100, 10, 300, Color.MAGENTA); Moon moon2_1 = new Moon(planet2, 100, 70, 500, Color.MAGENTA);
attractors.addActor(moon2_1); attractors.addActor(moon2_1);
Planet planet3 = new Planet(new Vector2(1500, 100), 1000, 70, Color.PINK);
attractors.addActor(planet3);
mainStage.addActor(attractors); mainStage.addActor(attractors);
// players // players
@ -57,6 +60,9 @@ public class GameScreen extends BaseScreen {
Player player2 = new Player(planet2, Color.WHITE); Player player2 = new Player(planet2, Color.WHITE);
players.addActor(player2); players.addActor(player2);
Player player3 = new Player(moon2_1, Color.YELLOW);
players.addActor(player3);
mainStage.addActor(players); mainStage.addActor(players);
// arrows // arrows