From e6356e4210e021b9c5e6d8096fe0517ad6b754c2 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Mon, 27 Jan 2020 14:26:01 +0400 Subject: [PATCH] Update AsteroidFactor to use rand from Constants Made its members private, small changes to generation method. --- .../src/com/me/asteroids/AsteroidFactory.java | 23 +++++++++---------- core/src/com/me/asteroids/Constants.java | 6 +++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/core/src/com/me/asteroids/AsteroidFactory.java b/core/src/com/me/asteroids/AsteroidFactory.java index e8ec6b9..e6473d9 100644 --- a/core/src/com/me/asteroids/AsteroidFactory.java +++ b/core/src/com/me/asteroids/AsteroidFactory.java @@ -3,19 +3,18 @@ package com.me.asteroids; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Polygon; import com.badlogic.gdx.math.Vector2; -import com.me.common.Random; + +import static com.me.asteroids.Constants.rand; public final class AsteroidFactory { - public static final Random rand = new Random(); + private int vertexCount; + private float size; - int vertexCount; - float size; + private float sizeVariation; + private float angleVariation; - float sizeVariation; - float angleVariation; - - boolean sizeRelativeToLast; + private boolean sizeRelativeToLast; public AsteroidFactory setVertexCount(int vertexCount) { this.vertexCount = vertexCount; @@ -97,20 +96,20 @@ public final class AsteroidFactory { // Pick a random starting angle float startAngle = rand.nextFloat() * MathUtils.PI2; - Vector2 dir = new Vector2(MathUtils.cos(startAngle), MathUtils.sin(startAngle)); + Vector2 dir = Utils.setUnitVectorAngleRad(new Vector2(), startAngle); float lastSize = size; float[] vertices = new float[vertexCount * 2]; - for (int i = 0; i < vertexCount; i++) { + for (int i = 0; i < vertices.length; i += 2) { Vector2 vertex = dir.cpy(); vertex = applyAngleVariation(vertex); lastSize = applySizeVariation(lastSize); vertex.scl(lastSize); - vertices[i * 2] = vertex.x; - vertices[(i * 2) + 1] = vertex.y; + vertices[i] = vertex.x; + vertices[i + 1] = vertex.y; dir.rotateRad(angleStep); } diff --git a/core/src/com/me/asteroids/Constants.java b/core/src/com/me/asteroids/Constants.java index 1bd5a2f..3cee301 100644 --- a/core/src/com/me/asteroids/Constants.java +++ b/core/src/com/me/asteroids/Constants.java @@ -1,13 +1,19 @@ package com.me.asteroids; +import com.me.common.Random; + public class Constants { public static final boolean DEBUG = false; + public static final Random rand = new Random(); + public static final int WIDTH = 800; public static final int HEIGHT = 600; public static final int HALF_WIDTH = WIDTH / 2; public static final int HALF_HEIGHT = HEIGHT / 2; + public static final float ASTEROID_SPAWN_DELAY = 1f; + }