package xaero.rotatenjump.game.graphics.model;

/**
 * Created by xaero on 18/08/2017.
 */

public class NinjaStar extends Model {

    public NinjaStar(float size){
        float[] vertexArray = new float[8 * 3 * VERTEX_SIZE];//repeating vertices to set normals
        short[] indexArray = new short[8 * 3];
        //center point
        float starFatness = 5f;
        //others
        float prevX = 0.4f * size * (float)Math.cos((2f * Math.PI / 8) * 7);//i = 7
        float prevY = 0.4f * size * (float)Math.sin((2f * Math.PI / 8) * 7);//i = 7
        for(int i = 0; i < 8; i++){
            float angle = (float) ((2f * Math.PI / 8) * i);
            float currentX = (i % 2 != 0 ? 0.4f : 1f) * size * (float)Math.cos(angle);
            float currentY = (i % 2 != 0 ? 0.4f : 1f) * size * (float)Math.sin(angle);
            //center vertex
            vertexArray[3 * VERTEX_SIZE * i] = vertexArray[3 * VERTEX_SIZE * i + 1] = 0;
            vertexArray[3 * VERTEX_SIZE * i + 2] = starFatness;
            //current vertex
            vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE] = currentX;
            vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE + 1] = currentY;
            //previous vertex duplicate
            vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE * 2] = prevX;
            vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE * 2 + 1] = prevY;
            //normals calculated with vector cross product of vector center -> prev and center -> current
            float normalX = (prevY - currentY) * -starFatness;
            float normalY = (currentX - prevX) * -starFatness;
            float normalZ = prevX * currentY - prevY * currentX;
            for(int j = 0; j < 3; j++) {
                vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE * j + 5] = normalX;
                vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE * j + 6] = normalY;
                vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE * j + 7] = normalZ;
                //colours
                vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE*j + 8] =
                        vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE*j + 9] =
                                vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE*j + 10] = vertexArray[3 * VERTEX_SIZE * i + VERTEX_SIZE*j + 11] = 1;//white
            }
            prevX = currentX;
            prevY = currentY;
            //end
            //positionArray[VERTEX_SIZE * i + 2] = 0;//0 by default, so not needed
            indexArray[3 * i] = (short)(3 * i);//center
            indexArray[3 * i + 1] = (short)(3 * i + 2);//previous
            indexArray[3 * i + 2] = (short)(3 * i + 1);//current
        }
        create(vertexArray, indexArray);
    }

}
