view Walk/resources/SceneGraph.js @ 0:718974a1a32b

Vacuum
author <e085737>
date Tue, 07 Dec 2010 17:31:15 +0900
parents
children
line wrap: on
line source

// ループさせる処理を入れる
function MainLoop(ctx,sgroot,w,h){
	collision_object(sg_exec_tree,sgroot,w,h)
        move_object(sg_exec_tree,sgroot,w,h)
	draw_object(ctx,sg_exec_tree,sgroot)
	//camera_object(ctx,sg_exec_tree,sgroot)
}

/*
//カメラの位置を調整
function camera_object(){
    var zoom = 10.0

    if(zoom_in_hold()){
        view_point += zoom
    }

    if(zoom_out_hold()){
        view_point -= zoom
    }
}

*/

/* 再帰的に呼び出して全てのnodeのcollisionを実行する */
function collision_object(node,sgroot,w,h){
    for(num in node.child){
	collision_object(node.child[num],sgroot,w,h)
    }
    if(node.collision != null){
        node.collision(node,sgroot,w,h)
        node.frame++
    }
}


/* 再帰的に呼び出して全てのnodeのmoveを実行する */
function move_object(node,sgroot,w,h){
    for(num in node.child){
	move_object(node.child[num],sgroot,w,h)
    }
    if(node.move != null){
        node.move(node,sgroot,w,h)
    }
}


/* 再帰的に呼び出して全てのnodeをdrawする */
function draw_object(ctx,node,sgroot){
    for(num in node.child){
        draw_object(ctx,node.child[num],sgroot)
    }
    if(node.id != null){
	/* idを引数にイメージを引っ張ってきてimageに格納している */
        var image = sgroot.getSgid(node.id)
        drawBall(ctx, node.angle, node.xyz, node.scale, image) 
    }
}


SceneGraph = function(name){
    this.next = null
    this.prev = null
    this.last = null
    this.parents = null
    this.children = null
    this.lastChild = null

    this.xyz = new Array(0,0,0)
    this.angle = new Array(0,0,0)
    this.stack_xyz = new Array(0,0,0)
    this.stack_angle = new Array(0,0,0)
    this.w = 0
    this.h = 0
    this.scale = 1.0
    this.dx = 1.0
    this.dy = 1.0
    this.dz = 1.0
    this.id = name
    this.num = 0
    this.time = 0

    this.move = null
    this.collision = null
    this.child = new Array()

    this.coord_xyz = null
    this.normal = null
    this.coord_tex = null

    this.texture_id = -1
    
    this.flag_remove = 0
    this.flag_drawable = 1
    this.sgid = -1
    this.gid = -1

    this.frame = 0
}


// SceneGraphを削除する
SceneGraph.prototype.remove = function(node){
    if(node.parents != null){
        var length = node.parents.child.length


	if(length != 0){
	    // 親の書き換え
	    for(num in node.child){
		node.child[num].parents = node.parents
	    }

	    // 要素を取り除く
	    node.parents.child.splice(node.num, 1)

	    // 取り除いたnodeのchildをnodeのparentsに追加
	    for(num in node.child){
		if(node.child[num].id != "LOCK"){
	            node.parents.child.push(node.child[num])
		}
	    }


	    // idの振りなおし
	    for(num in node.parents.child){
		node.parents.child[num].num = num
	    }
	}else{
	    node.parents.child.length = 0
	}
    }
}


/* moveとcollisionをsetする */
SceneGraph.prototype.set_move_collision = function(new_move, new_collision){
    this.move = new_move
    this.collision = new_collision
}


SceneGraph.prototype.clone = function(){
    p = new SceneGraph(this)
    return p
}


// zoom_inする関数
SceneGraph.prototype.zoom_in = function(node){
    if(zoom_in_hold()){
        view_point += 10.0
    }
}


// zoom_outする関数
SceneGraph.prototype.zoom_out = function(node){
    if(zoom_out_hold()){
        view_point -= 10.0
    }
}


SceneGraph.prototype.addBrother = function(bro){
    if(this.parents != null){
        parents.addChild(bro)
    }else{
	alert("error : doesn't have parent")
    }

    return bro
}


/* 子どもの追加 */
SceneGraph.prototype.addChild = function(node){	
   var num = this.child.length 
   this.child.push(node)
   node.parents = this
   node.num = num
}