view Viewer_new/resources/vacuum.js @ 8:e18bf5f59698

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

// 再帰的に呼び出して末端のchildから先に描画している
function Draw_renew(ctx, node){
    for(num in node.child){
	Draw_renew(ctx, node.child[num])
    }
    if(node.kind != null){
        if(node.kind == "REDCUBE"){
            drawBall(ctx, node.angle, node.xyz, node.scale, node.image);
        }else if(node.kind == "ENEMY"){
	    drawBall(ctx, node.angle, node.xyz, node.scale, node.image);
	    if(node.lock_on){
                drawBall(ctx, node.angle, node.xyz, node.scale, ctx.obj.LOCK);
	    }
	}else if(node.kind == "BIGCUBE" && !node.hit){
	    drawBall(ctx, node.angle, node.xyz, node.scale, node.image);
	}
    }    
}


// それぞれのnodeにchildを追加する
function Vacuum_coll(ctx, root){
    for(num in root.child){
        Vacuum_coll(ctx, root.child[num])
    }
    var rand = Math.random()
    var id   = root.child.length
   
    if(rand < 0.5){
        root.child.push(new Add_child(root, id, "REDCUBE", ctx.obj.REDCUBE.w, ctx.obj.REDCUBE.h, ctx.obj.REDCUBE))
    }else if(rand >= 0.5 && !root.hit){
        root.child.push(new Add_child(root, id, "ENEMY", ctx.obj.ENEMY.w, ctx.obj.ENEMY.h, ctx.obj.ENEMY))
    }
}


function My_action(){
    var pad = new HandleKeys()
    
    if(pad.vacuum_hold()){
        my_cube.my_collision(my_cube);
	my_cube.vacuum(my_cube);
    }

    if(pad.lock_on_hold()){
	my_cube.lock(my_cube)
    }else{
	my_cube.attack(my_cube)
    }
}


// ランダムにcubeを発生させる
function Random_cube(ctx, root){
    var rand = Math.random()
    var id   = root.child.length

    
    if(rand < 0.5){
	root.child.push(new Add_cube(root, id, "REDCUBE", ctx.obj.REDCUBE.w, ctx.obj.REDCUBE.h, ctx.obj.REDCUBE))
    }else{
	root.child.push(new Add_cube(root, id, "ENEMY", ctx.obj.ENEMY.w, ctx.obj.ENEMY.h, ctx.obj.ENEMY))
    }
}


// 吸引関数vacuum()
vacuum = function Vacuum(node){
    const range = 200;
    const suction = 1.5;
    for(num in node.child){
        vacuum(node.child[num])
    }
    
    if(node.parents != null){
	var x = my_cube.xyz[0] - node.xyz[0]
	var y = my_cube.xyz[1] - node.xyz[1]
        var d = Math.sqrt(Math.pow(x,2) + Math.pow(y,2))

        if(x > 0 && d < range){
	    node.xyz[0] += suction
	}else if(x <= 0 && d < range){
	    node.xyz[0] += -suction
	}

	if(y > 0 && d < range){
	    node.xyz[1] +=  suction
	}else if(y <= 0 && d < range){
            node.xyz[1] += -suction
	}

    }
}


// 自機の移動関数
function Vacuum_move(node){
    var pad = new HandleKeys()
    const lim_x = (width-my_cube.w)/2.0
    const lim_y = (height-my_cube.h)/2.0
    const move_angle = 2.0
    const move_speed = 5.0

    if(pad.right_hold() && node.xyz[0] < lim_x){
	node.xyz[0] += move_speed
        node.angle[0] += move_angle
    }else if(pad.left_hold() && node.xyz[0] > -lim_x){
	node.xyz[0] -= move_speed
	node.angle[0] -= move_angle
    }

    if(pad.up_hold() && node.xyz[1] < lim_y){
	node.xyz[1] += move_speed
        node.angle[1] += move_angle
    }else if(pad.down_hold() && node.xyz[1] > -lim_y){
	node.xyz[1] -= move_speed
        node.angle[1] -= move_angle
    }
    
    if(pad.start_hold()){
        node.xyz[0] = 0
	node.xyz[1] = 0
    }	
}



// 自機との当たり判定
mycollision = function Mcollision(node){
    for(num in node.child){
        mycollision(node.child[num])
    }

    if(node.parents != null){
        var r = ((my_cube.w * my_cube.scale) + (node.w * node.scale)) / 2.0   
        var bx = my_cube.xyz[0] - node.xyz[0]              
        var by = my_cube.xyz[1] - node.xyz[1]              
        var d = Math.sqrt(Math.pow(bx,2) + Math.pow(by,2))

        if(d < r){
	    /* もしENEMYを吸い込んでいたら自機のhit判定をtrueに */
	    if(node.kind == "ENEMY" && my_cube.hit != true){
		my_cube.hit = true;
	    }
	    node.remove(node)
        }
    }
}



// ENEMYをLock_onする関数
lock_cube = function Lock_on(node){
    const lock_on_range = 200;
    for(num in node.child){
        lock_cube(node.child[num])
    }

    if(node.parents != null){
        var x = my_cube.xyz[0] - node.xyz[0]
	var y = my_cube.xyz[1] - node.xyz[1]
	var d = Math.sqrt(Math.pow(x,2) + Math.pow(y,2))

	if(d < lock_on_range && node.kind == "ENEMY"){
	    node.lock_on = true
	}
    }
}


// Lock_onされてるENEMYを攻撃
lock_attack = function Lock_attack(node){
    for(num in node.child){
        lock_attack(node.child[num])
    }

    if(node.parents != null && node.lock_on){
	node.remove(node)
    }
}


/* SceneGraph_init()を継承して、vacuum用の関数や変数を追加
 * 自機なので、色々専用の関数を追加している。
 */ 
function My_cube(parents, kind, w, h, image){
    var my = new SceneGraph_init()
    my.parents = parents
    my.kind = kind
    my.w = w
    my.h = h
    my.image = image
    my.title = new create_title
    my.gameover = new create_gameover
    my.lock = lock_cube
    my.attack = lock_attack
    my.vacuum = vacuum
    my.my_collision = mycollision
    return my
}


/* child用の追加関数 */
function Add_child(parents, id, kind, w, h, image){
    var child = new SceneGraph_init()
    child.xyz[0] = parents.xyz[0]
    child.xyz[1] = parents.xyz[1]
    child.parents = parents
    child.kind = kind
    child.w = w
    child.h = h
    child.id = id
    child.image = image
    child.dx = child.random_x(2.0)
    child.dy = child.random_y(2.0)
    child.lock_on = false
    return child
}


/* ランダムに作成するCubeに関しては、親と同じ座標から出力されないように
 * している。
 */
function Add_cube(parents, id, kind, w, h, image){
    var child = new SceneGraph_init()
    child.xyz[0] = child.random_x(width/2.0)
    child.xyz[1] = child.random_y(height/2.0)
    child.parents = parents
    child.kind = kind
    child.w = w
    child.h = h
    child.id = id
    child.image = image
    child.dx = child.random_x(2.0)
    child.dy = child.random_y(2.0)
    child.lock_on = false
    return child
}


// TITLEを作成
create_title = function Title(parents, kind, w, h){
    var title = new SceneGraph_init()
    title.parents = parents
    title.kind = kind
    title.w = w
    title.h = h
    return title
}


// GAMEOVERを作成
create_gameover = function Gameover(parents, kind, w, h){
    var gameover = new SceneGraph_init()
    gameover.parents = parents
    gameover.kind = kind
    gameover.w = w
    gameover.h = h
    return gameover
}