import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.Serializable; public class StickyBomb extends GenericSprite implements Serializable { public static final int spriteLength = 35; public static final int length = 25; public int xVelocity; public int yVelocity; public boolean isMove; public int realX; public BufferedImageWrapper sprite ; public BufferedImageWrapper[] explosionSpriteArray; public int fuse; public int explosionPixel = 0; public int explosionCounter = 0; public boolean alive; public boolean erase; public StickyBomb(int x, int y, int xVelocity, int yVelocity, BufferedImageWrapper sprite, BufferedImageWrapper[] explosionSpriteArray){ super(x,y,length,length); this.xVelocity = xVelocity; this.yVelocity = yVelocity; this.sprite = sprite; this.explosionSpriteArray = explosionSpriteArray; fuse = GlobalState.second*5; isMove = true; alive = true; erase = false; } public void update(){ realX = x - GameFrame.game.camera.x; } public void explode() throws UnsupportedAudioFileException, LineUnavailableException, IOException { // Sound explode = new Sound("sound/explode.wav"); // if (explode == null) { // try { // explode = new Sound("sound/explode.wav"); // } catch (UnsupportedAudioFileException | LineUnavailableException e) { // throw new RuntimeException(e); // } // } // explode.start(); SoundWrapper.playSound("sound/explode.wav"); double yDis = GameFrame.game.player.y+Player.PLAYER_HEIGHT/2-(y+(double)length/2); double xDis = GameFrame.game.player.x+Player.PLAYER_WIDTH/2-(realX+(double)length/2); double hypo = Math.sqrt(yDis*yDis+xDis*xDis); if(hypo<300) { if (yDis != 0) { GameFrame.game.player.yVelocity += 10 * (yDis) / (hypo); } if (xDis != 0) { GameFrame.game.player.xVelocity += 10 * (xDis) / (hypo); } } GameFrame.game.player.capSpeed(); alive = false; for(int i=0; itile.x&&x50){yVelocity=50;} update(); if(fuse>0) { fuse-=1; if(fuse == 0) { explode(); } } if(canUpdate(1,1)&&canUpdate(-1,1)&&canUpdate(0,-1)&&isMove == false&&fuse>0){ isMove = true; xVelocity = 0; yVelocity = 0; } if(isMove) { if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){ checked = true; x += -Math.signum(xVelocity); isMove = false; int updateAmount = 0; if(xVelocity>0&&yVelocity>0){ while(canUpdate(updateAmount, updateAmount)){ updateAmount++; } x+=updateAmount; y+=updateAmount; } else if(xVelocity<0&&yVelocity>0){ while(canUpdate(-updateAmount, updateAmount)){ updateAmount++; } x-=updateAmount; y+=updateAmount; } else if(xVelocity>0&&yVelocity<0){ while(canUpdate(updateAmount, -updateAmount)){ updateAmount++; } x+=updateAmount; y-=updateAmount; } else if(xVelocity<0&&yVelocity<0){ while(canUpdate(-updateAmount, -updateAmount)){ updateAmount++; } x-=updateAmount; y-=updateAmount; } xVelocity = 0; yVelocity = 0; } if(!canUpdate(xVelocity, 0)){ checked = true; 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)){ checked = true; 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)&&!checked) { y = y + (int) yVelocity; x = x + (int) xVelocity; } else { isMove = false; } yVelocity+=3; } } public void draw(Graphics g) throws UnsupportedAudioFileException, LineUnavailableException, IOException { if (explosionCounter >= 2) { explosionPixel += 1; explosionCounter -= 2; } if(alive) { g.drawImage(sprite.image, x - GameFrame.game.camera.x - (spriteLength-length)/2, y - (spriteLength-length)/2, spriteLength, spriteLength, null); //g.drawRect(x-GameFrame.game.camera.x,y,length,length); } else if (explosionPixel < explosionSpriteArray.length - 1) { // please note that the explosion is completely centered on the x plane ("5*") but tends upwards on the y plane ("10*") g.drawImage(explosionSpriteArray[explosionPixel].image, x - GameFrame.game.camera.x - 5*explosionPixel, y-10*explosionPixel, spriteLength+10*explosionPixel, spriteLength+10*explosionPixel, null); explosionCounter += 1; } else { erase = true; } } }