From 0101996b9150065bfd25131dca4f0a034fa7a1df Mon Sep 17 00:00:00 2001 From: Damien Date: Sat, 29 May 2021 12:21:10 +0200 Subject: [PATCH] planets random --- core/src/sagittarius/view/GameScreen.java | 61 ++++++++++++++++++----- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/core/src/sagittarius/view/GameScreen.java b/core/src/sagittarius/view/GameScreen.java index 26d9483..eb5f327 100644 --- a/core/src/sagittarius/view/GameScreen.java +++ b/core/src/sagittarius/view/GameScreen.java @@ -9,6 +9,7 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector3; +import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Group; import sagittarius.SagittariusGame; @@ -16,6 +17,19 @@ import sagittarius.model.*; public class GameScreen extends BaseScreen implements InputProcessor { + // ---------- GENERATION ---------- + + public static int numberPlanets = 4; + public static int numberMoons = 2; + public static int minDistance = 400; + public static int maxDistance = 800; + public static int minMass = 500; + public static int maxMass = 1000; + public static int minRadius = 20; + public static int maxRadius = 150; + + + // ---------- ATTRIBUTEs ---------- public static BitmapFont fontDebug = new BitmapFont(); @@ -72,30 +86,51 @@ public class GameScreen extends BaseScreen implements InputProcessor { // planets & moons attractors = new Group(); - Planet planet1 = new Planet(new Vector2(300, 200), 1000, 50, Color.BLUE); - attractors.addActor(planet1); + for (int i = 0; i < numberPlanets; i++) { + Vector2 position; + if (i == 0) { + position = new Vector2(0, 0); + } else { + int distance = minDistance + MathUtils.random(maxDistance - minDistance); + float angle = 2*MathUtils.PI*MathUtils.random(); + float shortestDistance; + do { + int index = MathUtils.random(i-1); + Actor planet0 = attractors.getChild(index); + position = new Vector2(distance * (float) Math.cos(angle), distance * (float) Math.sin(angle)); + position.add(planet0.getX(), planet0.getY()); - Planet planet2 = new Planet(new Vector2(1200, 700), 3000, 200, Color.ORANGE); - attractors.addActor(planet2); - - Moon moon2_1 = new Moon(planet2, 100, 70, 500, Color.MAGENTA); - attractors.addActor(moon2_1); - - Planet planet3 = new Planet(new Vector2(1500, 100), 1000, 70, Color.PINK); - attractors.addActor(planet3); + shortestDistance = maxDistance; + for (int j = 0; j < i-1; j++) { + Actor planet1 = attractors.getChild(j); + float dx = planet1.getX() - position.x; + float dy = planet1.getY() - position.y; + float distanceToPlanet1 = (float) Math.sqrt(dx*dx + dy*dy); + if (distanceToPlanet1 < shortestDistance) { + shortestDistance = distanceToPlanet1; + } + } + } while (shortestDistance < minDistance); + } + int mass = minMass + MathUtils.random(maxMass - minMass); + int radius = minRadius + MathUtils.random(maxRadius - minRadius); + Color color = new Color((int)(Math.random() * 0x1000000)); + Planet planet = new Planet(position, mass, radius, color); + attractors.addActor(planet); + } mainStage.addActor(attractors); // players players = new Group(); - Player player1 = new Player(planet1, Color.RED); + Player player1 = new Player((Planet) attractors.getChild(0), Color.RED); players.addActor(player1); - Player player2 = new Player(planet2, Color.WHITE); + Player player2 = new Player((Planet) attractors.getChild(1), Color.WHITE); players.addActor(player2); - Player player3 = new Player(moon2_1, Color.YELLOW); + Player player3 = new Player((Planet) attractors.getChild(2), Color.YELLOW); players.addActor(player3); mainStage.addActor(players);