class Brick extends DrawRect{ int hit_num; boolean alive = true; Brick(int x, int y, int width, int height){ super(x,y,width,height); hit_num = 1; } void main(){ if(alive){ draw(); } } void hit(){ hit_num--; if(hit_num <= 0){ alive = false; } } } class Bat extends DrawRect{ Bat(int x, int y, int width, int height){ super(x,y,width,height); } void main(){ x += ((mouseX-(width>>1))-x)>>2; x = constrain(x, 0, game_width-width); draw(); } } class Ball extends DrawRect{ int vx,vy,speed; Ball(int x, int y, int width, int height){ super(x,y,width,height); vx = 2; vy = 4; speed = 0; } void main(){ move(); checkBrickCollision(); checkWallCollision(game_width, game_height); checkBatCollision(); draw(); } void move(){ x += vx; y += vy; } void checkBatCollision(){ if(intersects(this, bat)){ resolveBatCollision(bat); } } void checkWallCollision(int w, int h){ if(x+width >= w){ x = w-width; vx = -vx; } if(x < 0){ x = 0; vx = -vx; } if(y < 0){ y = 0; vy = -vy; } if(y+height > h){ y = h-height; vy = -vy; } } void checkBrickCollision(){ for(int i = 0; i < brick.length; i++){ if(intersects(this, brick[i]) && brick[i].alive){ resolveBrickCollision(brick[i]); } } } void resolveBatCollision(Bat b){ updateCenter(); int side = b.voronoi(cx, cy); int xd = cx - b.cx; if(abs(xd) < b.width>>1) side = UP; switch(side){ case UP: y = b.y-height; if(xd < -50){ vx = -4-speed; vy = -2-speed; } else if(xd < -40){ vx = -3-speed; vy = -3-speed; } else if(xd < -15){ vx = -2-speed; vy = -4-speed; } else if(xd < 0){ vx = -1-speed; vy = -5-speed; } else if(xd >= 0 && xd < 16){ vx = 1+speed; vy = -5-speed; } else if(xd > 15){ vx = 2+speed; vy = -4-speed; } else if(xd > 40){ vx = 3+speed; vy = -3-speed; } else if(xd > 50){ vx = 4+speed; vy = -2-speed; } break; case LEFT: x = b.x-width; vx = -4-speed; vy = -2-speed; break; case UP_LEFT: x = b.x-width; y = b.y-height; vx = -4-speed; vy = -2-speed; break; case RIGHT: x = b.x+b.width; vx = 4+speed; vy = -2-speed; break; case UP_RIGHT: x = b.x+b.width; y = b.y-height; vx = 4+speed; vy = -2-speed; break; } } void resolveBrickCollision(Brick w){ w.hit(); updateCenter(); int side = w.voronoi4(cx, cy); switch(side){ case RIGHT: x = w.x+w.width; vx = abs(vx); break; case DOWN: y = w.y+w.height; vy = abs(vy); break; case LEFT: x = w.x-width; vx = -abs(vx); break; case UP: y = w.y-height; vy = -abs(vy); break; case DOWN_RIGHT: x = w.x+w.width; y = w.y+w.height; vx = abs(vx); vy = abs(vy); break; case DOWN_LEFT: x = w.x-width; y = w.y+w.height; vx = -abs(vx); vy = abs(vy); break; case UP_LEFT: x = w.x-width; y = w.y-height; vx = -abs(vx); vy = -abs(vy); break; case UP_RIGHT: x = w.x+w.width; y = w.y-height; vx = abs(vx); vy = -abs(vy); break; } } }