////////////////////////////////////
//
// Engine Source
//


var root = document.getElementById("root");

root.style.overflow = "hidden";



function InitEntine() {};

/*========= Heights ==========*/

var ZGROUND = 100;
var ZTANKS  = 200;
var ZCLOUDS = 400;
var ZSPACE = 500;

var TRUE = 1;
var FALSE = null;

/*========= Heights ==========*/



/*========= Models ==========*/


var map = new Object();
var model = new Object();


model.banana = {
  model: "banana.gif",
  sx: 128,  sy: 148,
}

model.duch = {
  model: "duch.png",
  sx: 32,  sy: 32,
}


model.ie = {
  model: "ie.gif",
  sx: 32,  sy: 32,
}

model.particle = {
  model: "particle.png",
  sx: 32,  sy: 32,
}

model.copter = {
  model: "copter.png",
  sx: 148,  sy: 116,
  shadow: {
  model: "copter-shadow.png",
  sx: 148,  sy: 116,
 },
}

model.cloud = {
  model: "cloud.png",
  sx: 256,  sy: 256,
}

model.nhtiles = {
  model:  "nhtiles.png",
 sx: 32, sy: 32,
 GetClip : function ( 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
          ")";
},


}


model.banana = {
  model: "banana.gif",
  sx: 33,  sy: 35,
}

model.player = {
  model: "chrome://mozapps/skin/profile/profileicon.gif",
  sx: 16, sy: 16,
}

map.grass = {
  model:  "grass.jpg",
}



/*========= Models ==========*/


  
/*========= Entitis ==========*/


var ents = new Array();
var ient = 0;

function Spawn() {
 var identity = "ent_" + ient;
 this.edict = document.createElement("img");  
 root.appendChild( this.edict );
 this.edict.setAttribute( "id", identity  );

 this.entnum = ient;
 this.needUpdate=1;
 this.SetSpeed = function (newspeed) { this.speed = newspeed; this.needUpdate= 1;  };
 this.SetGoal = function (gox,goy,goz) { this.goalx = gox; this.goaly = goy;  this.goalz = goz; this.needUpdate=1; };
 this.SetOrigin = function ( x,y,z) { this.x = x; this.y = y; this.z = z; this.needUpdate=1 };
 this.Think = null;
 this.scale = 1;
 this.alpha = 1;
 this.goalx = 0;
 this.goaly = 0;
 this.speed = 1;


 ents[ient] = this;
 ient  = ient + 1;
}

Spawn.prototype.Update = function () {
  if (!this.needUpdate)  return;
  this.ForceUpdate();
  this.needUpdate = FALSE;
}


Spawn.prototype.ForceUpdate = function (){
  var me = this.edict.style;
  if (!me) return;

  me.position = "absolute";
  me.zIndex =  parseInt( this.z );
  me.left = (this.x - (this.sizex*this.scale)/2 ) + "px";
  me.top = (this.y - (this.sizey*this.scale)/2 )+ "px";
  me.width = this.sizex* this.scale;
  me.height = this.sizey * this.scale;
  me.opacity = this.alpha;
}

Spawn.prototype.SetModel = function ( modelThing ) {
 this.model = modelThing.model;
 this.sizex = modelThing.sx;
 this.sizey = modelThing.sy;
 this.scale = 1;

// document.getElementById(this.id).setAttribute("src", this.model);
 this.edict.setAttribute("src", this.model);
 this.needUpdate=TRUE;
}

Spawn.prototype.MovetypeTeleport = function (){
 this.Update();
}

Spawn.prototype.MovetypeGoal = function (){
   if (!this.speed) return;   

  var orgx = this.x;
  var orgy = this.y;

  if (this.x < this.goalx)
     this.x = this.x + this.speed;

  if (this.x >this.goalx)
    this.x = this.x - this.speed;

  if (this.y < this.goaly)
    this.y = this.y + this.speed;

  if (this.y > this.goaly)
     this.y =   this.y - this.speed;

 

  if (this.x != orgx || this.y != orgy)  {
    this.needUpdate = TRUE;
   // window.status = "need update:" + rand(1000); 
  }

 if (this.Touch) {
    var distance = (Math.abs( this.x - this.goalx) + Math.abs(this.y - this.goaly)) 
   if ( distance< (this.speed*2)) {
       this.Touch();
   }
 }
   
 this.Update();//Update if needed
}

Spawn.prototype.Nextthink = function ( delay ){
  if (!this.Think) {
      alert("ERROR: Nexthing withouth delay");
      return;
  }
  if (!delay)
  delay = this.ticrate;

  setTimeout( "ents["+this.entnum+"].Think()", delay );
}

function getEnt(identity) {
 return ents[identity];
}


/*========= Entitis ==========*/

/*========= Program ==========*/


/*========= Program ==========*/

/*========= Input Capture ==========*/

function InitEngine(){
 document.captureEvents(Event.MOUSEMOVE | Event.MOUSEDOWN); 
 document.onMouseMove = getMouseLoc; 
 document.onMouseDown = getMouseClick;
 document.addEventListener('mousemove', getMouseLoc, true);
 document.addEventListener('mousedown', getMouseClick, true);
};


function Point(x,y) {  this.x = x; this.y = y; }
var mouseLocation = new Point(0,0);
var mouseClickLocation = new Point(0,0);
var UserClick = null;

function getMouseLoc(e)
{
    mouseLocation.x = e.pageX;
    mouseLocation.y = e.pageY;
  return true;
}

function getMouseClick(e) {
  if (UserClick)
   UserClick( e.pageX, e.pageY );

    mouseClickLocation.x = e.pageX;
    mouseClickLocation.y = e.pageY;

  return true;
}


/*========= Input Capture ==========*/


/*========= Host ==========*/



function MapColor (color) {
  root.style.backgroundColor = color;
}

function MapModel ( moted ) {
  root.setAttribute("background", moted.model ); 
}



var numframe = 0;
var SimulationActive = 1;

function ToggleSimulation() {
 SimulationActive = !SimulationActive;
}


/*========= Host ==========*/



var rand = function(value) {  return Math.random()*value; }; 

