#ifndef __ASTEROIDSFIELD__
#define __ASTEROIDSFIELD__

#include <stdlib.h>
#include "Vec3f.h"
#include <vector>

#define PI 3.14159265

#define Z_VALUE_DISAPPEAR 0.0f
#define Z_VALUE_APPEAR 500.0f
#define XY_DISAPPEAR_RANGE 3
#define MAX_MOVING_SPEED 0.2
#define ASTEROID_COLLISION_TEST_RADIUS 30

#define CHUNK_SPEED_MAX 0.4
#define CHUNK_SPEED_MIN 0.2
#define CHUNK_NUMBER 30

#define PLAYER_COLLISION_IMUN_TICKS 100



class Stone;
class Asteroid;
class Material;
class AABoundBox;
class TextureObject;
class Billboard;
class Camera;
class Player;

class AsteroidsField
{
public:
	AsteroidsField(	Vec3f position, float width, float height, float depth,
					int number,int variations, float sizeMax, float sizeMin,
					Material* material, TextureObject* texture = NULL, Camera* cam = NULL);

	virtual ~AsteroidsField();

	void setPlayerSpeed(float speed);

	std::vector<Stone*> getStones();

	void renderBeforePlayer();

	void renderBehindPlayer();

	void update( Player* player);

	bool collisionTestWithPlayer(AABoundBox* tested, float zMin, float zMax);

	void asteroidDestruction( Asteroid* destroyed);

	int findAsteroidIndexAtZ( float zValue );

protected:

	int findAsteroidIndexAtZRek( float zValue, int lower, int uppper);
	int asteroidIndexNearPlayer;

	Vec3f calcPos(float radius);

	Material* material;
	TextureObject* texture;

	std::vector<Stone*> stones;

	std::vector<Asteroid*> asteroids;
	std::vector<Asteroid*> chunks;

	static int comparatorZpos ( const void * elem1, const void * elem2 );

	void asteroidCollision(unsigned int fieldIndex);
	std::vector<bool> asteroidCollisionTested;

	Vec3f position;
	float width;
	float height;
	float depth;

	// Asteroidenfeldgrenzen
	float front;
	float back;
	float left;
	float right;
	float top;
	float bottom;

	int number;
	int variations;
	float sizeMax;
	float sizeMin;

	float playerSpeed;

	float playerPos;

	int playerImunTicks;

	Billboard* explosion;
	Camera* cam;

};

#endif __ASTEROIDSFIELD__