/**
 * Created by Jared Rewerts on 3/7/2016.
 */
"use strict";

var Physics = require("physicsjs");

/**
 * @param {{numberPlayers: number,
 * arenaRadius: number}} config
 * @constructor
 */
var Arena = function(config) {
    this.numberPlayers = config.numberPlayers;
    this.arenaRadius = config.arenaRadius;

    this.goals = [];
    this.bumpers = [];
    this.points = [];

    var midX = this.arenaRadius;
    var midY = this.arenaRadius;

    var theta = 2* Math.PI / this.numberPlayers;
    var thetaOffset = (3/2) * Math.PI - theta / 2;

    for (var i = 0; i < this.numberPlayers; i++) {
        this.points.push(Arena.getCoordinates(theta * i + thetaOffset, this.arenaRadius, midX, midY));
    }

    // Build the circular bumpers. These won't have any special collision detection.
    this.bumpers = [];

    for (var j = 0; j < this.numberPlayers; j++) {
        this.bumpers.push(
            Physics.body('circle', {
                x: this.points[j].x,
                y: this.points[j].y,
                radius: 45,
                treatment: 'static'
            })
        );

        // Create the goals.
        if (j < this.numberPlayers - 1) {
            var xCenter = (this.points[j].x - this.points[j + 1].x) / 2;
            var yCenter = (this.points[j].y - this.points[j + 1].y) / 2;

            this.goals.push(Physics.body('rectangle', {
                x: xCenter,
                y: yCenter,
                angle: theta * j,
                width: this.points[j].x - this.points[j + 1].x,
                height: 20,
                mass: 20
            }));
        }
    }
};

/**
 * Gets the coordinates of a given position.
 * Used internally to get locations for bumpers and goals.
 * @param theta: number
 * @param radius: number
 * @param midX: number
 * @param midY: number
 * @returns {*|Physics.vector}
 */
Arena.getCoordinates = function(theta, radius, midX, midY) {

    var x = midX + radius * Math.cos(theta);
    var y = midY - radius * Math.sin(theta);

    return new Physics.vector(x, y);
};

/**
 * This returns the location of the players score interface in arena physical coordinates.
 * This will be used to draw the HUD. UNIMPLEMENTED
 * @param playerId: number
 * @returns {position: Physics.vector}
 */
Arena.prototype.getScorePosition = function(playerId) {
    var theta = 2* Math.PI / this.numberPlayers;
    var thetaOffset = (3/4) * Math.PI - theta / 2;

    var midX = this.getCenter().x;
    var midY = this.getCenter().y;

    return Arena.getCoordinates(theta * playerId + thetaOffset, this.arenaRadius, midX, midY);
};

/**
 * Gets the center of the arena.
 * @returns {center: Physics.vector}
 */
Arena.prototype.getCenter = function() {
    return new Physics.vector(this.radius, this.radius);
};

/**
 * Grabs a bumper. The index should be that players left bumper.
 * @param index: number
 * @returns {bumper: Physics.body}
 */
Arena.prototype.getBumper = function(index) {
    return this.bumpers[index];
};

/**
 * Returns the players goal given by index.
 * @param index: number
 * @returns {goal: Physics.body}
 */
Arena.prototype.getGoal = function(index) {
    return this.goals[index];
};

module.exports = Arena;
@@ -94,16 +94,17 @@
 sjs%22);%0A%0A
+%0A
 /**%0A * @
@@ -1831,10 +1831,8 @@
 ns %7B
-*%7C
 Phys
@@ -2232,18 +2232,8 @@
 ns %7B
-position: 
 Phys
@@ -2629,16 +2629,8 @@
 ns %7B
-center: 
 Phys
@@ -2717,25 +2717,30 @@
 vector(this.
-r
+arenaR
 adius, this.
@@ -2739,17 +2739,22 @@
 s, this.
-r
+arenaR
 adius);%0A
@@ -2867,16 +2867,8 @@
 ns %7B
-bumper: 
 Phys
@@ -3052,14 +3052,8 @@
 ns %7B
-goal: 
 Phys
@@ -3140,20 +3140,588 @@
 index%5D;%0A
-
 %7D;%0A%0A
+/**%0A * Get an array containing all of the bumpers in the arena.%0A * @return %7BPhysics.body%5B%5D%7D%0A */%0AArena.prototype.getBumpers = function () %7B%0A    return Array.apply(undefined, this.bumpers);%0A%7D;%0A%0A/**%0A * Get an array containing all of the goals in the arena.%0A * @return %7BPhysics.body%5B%5D%7D%0A */%0AArena.prototype.getGoals = function () %7B%0A    return Array.apply(undefined, this.goals);%0A%7D;%0A%0A/**%0A * @external Physics.vector See http://wellcaffeinated.net/PhysicsJS/docs/#Physics-vector%0A */%0A%0A/**%0A * @external Physics.body http://wellcaffeinated.net/PhysicsJS/docs/#Physics-body%0A */%0A%0A
 module.e

fix some syntax errors in Arena. Add physics body bulk getters.