import processing.core.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class phoneme extends PApplet {String lines[];
String gun = "";
int linecount;
invader i ;
HashSet invaders = new HashSet();
HashSet deads = new HashSet();
HashSet killers = new HashSet();
float counter = 0;

float timeToNext = 200;
float timer = timeToNext;

public void setup(){
  size(400,400);
    textFont(loadFont("Arial-Black-14.vlw"),14);
  lines = loadStrings("words.txt"); 
  linecount = lines.length;

frameRate(30);

  
 
}

boolean virgin = true;

public void mousePressed(){
  virgin = false;
 if(lives <= 0){
    lives = 3;
   timeToNext = 200;
   timer = timeToNext;
 invaders = new HashSet();
 deads = new HashSet();
 killers = new HashSet();
counter = 0;
invader newinv = new invader();
    invaders.add(newinv);
 } 
  score = 0;
}



int score = 0;
int lives = 0;



public void draw(){
 counter++; 
  background(128);

if(virgin){
  fill(255);
   text("click to play",10,100);
   text("PHONEME INVADERS",80,150); 
      text("type the word that is sounded out",100,200); 
            text("before it destroys your planet",100,220); 
                  text("or whatever",100,240); 
}

  fill(0);
   text(gun,10,390);
   

fill(255);
  Iterator i = invaders.iterator();

  while(i.hasNext()){
     invader inv = (invader)i.next();

    if(lives > 0) {inv.move();}
     inv.show();
          if(inv.y > 375){
        lives--;
        i.remove();
killers.add(inv);        
     } 
     
  }
  
   i = killers.iterator();
  while(i.hasNext()){
  fill(255);
    invader inv = (invader)i.next();
     inv.show();
fill(128,0,0);
    inv.killershow();
  }
  
fill(0);   
   i = deads.iterator();
  while(i.hasNext()){
     invader inv = (invader)i.next();
     inv.deadmove();
     if(inv.y > 400){
       //println("deader");
        i.remove(); 
     }
     inv.show();
  }
  
  timer--;
    if(timer == 0){
           if(timeToNext > 50) {timeToNext-= 10;}
            timer = timeToNext;
     invaders.add(new invader()); 

   }
   
   line(0,350,400,350);
      text("score:"+score+" lives:"+lives,200,390);

  if(invaders.size() == 0){
     invaders.add(new invader()); 
  } 
}

public void tryToKill(String s){
     if(lives>0){
      
       Iterator i = invaders.iterator();
  while(i.hasNext()){
     invader inv = (invader)i.next();
     if(inv.word.equals(s)){
       i.remove();
       inv.kill();
      deads.add(inv); 
       //println("kill"); 
       score++;
      
     }
  }
     
  }
  
}


public void keyPressed(){
  if(keyCode == ENTER || keyCode == RETURN){
      tryToKill(gun);
     gun = "";
     return;
  }  if(keyCode == BACKSPACE || keyCode == DELETE){
      if(gun.length() > 0){
         gun = gun.substring(0,gun.length()-1); 
      }
    
    return;
  }
  if(key >= 'a' && key <= 'z'){
    gun += key;

  }
}



class invader{
  String word; String[] parts;

  float x ,y;
  float xs[];float xp[];
float fat;
float speed;
  invader(){
   String line = lines[PApplet.parseInt(random(linecount))] ;
     word = digWord(line);
     //println(word);

     parts = digParts(line);


          xp = new float[parts.length];
          calcfat();
          x = random(400 - fat);
              for(int i = 0 ; i < parts.length; i++){
                 xp[i] += x; 
              }
          y = 0;
          speed = 1 + random(.3f);
  }
 
  public void move(){
     y += speed; 
  }
  public void calcfat(){
    fat = 0;
    for(int i = 0 ; i < parts.length; i++){
      xp[i] = fat;
      fat += textWidth(parts[i])+5;
    }
    //println("fat is "+fat);
  }
  
  public void killershow(){
   text(word,x,y); 
  }
  
  public void kill(){
     speed -= 1; 
          xs = new float[parts.length];
         for(int i = 0; i < parts.length; i++){
           xs[i] = random(-.5f,.5f);  
         }
         

  }
  
  public void deadmove(){
  for(int i = 0; i < parts.length; i++){
     xp[i] += xs[i];
  } 
    speed += .1f; 
     y += speed;
  }
  
  public void show(){  

    for(int i = 0 ; i < parts.length; i++){
      
     text(parts[i],xp[i],y); 

    }
    
  }
  
  
  public String digWord(String full){
  String[] tabsplit = full.split("\t");
    return tabsplit[0];
}


public String [] digParts(String full){
  String[] tabsplit = full.split("\t");
  String[] spacesplit = tabsplit[1].split(" ");
  return spacesplit;
}

}
static public void main(String args[]) {   PApplet.main(new String[] { "phoneme" });}}