

o3djs.require('ft.house');
o3djs.require('ft.viewinfo');
o3djs.require('ft.event');
o3djs.require('ft.orthoimage');
o3djs.require('ft.canvas');
o3djs.require('ft.action');
o3djs.require('ft.config');
o3djs.require('ft.shapes');

o3djs.require('o3djs.math');
o3djs.require('o3djs.picking');



window.onload = init;
window.onunload = uninit;

var g_o3d;             //o3d名字空间
var g_client;          //O3D应用入口
var g_clock = 0;
var g_timeMult = 1;
var g_finished = false; 

var g_pack;
var g_math;
var g_transform;
var g_transform_city;
var g_transform_list=[];
var g_transform_road=[];
var g_transform_bottom=[];
var g_shape_list=[];
var g_transform_persp;//透视视区transform
var g_transform_ortho;//平视视区transform
var g_viewInfo_persp;//透视视区
var g_viewInfo_ortho;//平视视区


//渲染事件——每渲染一次调用此函数
function renderCallback(renderEvent) 
{
    g_clock += renderEvent.elapsedTime * g_timeMult;  // Rotate the cube around the Y axis.  
	val=0.2 * g_clock;
	g_transform_city.identity(); 
	g_transform_city.translate([-7,-1,7]);
	g_transform_city.rotateY(val);
}



//创建 O3D Plug-in 对象
function init() 
{
    setEye();
    document.getElementById("o3d").focus();
	o3djs.util.makeClients(initStep2);
}

function initStep2(clientElements) 
{
	//初始化全局变量
    var o3dElement = clientElements[0];  //HTML O3D element, which is part of the DOM
    g_client = o3dElement.client;  
    g_o3d = o3dElement.o3d;  
	g_math = o3djs.math;
    o3djs.base.init(o3dElement);
    g_pack = g_client.createPack();
    createViewInfo();
	createMap();
	createHoure();
	createFullScreenImage();
	ft.event.registerEvent(o3dElement);
	g_client.setRenderCallback(renderCallback);
	g_finished = true;
}

function createViewInfo()
{
	g_transform_persp=g_pack.createObject('Transform');
    g_viewInfo_persp=ft.viewinfo.createPerspViewinfo(g_transform_persp);
	
	g_transform_ortho=g_pack.createObject('Transform');
	g_viewinfo_ortho=ft.viewinfo.createOrthoViewinfo(g_transform_ortho);
	g_viewinfo_ortho.root.priority = g_viewInfo_persp.root.priority + 1;
}

function createFullScreenImage()
{
	var tf_ortho=ft.orthoimage.createFullScreen('fullscreen',100,50,'images/fullscreen.jpg');
	tf_ortho.identity();
	tf_ortho.translate(50,550,0);
	tf_ortho.rotateX(4.7123);
	tf_ortho.parent=g_transform_ortho;
	g_client.setFullscreenClickRegion(10,
        g_client.height - 50 - 10, 100,
        50, getFullscreenModeId(g_client));
}

getFullscreenModeId=function(client) {
  var displayModes = client.getDisplayModes();
  var bestMode;
  for (var index in displayModes) {
    var mode = displayModes[index];
    if (!bestMode ||
        (mode.width > bestMode.width && mode.height > bestMode.height)) {
      bestMode = mode;
    }
  }
  if (bestMode) {
    return bestMode.id;
  } else {  // getDisplayModes isn't implemented on all platforms yet.
    return g_o3d.Renderer.DISPLAY_MODE_DEFAULT;
  }
}

function uninit() 
{
	if (g_client) 
	{
		g_client.cleanup();  
	}
}

function initShape()
{
	g_shape_list[0]=ft.shapes.createBox(1,0.01,1,city_texture[1]);//房屋地基
	g_shape_list[1]=ft.shapes.createGreen('a',city_texture[2]);//绿地
	g_shape_list[2]=ft.shapes.createZdoor();//透明门
	g_shape_list[3]=ft.shapes.creatPlane('r',1,1,city_texture[4]);//道路
	g_shape_list[4]=ft.shapes.creatPlane('r',1,1,city_texture[5]);//拐角道路
	
	g_shape_list[10]=ft.shapes.createBox(1,1,1,city_texture[0]);//城墙
}

function createMap()
{
	initShape();
	var g_transform=g_pack.createObject('Transform');
	for(var wi=0;wi<wall_positions.length;wi++)//城墙循环
	{
		var transform_map=g_pack.createObject('Transform');
		transform_map.addShape(g_shape_list[10]);
		transform_map.translate(wall_positions[wi]);
		transform_map.parent=g_transform;
	}
	
	//东西方向道路
	for(var ri=0;ri<road_rows.length;ri++)
	{
	    var transform_map=g_pack.createObject('Transform');
		transform_map.addShape(g_shape_list[3]);
		transform_map.translate(road_rows[ri]);
		transform_map.rotateZ(3.1415926);
		transform_map.parent=g_transform;	    
	}
	//南北方向道路
	for(var ri=0;ri<road_cols.length;ri++)
	{
	    var transform_map=g_pack.createObject('Transform');
		transform_map.addShape(g_shape_list[3]);
		transform_map.translate(road_cols[ri]);
		transform_map.rotateZ(3.1415926);
		transform_map.rotateY(1.5707963);
		transform_map.parent=g_transform;	    
	}
	//拐角道路
	for(var ri=0;ri<roadL_positions.length;ri++)
	{
	    var transform_map=g_pack.createObject('Transform');
		transform_map.addShape(g_shape_list[4]);
		transform_map.translate(roadL_positions[ri].position);
		transform_map.rotateZ(3.1415926);
		if(roadL_positions[ri].direction==1)
			transform_map.rotateY(-1.5707963);
		if(roadL_positions[ri].direction==2)
			transform_map.rotateY(3.1415926);
		if(roadL_positions[ri].direction==3)
			transform_map.rotateY(1.5707963);
		transform_map.parent=g_transform;	    
	}
	//去城市箭头
	var rangv_num=rangv.length;
	var px;
	var pz;
	for(ri=0;ri<go_citys.length;ri++)
	{
		var transform_map=g_pack.createObject('Transform');
		var shape_city=ft.shapes.creatPlane('r',1,1,go_citys[ri].url_texture);
		transform_map.addShape(shape_city);
		transform_map.translate(go_citys[ri].position);
		transform_map.rotateZ(3.1415926);
		px=go_citys[ri].position[0];
		pz=go_citys[ri].position[2];
		if(go_citys[ri].direction==0)
		{
			rangv[rangv_num++]=[px-0.4,px+0.4,pz-0.4,pz+1];
		}
		if(go_citys[ri].direction==1)
		{
			transform_map.rotateY(-1.5707963);
			rangv[rangv_num++]=[px-0.4,px+1,pz-0.4,pz+0.4];
		}
		if(go_citys[ri].direction==2)
		{
			transform_map.rotateY(3.1415926);
			rangv[rangv_num++]=[px-0.4,px+0.4,pz-1,pz+0.4];
		}
		if(go_citys[ri].direction==3)
		{
			transform_map.rotateY(1.5707963);
			rangv[rangv_num++]=[px-1,px+0.4,pz-0.4,pz+0.4];
		}
		transform_map.parent=g_transform;
	}

	var transform_map=g_pack.createObject('Transform');//中央绿地        
	transform_map.addShape(g_shape_list[1]);
	transform_map.translate([-7,0,7]);
	transform_map.rotateZ(3.1415926);
	transform_map.parent=g_transform;
	
	g_transform_city=g_pack.createObject('Transform');
	g_transform_city.addShape(g_shape_list[7]);
	g_transform_city.translate([-7,-1,7]);
	g_transform_city.parent=g_transform;
	
	g_transform.parent=g_transform_persp;
}

/**
*创建房子
*/
function createHoure()
{
	var rangv_num=rangv.length;
	var doorx;
	var doory;
	var doorz;
	g_transform=g_pack.createObject('Transform');
	
	for(var ui=0;ui<houselist.length;ui++)
	{
		
		g_transform_list[ui]=ft.house.selectHouse(houselist[ui].name,
																	g_viewInfo_persp,
																	houselist[ui].url_texture,
																	houselist[ui].style);
		g_transform_list[ui].translate(houselist[ui].position);
	    g_transform_list[ui].parent = g_transform;
		
		//地基
		var transform_bottom=g_pack.createObject('Transform');
		transform_bottom.addShape(g_shape_list[0]);
		transform_bottom.translate(houselist[ui].position);
		transform_bottom.parent=g_transform;
		//透明门走廊
		var tf_door=g_pack.createObject('Transform');
	    tf_door.createParam('diffuse', 'ParamFloat4').value = [1,0.6,0.4,0.8];
		tf_door.addShape(g_shape_list[2]);
		doorx=houselist[ui].position[0];
	    doory=-0.25;
	    doorz=houselist[ui].position[2];
		if(houselist[ui].direction==0)
		{
			doorx=houselist[ui].position[0]-0.5;
			tf_door.translate([doorx,doory,doorz]);
		}
		if(houselist[ui].direction==1)
		{
			doorz=houselist[ui].position[2]+0.5;
			tf_door.translate([doorx,doory,doorz]);
			tf_door.rotateY(1.5707963);
		}
		if(houselist[ui].direction==2)
		{
			doorx=houselist[ui].position[0]+0.5;
			tf_door.translate([doorx,doory,doorz]);
		}
		if(houselist[ui].direction==3)
		{
			doorz=houselist[ui].position[2]-0.5;
			tf_door.translate([doorx,doory,doorz]);
			tf_door.rotateY(1.5707963);
		}
		rangv[rangv_num++]=[doorx-0.15,doorx+0.15,doorz-0.15,doorz+0.15];
		tf_door.parent=g_transform;
	}
	g_transform_list[houselist.length]=ft.house.selectHouse('aa',
																	g_viewInfo_persp,
																	city_texture[3],
																	0);
	g_transform_list[houselist.length].translate(-7,-1,7);
	g_transform_list[houselist.length].parent = g_transform;
	g_transform.parent=g_transform_persp;
}

setEye=function()
{
    gf_comecity(g_cityid);
}
//转换城市
goCity=function()
{
	var nx=ft.viewinfo.persp_eye[0];
	var nz=ft.viewinfo.persp_eye[2];
	var x1;
	var x2;
	var y1;
	var y2;
	for(ri=0;ri<go_citys.length;ri++)
	{
		x1=go_citys[ri].gocity[0];
		x2=go_citys[ri].gocity[1];
		y1=go_citys[ri].gocity[2];
		y2=go_citys[ri].gocity[3];
		if(ft.math.israngeXZ(x1,x2,y1,y2,nx,nz))
		{
			gf_gocity(go_citys[ri].cityid,g_cityid);
		}
	}
}

//进入房间
goHoure=function()
{
	var nx=ft.viewinfo.persp_eye[0];
	var nz=ft.viewinfo.persp_eye[2];
	for(var ui=0;ui<houselist.length;ui++)
	{
		doorx=houselist[ui].position[0];
	    doorz=houselist[ui].position[2];
		if(houselist[ui].direction==0)
		{
			if(ft.math.israngeXZ(doorx-0.45,doorx-0.35,doorz-0.5,doorz+0.5,nx,nz))
			{
				gf_gohoure(houselist[ui].position,0,g_cityid,houselist[ui].name,houselist[ui].houreid);
			}
		}
		if(houselist[ui].direction==1)
		{
			if(ft.math.israngeXZ(doorx-0.5,doorx+0.5,doorz+0.35,doorz+0.45,nx,nz))
			{
				gf_gohoure(houselist[ui].position,1,g_cityid,houselist[ui].name,houselist[ui].houreid);
			}
		}
		if(houselist[ui].direction==2)
		{
			if(ft.math.israngeXZ(doorx+0.35,doorx+0.45,doorz-0.5,doorz+0.5,nx,nz))
			{
				gf_gohoure(houselist[ui].position,2,g_cityid,houselist[ui].name,houselist[ui].houreid);
			}
		}
		if(houselist[ui].direction==3)
		{
			if(ft.math.israngeXZ(doorx-0.5,doorx+0.5,doorz-0.45,doorz-0.35,nx,nz))
			{
				gf_gohoure(houselist[ui].position,3,g_cityid,houselist[ui].name,houselist[ui].houreid);
			}
		}
	}
}