//GAMELOGIC

MapColor( "#476c6c" );

function MovePatrol () {
 this.Nextthink ( this.ticrate );
 this.MovetypeGoal();
 this.scale = 0.1+this.y*0.01;
 this.needUpdate = 1;
// window.status = "x: " +this.x + "gx" + this.goalx;
 this.Update();
}

function CreateDuch( x, y, z) {
  var head = new Spawn();
  head.SetModel( model.duch );
  head.SetOrigin( x, y, z);
  head.scale = 0.1+z*0.01;
  head.Update();
  head.Think = MovePatrol;
  head.ticrate = 100;
  head.Nextthink( head.ticrate);
  head.speed = 5;
 return head.entnum;
}

var kan = CreateDuch(100,100, 100);

var UserClick = function ( x, y ) {
  // CreateDuch(x, y , y);
//  var head = getEnt( kan );
//  head.SetGoal( x, y, 0 );
   getEnt( kan ).SetGoal( x, y, 0 );
}


function CreateBanana( x, y, z) {
  var head = new Spawn();
//  head.SetModel( model.banana );
  head.SetModel( model.duch );
  head.SetOrigin( x, y, z);
  head.scale = 10;
  head.Update();
}


function CreateDuch( x, y, z) {
  var head = new Spawn();
//  head.SetModel( model.duch );
  head.SetModel( model.banana );
  head.SetOrigin( x, y, z);
  head.scale = 0.1+z*0.01;
  head.Update();
}

{ var x,y,z;
 for(var t=0;t<150;t++) { 
   x =  rand(900) - 100;
   y = 100 + rand(300); 
   z = y;

   
   CreateDuch(x , y, z);
   //CreateBanana(x , y, z);
 }
}

CreateBanana( 400,250, 250 );


MapModel( map.grass );


function MovePatrol () {

 this.Nextthink ( this.ticrate );
 this.MovetypeGoal();
}

function ShadowMove() {
  var o =getEnt( this.owner );
  this.SetOrigin( o.x + o.z /8 , o.y + o.z/8, ZGROUND);
  this.Nextthink ( this.ticrate );
  this.MovetypeTeleport();
}


function CopterArrivedLocation (){
 this.SetGoal(  rand(800) ,10+ rand(600) ,0 );
}

function Sub_Null(){
 alert("hi!");
}

function CreateCopter() {
 var head = new Spawn();
 
 head.SetModel( model.copter );
 head.scale = 0.9;
 head.SetOrigin(10 + rand(800) , rand(600) , ZCLOUDS);
 head.SetSpeed( 2 );
 head.SetGoal(  rand(800) ,10+ rand(600) ,0 );
 
 head.Think = MovePatrol
 head.Touch = CopterArrivedLocation;
 head.ticrate = 10;
 head.Nextthink( head.ticrate ); 
 head.speed = 5;
 
 var shadow = new Spawn(); 

 shadow.SetModel( model.copter.shadow );
 shadow.owner = head.entnum;
 shadow.scale = 1;
 shadow.alpha = 0.3;
 shadow.ticrate = 10;
 shadow.Think = ShadowMove;
 shadow.Touch= Sub_Null;
 shadow.Nextthink(shadow.ticrate);


}

function CloudMove () {
 this.Nextthink (100);
 this.MovetypeGoal();
}

function CloudRestart() {
 this.SetOrigin( rand(800) , rand(600) +600, ZCLOUDS+1);
 this.SetGoal(  rand(800) , -100 , 0 ); 
}

function CreateCloud() {
 var head = new Spawn();
 
 head.SetModel( model.cloud );
 head.scale = 1;
 head.SetOrigin( rand(800) , rand(600) +100, ZCLOUDS+1);
 head.SetSpeed( 1 );
 head.SetGoal(  rand(800) , -100 , 0 ); 
 head.Think = CloudMove;
 head.Touch = CloudRestart;
 head.Nextthink(100); 
 head.speed = 5;

}


function GetClip ( ofx, ofy ) {
  //rect (top, right, bottom, left)
  var dx = ofx*16;
  var dy = ofy*16;

  return "rect(" +  //shape
           dy + "px, " +  //top
           (dx+16)+"px," + //right
           (dy+16) + "px," + //bottom
           dx + "px" + //left
          ")";
}


CreateCloud();
/*CreateCloud();
CreateCloud();*/
//CreateCopter()
/*

var area = document.createElement("img");  
area.setAttribute("src", model.nhtiles.model );
//area.style.border = "1px solid green";
area.style.position = "absolute";
area.style.top = "40px";
area.style.left = "40px";

//area.style.clip = "rect(0px, 16px, 16px, 0px)";


var clip = GetClip( 3, 2 );;
area.style.clip = clip;

//area.style.MozImageRegion = "rect(0px, 16px, 16px, 0px)";
//area.style.MozImageRegion = "rect(0px, 16px, 16px, 0px) !important";

root.appendChild( area );

var area = document.createElement("img");  
area.setAttribute("src", model.nhtiles.model );
area.style.position = "absolute";
area.style.top = "90px";
area.style.left = "90px";
area.width *= 2;
area.height *= 2;

root.appendChild( area );

//CreateCopter();

//CreateCloud();


*/


