import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; public class StickyBomb extends GenericSprite{ public int length; public int xVelocity; public int yVelocity; public boolean isMove; public int realX; public BufferedImage sprite; public BufferedImage[] explosionSpriteArray; public int fuse; public int explosionPixel = 0; public int explosionCounter = 0; public boolean alive; public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite, BufferedImage[] explosionSpriteArray){ super(x,y,length,length); this.length = length; this.xVelocity = xVelocity; this.yVelocity = yVelocity; this.sprite = sprite; this.explosionSpriteArray = explosionSpriteArray; fuse = GlobalState.second*5; isMove = true; alive = true; } public void update(){ realX = x - GamePanel.camera.x; } public void explode(){ double yDis = GamePanel.player.y+Player.PLAYER_HEIGHT/2-(y+(double)length/2); double xDis = GamePanel.player.x+Player.PLAYER_HEIGHT/2-(realX+(double)length/2); double hypo = Math.sqrt(yDis*yDis+xDis+xDis); if(yDis!=0){ GamePanel.player.yVelocity += 10000/(yDis*hypo); } if(xDis!=0) { GamePanel.player.xVelocity += 100000/(xDis*hypo); } GamePanel.player.capSpeed(); alive = false; } public void move(){ update(); if(fuse>0) { fuse-=1; if(fuse == 0) { explode(); } } if(isMove) { if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){ x += -Math.signum(xVelocity); //isMove = false; } if(!canUpdate(xVelocity, 0)){ isMove = false; int updateAmount = 0; if(xVelocity>0){ while(canUpdate(updateAmount, 0)){ updateAmount++; } x+=updateAmount-1; } else if(xVelocity<0){ while(canUpdate(updateAmount, 0)){ updateAmount--; } x+=updateAmount+1; } xVelocity = 0; } if(!canUpdate(0, yVelocity)){ isMove = false; if(yVelocity>0){ while(canUpdate(0,1)){ y+=1; } isGrounded = true; } else if(yVelocity<0){ while(canUpdate(0,-1)){ y-=1; } } yVelocity = 0; } if(canUpdate(xVelocity, yVelocity)) { y = y + (int) yVelocity; x = x + (int) xVelocity; } else { isMove = false; } yVelocity+=3; } } public void mousePressed(MouseEvent e){ int xx = e.getX(); int yy = e.getY(); GamePanel.b = new StickyBomb(GamePanel.player.x+GamePanel.camera.x,GamePanel.player.y,35, (xx-GamePanel.player.x)/20, (yy-GamePanel.player.y)/10, sprite, explosionSpriteArray); } public void draw(Graphics g){ if (explosionCounter >= 2) { explosionPixel += 1; explosionCounter -= 2; } if(alive) { g.drawImage(sprite, x - GamePanel.camera.x, y, length, length, null); } else if (explosionPixel < explosionSpriteArray.length - 1) { g.drawImage(explosionSpriteArray[explosionPixel], x - GamePanel.camera.x - 10*explosionPixel, y-10*explosionPixel, length+10*explosionPixel, length+10*explosionPixel, null); explosionCounter += 1; } } }