maggick_wiimote_glove/processing_main/processing_main.pde
import processing.serial.*;
/*arduino information*/

Serial port;
String portname = "/dev/tty.usbserial-AD01U4VO";
//String portname = "/dev/ttyBluetooth-PDA-Sync";

final color ORANGE = color(204,102,0);
final color RED = color(205,0,0);
final color GREEN = color(0,205,0);
final color WHITE = color(255,255,255);
final color BLACK = color(0,0,0);
final color BLUE = color(0,0,255);
final color YELLOW = color(255,255,0);
final color GREY = color(165,165,165);
boolean started = false;

final int W_HEIGHT = 800;
final int W_WIDTH = 1000;

int n_data  = 3;

int ax_mouse = 0;
int ay_mouse = 0;
int az_mouse = 0;

// the mouse position
int x_mouse;
int x_mouse_start;
int y_mouse;
int y_mouse_start;

// the target position
int x_target;
int y_target;

// traps
int traps_size=32;
int[][] traps= new int[traps_size][4];

// some counters
int loose =0;
int win = 0;

void setup() {
  background(WHITE);
  size(W_WIDTH,W_HEIGHT); // définition de la surface de travail
  port = new Serial(this, portname, 9600);
  port.clear();
  background(BLACK);

  generateLevel();

  println(Serial.list());
}

void draw(){
  if(started){
    /*message à arduino pour l'acquisition des données*/
    port.write(ENTER);

    /*attendre que le buffer contienne toutes les données*/
    while(!(port.available() >= n_data ));

    background(BLACK);
    drawLevel();

    fill(BLUE);
    stroke(WHITE);

    /*les données reçues vont de 0 à 180, on veut du +/-90*/
    ax_mouse = 90 - port.read();
    ay_mouse = 90 - port.read();
    az_mouse = 90 - port.read();

    /*on set le maximum pour éviter de faire des mouvements trops grands*/
    float x_lim = 40.0;
    float y_lim = 45.0;
    float z_lim = 90.0;
    if(ax_mouse > x_lim)
        ax_mouse = (int)x_lim;
    else if(ax_mouse < -x_lim)
        ax_mouse = -(int)x_lim;
    else if
        (abs(ax_mouse) < 5.0) ax_mouse = 0;

    if(ay_mouse > y_lim)
        ay_mouse = (int)y_lim;
    else if(ay_mouse < -y_lim)
        ay_mouse = -(int)y_lim;
    else if(abs(ay_mouse) < 3.0)
        ay_mouse = 0;

    if(az_mouse > z_lim)
        az_mouse = (int)z_lim;
    else if(az_mouse < -z_lim)
        az_mouse = -(int)z_lim;

    /*on opère les changements*/
    x_mouse -= (int) ((W_WIDTH / 100.0) * (ax_mouse/x_lim));
    if(x_mouse < 0)
        x_mouse = 0;
    if(x_mouse > W_WIDTH)
        x_mouse = W_WIDTH;

    y_mouse += (int) ((W_HEIGHT / 100.0) * (ay_mouse/y_lim));
    if(y_mouse < 0)
        y_mouse = 0;
    if(y_mouse > W_HEIGHT)
        y_mouse = W_HEIGHT;

    // we draw the bille
    ellipse(x_mouse,y_mouse,12,12);

    // we check the position of the bille
    int pos = isPositionInteresting();
    if (pos == 1){
        win++;
        println("YOU WIN "+ win + " times");
        x_mouse=y_mouse_start;
        y_mouse=x_mouse_start;
    }
    if (pos == -1){
        loose++;
        println("YOU LOOSE "+loose +" times");
        x_mouse=x_mouse_start;
        y_mouse=y_mouse_start;
    }
  }
}

void keyPressed(){
  // replace the mote with keyboard
  if(key == 'z' || key=='w'){
      ay_mouse=-90;
  }
  if (key == 's'){
      ay_mouse=90;
  }
  if (key == 'q' || key=='a'){
    ax_mouse=90;
  }
  if (key == 'd'){
    ax_mouse=-90;
  }

if(key == ESC){
    while(port.available()>0){
      port.read();
    }
    exit();
  }

  if(!started){
    started = true;
  }
}

void keyReleased(){
    if(key =='z' || key=='s' || key=='w'){
        ay_mouse=0;
    }

    if(key=='q' || key=='d' || key=='a'){
        ax_mouse=0;
    }
}

// check if the position is particular
int isPositionInteresting (){
    if (y_mouse==W_HEIGHT || y_mouse==0 || x_mouse==W_WIDTH || x_mouse==0){
        return -1;
    }

    if ((y_mouse<=y_target+9 && y_mouse>= y_target-9)  &&
            (x_mouse<=x_target+9 && x_mouse >= x_target-9)){
        return 1;
    }

    if (isTraped()){
        return -1;
    }

    return 0;
}

boolean isTraped(){
    for (int i=0; i<traps_size; i++){
        if (x_mouse>=traps[i][0] && x_mouse<=traps[i][0]+traps[i][2] &&
            y_mouse>=traps[i][1] && y_mouse<=traps[i][1]+traps[i][3])
            return true;
    }
    return false;
}

// generate the level
void generateLevel(){

     // target position
     x_target=W_WIDTH/2;
     y_target=W_HEIGHT/2;

     // mouse start position
     x_mouse=25;
     x_mouse_start=25;
     y_mouse=25;
     y_mouse_start=25;

     // zero square
     traps[0][0]=0;
     traps[0][1]=0;
     traps[0][2]=10;
     traps[0][3]=W_HEIGHT;

     traps[1][0]=0;
     traps[1][1]=0;
     traps[1][2]=W_WIDTH;
     traps[1][3]=10;

     traps[2][0]=W_WIDTH-10;
     traps[2][1]=0;
     traps[2][2]=10;
     traps[2][3]=W_HEIGHT;

     traps[3][0]=0;
     traps[3][1]=W_HEIGHT-10;
     traps[3][2]=W_WIDTH;
     traps[3][3]=10;

     // first square
     traps[4][0]=50;
     traps[4][1]=50;
     traps[4][2]=10;
     traps[4][3]=W_HEIGHT-100;

     traps[5][0]=50;
     traps[5][1]=50;
     traps[5][2]=W_WIDTH-100;
     traps[5][3]=10;

     traps[6][0]=W_WIDTH-60;
     traps[6][1]=50;
     traps[6][2]=10;
     traps[6][3]=W_HEIGHT-100;

     traps[7][0]=50;
     traps[7][1]=W_HEIGHT -60;
     traps[7][2]=W_WIDTH-180;
     traps[7][3]=10;

     // second square
     traps[8][0]=100;
     traps[8][1]=100;
     traps[8][2]=10;
     traps[8][3]=W_HEIGHT-280;

     traps[9][0]=100;
     traps[9][1]=100;
     traps[9][2]=W_WIDTH-200;
     traps[9][3]=10;

     traps[10][0]=W_WIDTH-110;
     traps[10][1]=100;
     traps[10][2]=10;
     traps[10][3]=W_HEIGHT-200;

     traps[11][0]=100;
     traps[11][1]=W_HEIGHT-110;
     traps[11][2]=W_WIDTH-200;
     traps[11][3]=10;

      // third square
     traps[12][0]=150;
     traps[12][1]=150;
     traps[12][2]=10;
     traps[12][3]=W_HEIGHT-300;

     traps[13][0]=150;
     traps[13][1]=150;
     traps[13][2]=W_WIDTH-380;
     traps[13][3]=10;

     traps[14][0]=W_WIDTH-160;
     traps[14][1]=150;
     traps[14][2]=10;
     traps[14][3]=W_HEIGHT-300;

     traps[15][0]=150;
     traps[15][1]=W_HEIGHT-160;
     traps[15][2]=W_WIDTH-300;
     traps[15][3]=10;

      // fourth square
     traps[16][0]=200;
     traps[16][1]=200;
     traps[16][2]=10;
     traps[16][3]=W_HEIGHT-480;

     traps[17][0]=200;
     traps[17][1]=200;
     traps[17][2]=W_WIDTH-400;
     traps[17][3]=10;

     traps[18][0]=W_WIDTH-210;
     traps[18][1]=200;
     traps[18][2]=10;
     traps[18][3]=W_HEIGHT-400;

     traps[19][0]=200;
     traps[19][1]=W_HEIGHT-210;
     traps[19][2]=W_WIDTH-400;
     traps[19][3]=10;

     // fifth square
     traps[20][0]=250;
     traps[20][1]=250;
     traps[20][2]=10;
     traps[20][3]=W_HEIGHT-500;

     traps[21][0]=330;
     traps[21][1]=250;
     traps[21][2]=W_WIDTH-580;
     traps[21][3]=10;

     traps[22][0]=W_WIDTH-260;
     traps[22][1]=250;
     traps[22][2]=10;
     traps[22][3]=W_HEIGHT-500;

     traps[23][0]=250;
     traps[23][1]=W_HEIGHT-260;
     traps[23][2]=W_WIDTH-500;
     traps[23][3]=10;

     // square six
     traps[24][0]=300;
     traps[24][1]=300;
     traps[24][2]=10;
     traps[24][3]=W_HEIGHT-600;

     traps[25][0]=300;
     traps[25][1]=300;
     traps[25][2]=W_WIDTH-600;
     traps[25][3]=10;

     traps[26][0]=W_WIDTH-310;
     traps[26][1]=300;
     traps[26][2]=10;
     traps[26][3]=W_HEIGHT-680;

     traps[27][0]=300;
     traps[27][1]=W_HEIGHT-310;
     traps[27][2]=W_WIDTH-600;
     traps[27][3]=10;

     // square seven
     traps[28][0]=350;
     traps[28][1]=430;
     traps[28][2]=10;
     traps[28][3]=W_HEIGHT-780;

     traps[29][0]=350;
     traps[29][1]=350;
     traps[29][2]=W_WIDTH-700;
     traps[29][3]=10;

     traps[30][0]=W_WIDTH-360;
     traps[30][1]=350;
     traps[30][2]=10;
     traps[30][3]=W_HEIGHT-700;

     traps[31][0]=350;
     traps[31][1]=W_HEIGHT-360;
     traps[31][2]=W_WIDTH-700;
     traps[31][3]=10;
}

// draw the level
void drawLevel(){
    fill(GREEN);
    stroke(GREEN);
    ellipse(x_target, y_target, 15,15);

    fill(WHITE);
    stroke(WHITE);
    for(int i=0; i <traps_size; i++){
        rect(traps[i][0], traps[i][1], traps[i][2], traps[i][3]);
    }
}

