// __autor__ = Juan Carlos De Abreu R.
// __date__ = 03/19/2008
// __version__ = 1.0
//
// This file define an exercise for LAPAROS project.
// The exercise consist in pass the instrument ....
//

include("arosInstrument.snf");

exersiceTime = 15000; // Time for do the exersice (15 secs).

// Load a material file with the basic colors and get 
// the colors to use in the exersice.
Material("../Models/Basic.mtl");
redMtl = getMaterial("Red");
blueMtl = getMaterial("Blue");
greenMtl = getMaterial("Green");
yellowMtl = getMaterial("Yellow");

// Path for each geometry in the scene.
spherePath = "../Models/sphere.obj";
simuladorPath = "../Models/Simulador3.obj";
arosPath = "../Models/aros.obj";
cubePath = "../Models/cube.obj";

// Load each geometry.
sphereGeometry = Model(spherePath);
simuladorGeometry = Model(simuladorPath);
arosGeometry = Model(arosPath);
cubeGeometry = Model(cubePath);

// Set default properties for each geometry.
sphereGeometry.draw(false);
cubeGeometry.setMaterial(greenMtl);
simuladorGeometry.castShadow(false);

// Set the camera position.
camera = Camera();
camera.setActive();
camera.setPos(0, 8, 10);

arosList = (); sphereList = ();
arosNode = Group();
buildArosNode(sphereList, arosList, arosNode);

// Set the initial specification of the scene.
scene =
	Group(
		Group(
			translate(0, 4.2, 0),
			rotate(-90, 1, 0, 0),
			scale(25, 25, 25),
			simuladorGeometry
		),
		instrumentNode,
		Group(
			translate(1.5, -0.5, 0),
			arosNode
		),
		Group(
			translate(-5, -0.6, 0),
			scale(0.8, 0.8, 0.8),
			cubeGeometry
		),
		Group(translate(1, 7, 12), Light())
	);

// Function to build the aros node.
def buildArosNode(gemetryCenterList, geometryList, node) {
   for (i = 0; i < 5; ++i) {
     newGeometry = clone(arosGeometry);
	 newCenterGeometry = clone(sphereGeometry);
	 geometryList.add(newGeometry);
	 gemetryCenterList.add(newCenterGeometry);
	 node.add( 
		Group(
			translate(-i*1.2, -2 -i*0.8, 0),
			Group(
				scale(15, 15, 15), 
				newGeometry
			),
			Group(
				translate(0, 6.75, 0),
				scale(1.2, 1.2, 1.2),
				newCenterGeometry
			)
		)
	 );
   }
}

// Function to check if the isntrument is inside the aros...
def checkInstrumentInside() {
  nSpheres = sphereList.size(); nCorrect = 0;
  for (i = 0; i < nSpheres; ++i) {
    if (collisionDetection(sphereList.get(i), bodyInstrumentGeometry)) {
	  arosList.get(i).setMaterial(blueMtl); nCorrect++;
	}
	else {
	  arosList.get(i).setMaterial(redMtl);
	}
  }
  
  if ((nCorrect == nSpheres) && 
      (collisionDetection(cubeGeometry, leftHeadInstrumentGeometry) ||
	   collisionDetection(cubeGeometry, rightHeadInstrumentGeometry))
	 ) {
    cubeGeometry.setMaterial(greenMtl); // Finish then...
  }
}
Timer(10, -1, checkInstrumentInside(), checkInstrumentInside());
