Mercurial > hg > Members > e085737 > sample
view Viewer/resources/vacuum.js @ 6:ca8540cab316
viewer
author | <e085737> |
---|---|
date | Fri, 26 Nov 2010 15:45:27 +0900 |
parents | |
children | 7cf40bc8a472 |
line wrap: on
line source
function update(ctx){ my.notify(); for(num in my.ball_info){ if(my.ball_info[num].hit != 1){ var data = my.ball_info[num]; data.wcollision(data); //data.bcollision(data,num); data.move(data); if(data.kind != "bomb"){ drawBall(ctx, currentAngle, data.x, data.y, data.z, data.scale, ctx.obj.REDCUBE); }else{ drawBall(ctx, currentAngle, data.x, data.y, data.z, data.scale, ctx.obj.ENEMY); } } } // bombを吸い込んでいたらgame overを表示 if(my.hit == 1){ drawBall(ctx, 0, 0,0,0, my.scale, ctx.obj.gameover); } drawBall(ctx, currentAngle, my.x,my.y,my.z, my.scale, ctx.obj.BIGCUBE); } function Ball(x,y,z,b_h, b_w, kind){ this.x=parseInt(Math.random()*(width/2)); this.y=parseInt(Math.random()*(height/2)); this.z=z; this.h=b_h; this.w=b_w; this.scale = 1.0; // 大きさ this.kind =kind; this.dx=1.0 this.dy=1.0 this.hit=0; this.move=ballmove; this.other; this.wcollision = wallcollision; this.bcollision = ballcollision; return this; } function add_ball(x, y, z, h, w){ my.ball_info.push(new Ball(x, y, z, h, w, "ball")); } function add_bomb(x, y, z, h, w){ my.ball_info.push(new Ball(x, y, z, h, w, "bomb")); } function Myball(balls,m_h,m_w,e_r){ this.x=0; this.y=0; this.z=0; this.h=m_h; this.w=m_w; this.e_r=e_r; this.scale = 1.0; // 大きさ this.dx=20.0; // 初速度 this.dy=20.0; this.hit=0; this.ball_info=balls; this.collision = mycollision; this.vacuum=vacuum; this.notify = ball_notify; return this; } // 子に対して、他の子の情報を更新する ball_notify = function notify(){ for(num in my.ball_info){ var data = my.ball_info[num]; data.other = my.ball_info; } } // 子に対して、他の子の情報を入れる function ball_in(){ for(num in my.ball_info){ var data = my.ball_info[num]; data.other = my.ball_info; } } // ボールの移動 ballmove = function Bmove(ball){ ball.x += ball.dx; ball.y += ball.dy; } // 壁との当たり判定 wallcollision = function Wcollision(ball){ const lim_x = width/2.0; const lim_y = height/2.0; if(ball.y <= -lim_y){ ball.y = -lim_y; ball.dy = -ball.dy; } if(ball.y >= lim_y){ ball.y = lim_y; ball.dy = -ball.dy; } if(ball.x <= -lim_x){ ball.x = -lim_x; ball.dx = -ball.dx; } if(ball.x >= lim_x){ ball.x = lim_x; ball.dx = -ball.dx; } } // 他のボールのとの当り判定 ballcollision = function Bcollision(ball,my_num){ for(num in ball.other){ if(num != my_num){ var data = ball.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((ball.dx * data.dx) < 0){ ball.dx = -ball.dx; }else if((ball.dy * data.dy) <0){ ball.dy = -ball.dy; }else if(ball.dx > data.dx && ball.x < data.x){ ball.dx = -ball.dx; }else if(ball.dx < data.dx && ball.x > data.x){ ball.dx = -ball.dx; }else if(ball.dy > data.dy && ball.y < data.y){ ball.dy = -ball.dy; }else if(ball.dy < data.dy && ball.y > data.y){ ball.dy = -ball.dy; } } } } } // 自機との当たり判定 mycollision = function Mcollision(){ //var r = ((my.w + my.e_r) / 2.0 * my.scale) for(num in my.ball_info){ var data = my.ball_info[num]; var r = ((my.w*my.scale) + (data.w*data.scale)) / 2.0 var bx = my.x-data.x; // 2点間のx距離を求める var by = my.y-data.y; // 2点間のy距離を求める var d = Math.sqrt(Math.pow(bx,2) + Math.pow(by,2)); if(d < r){ data.hit = 1; if(data.kind == "bomb" && my.hit != 1){ my.hit = 1; } } } } // 吸引関数vacuum() vacuum = function vacuum(){ var range = 100; var suction = 3.0; for(num in my.ball_info){ var data = my.ball_info[num]; p_x = my.x-data.x; p_y = my.y-data.y; if(p_x > 0 && p_x < range){ data.x += suction; }else if(p_x < 0 && p_x > -range){ data.x += -suction; } if(p_y > 0 && p_y < range){ data.y += suction; }else if(p_y < 0 && p_y > -range){ data.y += -suction; } } }