// __autor__ = Juan Carlos De Abreu R.
// __date__ = 03/19/2008
// __version__ = 1.0
//
// This scene define an exercise for LAPAROS project.
// The exercise consist in touch a box every time that has a focus.
//

include("TouchColorsUtil.snf");
include("TouchColorsInstrument.snf");

nTouch = 16;                     // Number of boxes to touch.
touchTime = 4000;                // Time to touch a box.
exersiceTime = nTouch * touchTime; // Total time for do the exersice.

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

// Path for each geometry in the scene.
quadPath = "../Models/quad.obj";
simuladorPath = "../Models/Simulador3.obj";

// Load each geometry.
quadGeometry = Model(quadPath);
simuladorGeometry = Model(simuladorPath);

// Set default properties for each geometry.
quadGeometry.castShadow(false);
quadGeometry.setMaterial(grayMtl);
simuladorGeometry.castShadow(false);

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

// Generate all the quads.
quadList = (); quadNode = Group();
createGrid(quadGeometry, 8, 8, quadList, quadNode);

// 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
		),
		Group(
			translate(0.5 - 4, 0 - 0.4, -1 - 4),
			scale(1.2, 1.2, 1.2),
			quadNode
		),
		instrumentNode,
		Group(translate(1, 7, 12), Light())
	);

// Function change the quad that have the focus.
focusQuad = 0; isQuadChanged = false;
def changeQuad() {
  // Restore to default material the prev focusQuad and set
  // the focus material to the new focusQuad.
  quadList.get(focusQuad).setMaterial(grayMtl);
  focusQuad = INT(rand(0, 64));
  quadList.get(focusQuad).setMaterial(yellowMtl);
  isQuadChanged = true;
}
Timer(touchTime, -1, changeQuad(), changeQuad());

// Function to check the instrument touch the box.
def checkIfTouchTheBox() {
  if (isQuadChanged) {
	leftQuadIndex = collision(leftHeadInstrumentGeometry, quadList);
	rightQuadIndex = collision(rightHeadInstrumentGeometry, quadList);
	if ((leftQuadIndex != -1) || (rightQuadIndex != -1)) {
      if ((leftQuadIndex == focusQuad) || (rightQuadIndex == focusQuad))
	    quadList.get(focusQuad).setMaterial(greenMtl);
      else
        quadList.get(focusQuad).setMaterial(redMtl);
      isQuadChanged = false;
	}
  }
}
Timer(10, -1, checkIfTouchTheBox(), checkIfTouchTheBox());
