view Vacuum/resources/cube.js @ 0:718974a1a32b

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

/* ある程度時間が経つとcube_splitを実行 */
cube_collision = function(node, sgroot, screen_w, screen_h){ 
    if(node.frame > 400){
	cube_split(node, sgroot)
    }
}


cube_move_left = function(node, sgroot, screen_w, screen_h){
    node.xyz[0] -= node.stack_xyz[0]
    node.xyz[1] += node.stack_xyz[1]

    if(node.xyz[0] < 0){
	node.set_move_collision(cube_move_right, cube_collision)
    }

    if(node.xyz[1] > 0 || node.xyz[1] < screen_h){
	node.stack_xyz[1] = -node.stack_xyz[1]
    }
}


cube_rotate = function(node, w, h){
    node.angle[0] += 2.0
    node.angle[1] += 2.0
    node.angle[2] += 2.0
}


cube_move_right = function(node, sgroot, screen_w, screen_h){
    node.xyz[0] += node.stack_xyz[0]
    node.xyz[1] += node.stack_xyz[1]

    if(node.xyz[0] > screen_w){
	node.set_move_collision(cube_move_left, cube_collision)
    }

    if(node.xyz[1] > 0 || node.xyz[1] < screen_h){
	node.stack_xyz[1] = -node.stack_xyz[1]
    }
}


cube_split = function(root, sgroot){
     
    if(Math.random() < 0.5){
        var p = sgroot.createSceneGraph2(redcube.name)
    }else{
	var p = sgroot.createSceneGraph2(enemy.name)
    }

    root.set_move_collision(cube_move_right, cube_collision)
    p.set_move_collision(cube_move_left, cube_collision)

    root.frame = 0
    p.frame = 0

    p.xyz[0] = root.xyz[0] + 2
    p.xyz[1] = root.xyz[1] 
    p.xyz[2] = root.xyz[2]

    p.stack_xyz[0] = 0.5
    p.stack_xyz[1] = Math.random()*2-1
    p.stack_xyz[2] = 0.0

    root.xyz[0] -= 2
    root.stack_xyz[0] = 0.5
    root.stack_xyz[1] = Math.random()*2-1

    root.addChild(p)
}



function collision_red(node){
    
    for(num in node.child){
	collision_red(node.child[num])
    }

    if(node.id == "REDCUBE"){
        var mcube = sg_exec_tree

        var dx = mcube.xyz[0] - node.xyz[0]
        var dy = mcube.xyz[1] - node.xyz[1]
        var ddx = dx*dx
        var ddy = dy*dy
        var r = Math.sqrt(ddx + ddy)
	
        if(r < 10){
	    node.remove(node)
        }else{
            if(r >= 1) var q = 200/r
            if(dx == 0){
                if(node.xyz[1] > mcube.xyz[1]){
                    node.stack_xyz[1] -= q
	        }else if(mcube.xyz[1] < node.xyz[1]){
	            node.stack_xyz[1] += q
                }
            }else{

	        if(node.xyz[0] > mcube.xyz[0]){
                    node.xyz[0] -= q*Math.cos(Math.atan(dy/dx))
	            node.xyz[1] -= q*Math.sin(Math.atan(dy/dx))
	        }else if(node.xyz[0] < mcube.xyz[0]){
	            node.xyz[0] += q*Math.cos(Math.atan(dy/dx))
    	            node.xyz[1] += q*Math.sin(Math.atan(dy/dx))
		}
	    }
	}
    }
}



function collision_purple(node, sgroot, w, h){
   
    for(num in node.child){
        collision_purple(node.child[num],sgroot,w,h)
    }

    if(node.id == "ENEMY"){
        var mcube = sg_exec_tree
        var dx = node.xyz[0] - mcube.xyz[0]
        var dy = node.xyz[1] - mcube.xyz[1]
        var ddx = dx*dx
        var ddy = dy*dy
        var r = Math.sqrt(ddx + ddy)

        if(r < 10){
            gameover_scene(w,h,mcube,sgroot)
            node.remove(node)
        }else{
            if(r >= 1) q = 200/r
            if(dx == 0){
                if(node.xyz[1] > mcube.xyz[1]){
	            node.stack_xyz[1] -= q
	        }else if(node.xyz[1] < mcube.xyz[1]){
       	            node.stack_xyz[1] += q
	        }
            }else{
 
  	        if(node.xyz[0] > mcube.xyz[0]){
	            node.xyz[0] -= q*Math.cos(Math.atan(dy/dx))
	            node.xyz[1] -= q*Math.sin(Math.atan(dy/dx))
	        }else if(node.xyz[0] < mcube.xyz[0]){
	            node.xyz[0] += q*Math.cos(Math.atan(dy/dx))
       	            node.xyz[1] += q*Math.sin(Math.atan(dy/dx))
		}
	    }
	}
    }
}