package MyCanvas.GameCanvas;
import javax.media.j3d.*;                   //Java3d

import MyMain.TextData;

import com.sun.j3d.utils.geometry.*;        //Primitive

public class GameTimer  implements Runnable{
	
	Thread thread;
	TransformGroup tfg;
	public static int time = 0;
	public static int clearTime = 0;
	private static boolean stop;
	public GameTimer(TransformGroup tfg){
		this.tfg = tfg;
	    
		stop = true;
    	
		thread = new Thread(this);
    	thread.start();
	}
	
	public void timerStart(){
	    MyCanvas.GameCanvas.CpLock.timerLock.lock();
    	stop = false;
    	time = 0;
    	clearTime = 0;
	    MyCanvas.GameCanvas.CpLock.timerLock.unlock();
	}
	
	public void timerStop(){
	
	    MyCanvas.GameCanvas.CpLock.timerLock.lock();
		stop = true;
		clearTime = time;
		time = 0;
	    MyCanvas.GameCanvas.CpLock.timerLock.unlock();
		
	}

	public void run(){
		
		time = 0;
		
		while(true){
		
		    MyCanvas.GameCanvas.CpLock.timerLock.lock();
		    Boolean _stop = stop;
		    MyCanvas.GameCanvas.CpLock.timerLock.unlock();
		    
			if (_stop == true){
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO 自動生成された catch ブロック
					e.printStackTrace();
				}
				continue;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO 自動生成された catch ブロック
				e.printStackTrace();
			}
			
		    MyCanvas.GameCanvas.CpLock.timerLock.lock();
		    
			time += 1;
			

			
			String timeStr = String.valueOf(time);
	    	((Text2D)(tfg.getChild(0))).setString(TextData.GAME_TIME2 + timeStr);
	    	
		    MyCanvas.GameCanvas.CpLock.timerLock.unlock();
			
		}
	}
}
