package com.udacity.gamedev.starfield;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;

/**
 * TODO: Start here!
 *
 * In this exercise we'll draw a star field of white points on a black background. The number of
 * points will be defined by a density parameter that states what proportion of the pixels should be
 * white.
 *
 * TODO: Run what you've got before making any changes
 *
 * One thing to note is we're using two new LibGDX classes, Array, and Vector2. We're using a custom
 * Array type so LibGDX can control the memory, and avoid unfortunate garbage collection events.
 * Vector2 is a super simple class for holding a 2D position. You can find more information in the
 * LibGDX Javadocs, or just by right clicking on the class name, and selecting Go To > Declaration.
 *
 * One new utility class we'll be using in this exercise is com.badlogic.gdx.math.Vector2. You can
 * find more information in the LibGDX Javadocs.
 *
 * Remember you can set up a Desktop run configuration using the dropdown in the toolbar, or you can
 * open the terminal at the bottom of the screen and run
 *
 * $ ./gradlew desktop:run
 */


public class Starfield extends ApplicationAdapter {

    private static final float STAR_DENSITY = 0.01f;
    ShapeRenderer shapeRenderer;
    Array<Vector2> stars;

    @Override
    public void create() {
        // TODO: Initialize a shapeRenderer

        // TODO: Call initStars

    }

    public void initStars(float density) {
        // TODO: Figure out how many stars to draw. You'll need the screen dimensions, which you can get using Gdx.graphics.getWidth() and Gdx.graphics.getHeight().

        // TODO: Create a new array of Vector2's to hold the star positions

        // TODO: Use java.util.Random to fill the array of star positions

    }

    @Override
    public void resize(int width, int height) {
        initStars(STAR_DENSITY);
        shapeRenderer = new ShapeRenderer();
    }

    @Override
    public void render() {
        // TODO: Make the night sky black
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // TODO: Begin a shapeRenderer batch using ShapeType.Point

        // TODO: Loop through the star positions and use shapeRenderer to draw points

        // TODO: End the shapeRenderer batch

    }

    @Override
    public void dispose() {
        // TODO: Dispose of our ShapeRenderer

        super.dispose();
    }
}

@@ -277,16 +277,42 @@
 Array;%0A%0A
+import java.util.Random;%0A%0A
 /**%0A * T
@@ -1588,32 +1588,76 @@
 a shapeRenderer%0A
+        shapeRenderer = new ShapeRenderer();
 %0A        // TODO
@@ -1673,16 +1673,48 @@
 itStars%0A
+        initStars(STAR_DENSITY);
 %0A    %7D%0A%0A
@@ -1921,16 +1921,195 @@
 ight().%0A
+        int screenWidth = Gdx.graphics.getWidth();%0A        int screenHeight = Gdx.graphics.getHeight();%0A        int numOfStarsToDraw = (int)(screenWidth * screenHeight * density);
 %0A       
@@ -2173,24 +2173,77 @@
 r positions%0A
+        stars = new Array%3CVector2%3E(numOfStarsToDraw);
 %0A        // 
@@ -2305,16 +2305,257 @@
 sitions%0A
+        Random random = new Random();%0A        for (int i = 0; i %3C numOfStarsToDraw; i++) %7B%0A            int x = random.nextInt(screenWidth);%0A            int y = random.nextInt(screenHeight);%0A            stars.add(new Vector2(x, y));%0A        %7D
 %0A    %7D%0A%0A
@@ -2812,17 +2812,17 @@
 arColor(
-1
+0
 , 0, 0, 
@@ -2943,16 +2943,76 @@
 e.Point%0A
+        shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
 %0A       
@@ -3090,16 +3090,172 @@
  points%0A
+        for (Vector2 star : stars) %7B%0A            //shapeRenderer.point(star.x, star.y, 0);%0A            shapeRenderer.circle(star.x, star.y, 6.0f);%0A        %7D
 %0A       
@@ -3292,16 +3292,44 @@
 r batch%0A
+        shapeRenderer.end();
 %0A    %7D%0A%0A
@@ -3416,16 +3416,48 @@
 enderer%0A
+        shapeRenderer.dispose();
 %0A       

Complete exercise 1.3.02