// Obstacle class for Verlet physics engine
// by Andreas Blixt <andreasblixt@msn.com> 2007
// Free for use. Please include this header.

Obstacle = function (world, x, y, width, height) {
	// Make sure a world was passed.
	if (!(world instanceof World)) throw "First argument must be an instance of World.";

	// Validate horizontal position.
	x = parseFloat(x);
	if (isNaN(x)) throw "Second argument must be a number.";

	// Validate vertical position.
	y = parseFloat(y);
	if (isNaN(y)) throw "Third argument must be a number.";

	// Validate horizontal dimension.
	width = parseFloat(width);
	if (isNaN(width)) throw "Fourth argument must be a number.";

	// Validate vertical dimension.
	height = parseFloat(height);
	if (isNaN(height)) throw "Fifth argument must be a number.";

	// Store a reference to the specified world which this group should belong to.
	this.World = world;

	// Position and size the obstacle.
	this.X = x;
	this.Y = y;
	this.Width = width;
	this.Height = height;

	// Get a unique ID for this obstacle.
	var idCounter = 1;
	while (this.World.GetByID(this.ID = "obstacle" + idCounter++));

	// Add this obstacle to the specified world.
	world.Obstacles.push(this);

	// Add this obstacle to the global list of objects.
	world.All.push(this);
};

Obstacle.prototype = {
	// A unique string identifying this particular obstacle.
	ID: "",

	// Coordinates of the obstacle box.
	X: NaN, Y: NaN,

	// Dimensions of the obstacle box.
	Width: NaN, Height: NaN,

	// A reference to the world which this obstacle belongs to.
	World: null,

	// Removes this obstacle.
	Remove: function () {
		for (var i in this.World.Obstacles)
			if (this.World.Obstacles[i] === this) this.World.Obstacles.splice(i, 1);
		for (var i in this.Group.World.All)
			if (this.World.All[i] === this) this.World.All.splice(i, 1);
	}
};