// __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("followPathInstrument.snf");
include("TouchColorsUtil.snf");
include("camera.snf");

// 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");
blackMtl = getMaterial("Black");

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

// Load each geometry.
lineGeometry = Model(linePath);
quadGeometry = Model(quadPath);
sphereGeometry = Model(spherePath);
simuladorGeometry = Model(simuladorPath);

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

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

linesNode = Group(); linesList = ();
buildLineGrid(linesList, linesNode);

// 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(-4.1, -0.4, 3.4),
			rotate(90, 1, 0, 0),
			scale(1.2, 1.2, 1.2),
			linesNode
		),
		Group(
			translate(-4.1, -0.4, 4),
			scale(0.3, 0.3, 0.3),
			sphereGeometry
		),
		Group(translate(1, 7, 12), Light())
	);

// Function to ...
def buildLineGrid(geometryList, node) {
  for (j = 0; j < 8; ++j) {
    for (i = 0; i < 8; ++i) {
	  newLine = clone(lineGeometry);
	  geometryList.add(newLine);
      node.add( Group(
					translate(i, -j, 0),
					newLine
				) );
      newLine = clone(lineGeometry);
	  geometryList.add(newLine);
	  node.add( Group(
					translate(i + 0.5, -j  + 0.5, 0),
					rotate(90, 0, 0, 1),
					newLine
				) );
    }
	newLine = clone(lineGeometry);
	geometryList.add(newLine);  
    node.add( Group(
				translate(8, -j, 0),
				newLine
			  ) );
  }
  for (i = 0; i < 8; ++i) {
    newLine = clone(lineGeometry);
	geometryList.add(newLine);
    node.add( Group(
				translate(i + 0.5, -7.5, 0),
				rotate(90, 0, 0, 1),
				newLine
			  ) );
  }
}

lines = ((1, 0), (2, 0), (2, 1), (3, 2), (4, 2), (5, 3), (7, 3),
         (8, 3), (7, 4), (6, 4), (6, 5), (7, 6), (8, 6), (8, 7));
setLine();

// Function to set the line for the exersice
def setLine() {
  nLines = lines.size();
  for (i = 0; i < nLines; ++i) {
    linei = lines.get(i);
	posX = linei.get(0);
	posY = linei.get(1);
	geometry = linesList.get(posY*17 + posX);
	geometry.setMaterial(redMtl);
  }
}

// NOTE: Hay un error que es que se borra las listas globales cuando salen
// de las funciones

actualDetect = 0; // ...
// 
def detect() {
  actualLine = lines.get(actualDetect);
  posX = actualLine.get(0);
  posY = actualLine.get(1);
  geometry = linesList.get(posY*17 + posX);
  if (collisionDetection(geometry, leftHeadInstrumentGeometry) ||
      collisionDetection(geometry, rightHeadInstrumentGeometry)) {
    geometry.setMaterial(greenMtl);
	actualDetect++;
  }
}
Timer(10, -1, detect(), detect());


/*
startPos = (0,0);
endPos = (8,8);
path = (startPos);

actualPos = (0,0);
actualDirection = 2;

// Def for the direction of the next step.
leftStep = 0; upStep = 1; rightStep = 2; downStep = 3;

// Function to add a new pos.
def addPos(stepDirection) {
  
  if (actualDirection == 0) actualDirection = (stepDirection + 3) % 4;
  if (leftStep)
  if (leftStep)
  if (leftStep)
}
  
// Function to create a random Path.
def randomPath(path) {
 
}
*/

