/*
 * Triangle.java
 *
 * Triangle-shaped model to support collisions.
 *
 * This shows how to work with a single, simple triangle.
 *
 * @author Walker M. White
 * @version 3/9/2025
 */
package edu.cornell.cis3152.box2d.shape;

import com.badlogic.gdx.math.*;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.physics.box2d.*;

import edu.cornell.cis3152.box2d.*;  // For Entity
import edu.cornell.gdiac.math.Poly2;
import edu.cornell.gdiac.math.PolyFactory;

/**
 * A triangular physics object.
 */
public class Triangle extends Entity {

	/** Shape information for this box */
	protected PolygonShape shape;

	/**
	 * Creates a new triangle, but do not initialize it yet.
	 */
	public Triangle() { }

	/**
	 * Returns an array of vertices representing an equilateral triangle
	 *
	 * This array is used for both physics AND drawing.
	 *
	 * @return an array of vertices representing an equilateral triangle
	 */
	private float[] makeTriangle(Vector2 size) {
		float[] vertices = new float[6];
		float altitude  = (float)Math.sqrt(3) * size.y / 2.0f;
		float halfWidth = size.x / 2.0f;

		vertices[0] = -halfWidth;
		vertices[1] = altitude / 3.0f;
		vertices[2] = 0.0f;
		vertices[3] = -2.0f * altitude / 3.0f;
		vertices[4] = halfWidth;
		vertices[5] = altitude / 3.0f;

		return vertices;
	}

	/**
     * Creates the collision shape information
     *
     * @param size The object bounding box
     */
	protected void makeFixture(Vector2 size) {
		shape = new PolygonShape();
		shape.set(makeTriangle(size));

		// Create the fixture
		FixtureDef def = new FixtureDef();
		def.density = density;
		def.friction = friction;
		def.restitution = restitution;
		def.shape = shape;

		fixture = body.createFixture(def);
		shape.dispose(); // Do not need it anymore
	}

	/**
     * Creates the drawing shape information
     *
     * @param size  The object bounding box
     */
	protected void makeGraphics(Vector2 size) {
		poly = new Poly2(makeTriangle(size));
        poly.scl( units );

		// Indices are trivial
        poly.indices.ensureCapacity( 3 );
        poly.indices.items[0] = 0;
        poly.indices.items[1] = 1;
        poly.indices.items[2] = 2;
        poly.indices.size = 3;
	}
}
