// MentalHangover.vtx
// Author - MrC

Texture checker;
Texture ball;
float ty;

float d = 0;
float c;

// colours
float ar,ag,ab,br,bg,bb;
float colamt,coltime;

// balls
float[] c_z(64),c_vz(64),c_sx(64),c_sy(64), c_vy(64);

void Init()
{
	checker.LoadTexture("checker_transp.png");
	ball.LoadTexture("smiley.png");
	
	ty = 0;
	coltime = 0;
	
	for (int x=0;x<64;x=x+1)
	{
      MakeNew(x);
      c_z[x] = (Rand()*6) - 2;
	  c_vy[x] = Rand()*2 - 1;
	}
}

void MakeNew(int n)
{
   c_z[n] = 4;
   c_vz[n] = Rand()* 1.0f + 1.0;
   c_vy[n] = (Rand()*1.0 + 0.5);
   c_sx[n] = (-0.5+Rand())*2.5;
   c_sy[n] = 0.0f;
}


void Sprite(float x, float y, float z, float sz)
{
   gfxTexCoord(0,0);
   gfxVertex(x-sz,y+sz,z);
   gfxTexCoord(1,0);
   gfxVertex(x+sz,y+sz,z);
   gfxTexCoord(1,1);
   gfxVertex(x+sz,y-sz,z);
   gfxTexCoord(0,1);
   gfxVertex(x-sz,y-sz,z);
}

void Render()
{
	d = (TIMEPASS*6 + (TREBLE * 4 * TIMEPASS)) * 0.25;

	ty -= d;

	if (ty > 2)
	ty -= 1.0f;

	if (ty < 2)
	ty += 1.0f;

	// Floor

	coltime = coltime - TIMEPASS;
	if (coltime<0)
	{
		coltime = 1+(Rand()*5);
		ar = br; ag = bg; ab = bb;
		colamt =  0;
		float x = (Rand()*5.0)+1.0;
		br = x%2;
		bg = ((x%4)-br)/2;
		bb = ((x%8)-(bg*2)-br)/4;
		br = br/2.0;
		bg = bg/2.0;
		bb = bb/2.0;
	}
	colamt = colamt + TIMEPASS;
	if (colamt>1) colamt=1;

	gfxRotate(-5.0,1,0,0);

	gfxSetTexture(NULL);
	gfxTranslate(0, -0.245, 2.0f);
	gfxSetBlendMode(BLEND_MOD);

	gfxBegin(PRIM_QUADLIST);
	gfxColour( ((1.0f-colamt)*ag)+(colamt*bg), ((1.0f-colamt)*ab)+(colamt*bb), ((1.0f-colamt)*ar)+(colamt*br), 1 );
	gfxVertex(-5, 0, 2.5);
	gfxVertex(5, 0, 2.5);
	gfxVertex(5, 0, -2.5);
	gfxVertex(-5, 0, -2.5);
	gfxEnd();

	gfxSetTexture(checker);
	gfxSetBlendMode(BLEND_MOD);

	gfxBegin(PRIM_QUADLIST);
	gfxColour(0, 0, 0, 0.4);
	gfxColour( ((1.0f-colamt)*ar)+(colamt*br), ((1.0f-colamt)*ag)+(colamt*bg), ((1.0f-colamt)*ab)+(colamt*bb), 1 );
	gfxTexCoord(0, 0 + ty);
	gfxVertex(-10, 0, 2.5);
	gfxTexCoord(20, 0 + ty);
	gfxVertex(10, 0, 2.5);
	gfxTexCoord(20, 5 + ty);
	gfxVertex(10, 0, -2.5);
	gfxTexCoord(0, 5 + ty);
	gfxVertex(-10, 0, -2.5);
	gfxEnd();

	gfxSetTexture(ball);

	gfxSetBlendMode(BLEND_MOD);

	gfxBegin(PRIM_QUADLIST);
	for (int x=0;x<32;x++)
	{
	
		c_z[x] = c_z[x] - (d*c_vz[x]);
		c_vy[x] = c_vy[x] + 1 * TIMEPASS*2;
		c_sy[x] = c_sy[x] - (c_vy[x] * d);
		if (c_sy[x] < 0.025)
		{
			c_sy[x] = 0.025;
			c_vy[x] = - (c_vy[x]* 0.9);
		}
		if (c_z[x]<-2)
			MakeNew(x);

		c = (7.0-(c_z[x]+2))*0.11; 
		gfxColour(1,1,1,1); 
		Sprite(c_sx[x],c_sy[x],c_z[x], 0.0350);
	} 

	gfxEnd();
	
}