view Vacuum_new/resources/SceneGraph.js @ 8:e18bf5f59698

vacuum_ver5
author <e085737>
date Mon, 29 Nov 2010 16:39:20 +0900
parents 7cf40bc8a472
children 182428b3aee8
line wrap: on
line source

// ループさせる処理を入れる
function MainLoop(ctx){
    if(my_cube.title.hit){
	Vacuum_move(my_cube)
	My_action()
        Move_renew(my_cube)
        Draw_renew(ctx, my_cube)
        Collision_renew(my_cube)
	Camera_zoom()
    }else{
        drawBall(ctx, my_cube.title.angle, my_cube.title.xyz, my_cube.title.scale, ctx.obj.TITLE)
    }

        // ENEMYを吸い込んでいたらgame overを表示
        if(my_cube.hit){
	    drawBall(ctx, my_cube.gameover.angle, my_cube.gameover.xyz, my_cube.gameover.scale, ctx.obj.gameover);
	}
}


// 再帰的に呼び出して末端のchildから動かしている
function Move_renew(node){
    for(num in node.child){
        Move_renew(node.child[num])
    }
    if(node.parents != null){
        node.move_func(node)
    }
}


// 再帰的に呼び出して末端のchildから壁との当たり判定を見ている
function Collision_renew(node){
    for(num in node.child){
	Collision_renew(node.child[num])
    }
    if(node.parents != null){ 
        node.wall_collision(node)
    }
}


function Camera_zoom(){
    var zoom = 10.0

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

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



// init()関数
function SceneGraph_init(){
    this.xyz = new Array(0,0,0)
    this.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.hit = false
    this.parents = null
    this.child = new Array()
}


// 移動関数
SceneGraph_init.prototype.move_func = function(node){
    node.xyz[0] += node.dx
    node.xyz[1] += node.dy
}


// 壁との当たり判定
SceneGraph_init.prototype.wall_collision = function(node){
    const lim_x = width/2.0
    const lim_y = height/2.0
    if(node.xyz[0] >= lim_x){
	node.xyz[0] = lim_x
        node.dx = -node.dx
    }
    if(node.xyz[0] <= -lim_x){
	node.xyz[0] = -lim_x
        node.dx = -node.dx
    }
    if(node.xyz[1] >= lim_y){
	node.xyz[1] = lim_y
        node.dy = -node.dy
    }
    if(node.xyz[1] <= -lim_y){
	node.xyz[1] = -lim_y
        node.dy = -node.dy
    }
}


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


	if(length != 0){
	    // 親の書き換え
	    for(num in node.child){
		node.child[num].parents = node.parents
	    }
	    // 要素を取り除く
	    node.parents.child.splice(node.id, 1)

	    // 取り除いた要素のchildを要素のparentsに追加
	    for(num in node.child){
	        node.parents.child.push(node.child[num])
	    }
	    // idの振りなおし
	    for(num in node.parents.child){
		node.parents.child[num].id = num
	    }
	}else{
	    node.parents.child.length = 0
	}
    }
}


// 乱数生成関数
SceneGraph_init.prototype.random_x = function(scope){
    var rand = Math.random()
    if(rand < 0.5){
        return(Math.random() * scope / 2.0)
    }else{
	return(Math.random() * -scope / 2.0)
    }	    
}   


// 乱数生成関数
SceneGraph_init.prototype.random_y = function(scope){
    var rand = Math.random()
    if(rand < 0.5){
	return(Math.random() * scope / 2.0)
    }else{
	return(Math.random() * -scope / 2.0)
    }
}


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


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



/* 他のボールのとの当り判定   ※ vacuumでは使っていない
 * 今後拡張予定
 */
mcollision = function Material_collision(root){
    for(num in root.child){
        if(num != my_num){
	    var data = material.other[num];
            var k = ((ball.w*ball.scale) + (data.w*data.scale)) / 2.0;
	    var bx = ball.x-data.x;
	    var by = ball.y-data.y;
            var distance = Math.sqrt(Math.pow(bx,2) + Math.pow(by,2));

	    if(distance < k){
	        if((material.dx * material.dx) < 0){
	            material.dx = -material.dx;
		}else if((material.dy * data.dy) <0){
		    material.dy = -material.dy;
	        }else if(material.dx > data.dx && material.x < data.x){
		    material.dx = -material.dx;
		}else if(material.dx < data.dx && material.x > data.x){
		    material.dx = -material.dx;
		}else if(material.dy > data.dy && material.y < data.y){
		    material.dy = -material.dy;
		}else if(material.dy < data.dy && material.y > data.y){
		    material.dy = -material.dy;
		}
	    }
        }
    }
}