/******************************************************************************* ** ** ** @author: Thanius ** ** @version: 1.0.0 ** ** ** ** @info: Functions for Bombs and Explosions ** ** ** *******************************************************************************/ package bomberman; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; public class Bomb { private Animation animBomb; private Animation eastExp, northExp, westExp, southExp, centerExp; private Animation horizontalExp1East, horizontalExp2East, horizontalExp3East, horizontalExp4East; private Animation horizontalExp1West, horizontalExp2West, horizontalExp3West, horizontalExp4West; private Animation verticalExp1North, verticalExp2North, verticalExp3North, verticalExp4North; private Animation verticalExp1South, verticalExp2South, verticalExp3South, verticalExp4South; private BufferedImage bomb1, bomb2, currentFrame; private BufferedImage east1, east2, east3, east4, eastCurrentFrame; private BufferedImage north1, north2, north3, north4, northCurrentFrame; private BufferedImage west1, west2, west3, west4, westCurrentFrame; private BufferedImage south1, south2, south3, south4, southCurrentFrame; private BufferedImage center1, center2, center3, center4, centerCurrentFrame; private BufferedImage hCurrentFrame1East, hCurrentFrame2East, hCurrentFrame3East, hCurrentFrame4East; private BufferedImage hCurrentFrame1West, hCurrentFrame2West, hCurrentFrame3West, hCurrentFrame4West; private BufferedImage horizontal1, horizontal2, horizontal3, horizontal4; private BufferedImage vCurrentFrame1North, vCurrentFrame2North, vCurrentFrame3North, vCurrentFrame4North; private BufferedImage vCurrentFrame1South, vCurrentFrame2South, vCurrentFrame3South, vCurrentFrame4South; private BufferedImage vertical1, vertical2, vertical3, vertical4; private ArrayList bricksUndestroyable, bricksDestroyable; private Rectangle area, userLocation; private Rectangle eastArea, northArea, westArea, southArea, centerArea; private Rectangle hori1EastArea, hori2EastArea, hori3EastArea, hori4EastArea; private Rectangle hori1WestArea, hori2WestArea, hori3WestArea, hori4WestArea; private Rectangle vert1NorthArea, vert2NorthArea, vert3NorthArea, vert4NorthArea; private Rectangle vert1SouthArea, vert2SouthArea, vert3SouthArea, vert4SouthArea; private int x, y, power; private boolean dropped, exploded; private boolean isEastD1, isEastD2, isEastD3, isEastD4, isEastD5; private boolean isEastU1, isEastU2, isEastU3, isEastU4, isEastU5; private boolean isNorthD1, isNorthD2, isNorthD3, isNorthD4, isNorthD5; private boolean isNorthU1, isNorthU2, isNorthU3, isNorthU4, isNorthU5; private boolean isWestD1, isWestD2, isWestD3, isWestD4, isWestD5; private boolean isWestU1, isWestU2, isWestU3, isWestU4, isWestU5; private boolean isSouthD1, isSouthD2, isSouthD3, isSouthD4, isSouthD5; private boolean isSouthU1, isSouthU2, isSouthU3, isSouthU4, isSouthU5; private long oldTime, newTime, elapsedTime, oldExpTime, newExpTime, elapsedExpTime; public Bomb() { InitBomb(); InitExplosion(); } private void InitBomb() { try { bomb1 = ImageIO.read(getClass().getResource("Images/BomberBombs/1.gif")); bomb2 = ImageIO.read(getClass().getResource("Images/BomberBombs/2.gif")); } catch (IOException ex) { Logger.getLogger(Bomb.class.getName()).log(Level.SEVERE, null, ex); } animBomb = new Animation(); animBomb.AddFrame(bomb1, 100); animBomb.AddFrame(bomb2, 100); power = 1; currentFrame = bomb1; x = y = 0; area = new Rectangle(); userLocation = new Rectangle(); dropped = exploded = false; oldTime = newTime = elapsedTime = 0; } private void InitExplosion() { try { east1 = ImageIO.read(getClass().getResource("Images/BomberFires/E1.gif")); east2 = ImageIO.read(getClass().getResource("Images/BomberFires/E2.gif")); east3 = ImageIO.read(getClass().getResource("Images/BomberFires/E3.gif")); east4 = ImageIO.read(getClass().getResource("Images/BomberFires/E4.gif")); north1 = ImageIO.read(getClass().getResource("Images/BomberFires/N1.gif")); north2 = ImageIO.read(getClass().getResource("Images/BomberFires/N2.gif")); north3 = ImageIO.read(getClass().getResource("Images/BomberFires/N3.gif")); north4 = ImageIO.read(getClass().getResource("Images/BomberFires/N4.gif")); west1 = ImageIO.read(getClass().getResource("Images/BomberFires/W1.gif")); west2 = ImageIO.read(getClass().getResource("Images/BomberFires/W2.gif")); west3 = ImageIO.read(getClass().getResource("Images/BomberFires/W3.gif")); west4 = ImageIO.read(getClass().getResource("Images/BomberFires/W4.gif")); south1 = ImageIO.read(getClass().getResource("Images/BomberFires/S1.gif")); south2 = ImageIO.read(getClass().getResource("Images/BomberFires/S2.gif")); south3 = ImageIO.read(getClass().getResource("Images/BomberFires/S3.gif")); south4 = ImageIO.read(getClass().getResource("Images/BomberFires/S4.gif")); center1 = ImageIO.read(getClass().getResource("Images/BomberFires/C1.gif")); center2 = ImageIO.read(getClass().getResource("Images/BomberFires/C2.gif")); center3 = ImageIO.read(getClass().getResource("Images/BomberFires/C3.gif")); center4 = ImageIO.read(getClass().getResource("Images/BomberFires/C4.gif")); horizontal1 = ImageIO.read(getClass().getResource("Images/BomberFires/H1.gif")); horizontal2 = ImageIO.read(getClass().getResource("Images/BomberFires/H2.gif")); horizontal3 = ImageIO.read(getClass().getResource("Images/BomberFires/H3.gif")); horizontal4 = ImageIO.read(getClass().getResource("Images/BomberFires/H4.gif")); vertical1 = ImageIO.read(getClass().getResource("Images/BomberFires/V1.gif")); vertical2 = ImageIO.read(getClass().getResource("Images/BomberFires/V2.gif")); vertical3 = ImageIO.read(getClass().getResource("Images/BomberFires/V3.gif")); vertical4 = ImageIO.read(getClass().getResource("Images/BomberFires/V4.gif")); } catch (IOException ex) { Logger.getLogger(Game.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } eastExp = new Animation(); eastExp.AddFrame(east1, 100); eastExp.AddFrame(east2, 100); eastExp.AddFrame(east3, 100); eastExp.AddFrame(east4, 100); eastExp.AddFrame(east3, 100); eastExp.AddFrame(east2, 100); eastExp.AddFrame(east1, 100); northExp = new Animation(); northExp.AddFrame(north1, 100); northExp.AddFrame(north2, 100); northExp.AddFrame(north3, 100); northExp.AddFrame(north4, 100); northExp.AddFrame(north3, 100); northExp.AddFrame(north2, 100); northExp.AddFrame(north1, 100); westExp = new Animation(); westExp.AddFrame(west1, 100); westExp.AddFrame(west2, 100); westExp.AddFrame(west3, 100); westExp.AddFrame(west4, 100); westExp.AddFrame(west3, 100); westExp.AddFrame(west2, 100); westExp.AddFrame(west1, 100); southExp = new Animation(); southExp.AddFrame(south1, 100); southExp.AddFrame(south2, 100); southExp.AddFrame(south3, 100); southExp.AddFrame(south4, 100); southExp.AddFrame(south3, 100); southExp.AddFrame(south2, 100); southExp.AddFrame(south1, 100); centerExp = new Animation(); centerExp.AddFrame(center1, 100); centerExp.AddFrame(center2, 100); centerExp.AddFrame(center3, 100); centerExp.AddFrame(center4, 100); centerExp.AddFrame(center3, 100); centerExp.AddFrame(center2, 100); centerExp.AddFrame(center1, 100); horizontalExp1East = new Animation(); horizontalExp1East.AddFrame(horizontal1, 100); horizontalExp1East.AddFrame(horizontal2, 100); horizontalExp1East.AddFrame(horizontal3, 100); horizontalExp1East.AddFrame(horizontal4, 100); horizontalExp1East.AddFrame(horizontal3, 100); horizontalExp1East.AddFrame(horizontal2, 100); horizontalExp1East.AddFrame(horizontal1, 100); horizontalExp2East = new Animation(); horizontalExp2East.AddFrame(horizontal1, 100); horizontalExp2East.AddFrame(horizontal2, 100); horizontalExp2East.AddFrame(horizontal3, 100); horizontalExp2East.AddFrame(horizontal4, 100); horizontalExp2East.AddFrame(horizontal3, 100); horizontalExp2East.AddFrame(horizontal2, 100); horizontalExp2East.AddFrame(horizontal1, 100); horizontalExp3East = new Animation(); horizontalExp3East.AddFrame(horizontal1, 100); horizontalExp3East.AddFrame(horizontal2, 100); horizontalExp3East.AddFrame(horizontal3, 100); horizontalExp3East.AddFrame(horizontal4, 100); horizontalExp3East.AddFrame(horizontal3, 100); horizontalExp3East.AddFrame(horizontal2, 100); horizontalExp3East.AddFrame(horizontal1, 100); horizontalExp4East = new Animation(); horizontalExp4East.AddFrame(horizontal1, 100); horizontalExp4East.AddFrame(horizontal2, 100); horizontalExp4East.AddFrame(horizontal3, 100); horizontalExp4East.AddFrame(horizontal4, 100); horizontalExp4East.AddFrame(horizontal3, 100); horizontalExp4East.AddFrame(horizontal2, 100); horizontalExp4East.AddFrame(horizontal1, 100); horizontalExp1West = new Animation(); horizontalExp1West.AddFrame(horizontal1, 100); horizontalExp1West.AddFrame(horizontal2, 100); horizontalExp1West.AddFrame(horizontal3, 100); horizontalExp1West.AddFrame(horizontal4, 100); horizontalExp1West.AddFrame(horizontal3, 100); horizontalExp1West.AddFrame(horizontal2, 100); horizontalExp1West.AddFrame(horizontal1, 100); horizontalExp2West = new Animation(); horizontalExp2West.AddFrame(horizontal1, 100); horizontalExp2West.AddFrame(horizontal2, 100); horizontalExp2West.AddFrame(horizontal3, 100); horizontalExp2West.AddFrame(horizontal4, 100); horizontalExp2West.AddFrame(horizontal3, 100); horizontalExp2West.AddFrame(horizontal2, 100); horizontalExp2West.AddFrame(horizontal1, 100); horizontalExp3West = new Animation(); horizontalExp3West.AddFrame(horizontal1, 100); horizontalExp3West.AddFrame(horizontal2, 100); horizontalExp3West.AddFrame(horizontal3, 100); horizontalExp3West.AddFrame(horizontal4, 100); horizontalExp3West.AddFrame(horizontal3, 100); horizontalExp3West.AddFrame(horizontal2, 100); horizontalExp3West.AddFrame(horizontal1, 100); horizontalExp4West = new Animation(); horizontalExp4West.AddFrame(horizontal1, 100); horizontalExp4West.AddFrame(horizontal2, 100); horizontalExp4West.AddFrame(horizontal3, 100); horizontalExp4West.AddFrame(horizontal4, 100); horizontalExp4West.AddFrame(horizontal3, 100); horizontalExp4West.AddFrame(horizontal2, 100); horizontalExp4West.AddFrame(horizontal1, 100); verticalExp1North = new Animation(); verticalExp1North.AddFrame(vertical1, 100); verticalExp1North.AddFrame(vertical2, 100); verticalExp1North.AddFrame(vertical3, 100); verticalExp1North.AddFrame(vertical4, 100); verticalExp1North.AddFrame(vertical3, 100); verticalExp1North.AddFrame(vertical2, 100); verticalExp1North.AddFrame(vertical1, 100); verticalExp2North = new Animation(); verticalExp2North.AddFrame(vertical1, 100); verticalExp2North.AddFrame(vertical2, 100); verticalExp2North.AddFrame(vertical3, 100); verticalExp2North.AddFrame(vertical4, 100); verticalExp2North.AddFrame(vertical3, 100); verticalExp2North.AddFrame(vertical2, 100); verticalExp2North.AddFrame(vertical1, 100); verticalExp3North = new Animation(); verticalExp3North.AddFrame(vertical1, 100); verticalExp3North.AddFrame(vertical2, 100); verticalExp3North.AddFrame(vertical3, 100); verticalExp3North.AddFrame(vertical4, 100); verticalExp3North.AddFrame(vertical3, 100); verticalExp3North.AddFrame(vertical2, 100); verticalExp3North.AddFrame(vertical1, 100); verticalExp4North = new Animation(); verticalExp4North.AddFrame(vertical1, 100); verticalExp4North.AddFrame(vertical2, 100); verticalExp4North.AddFrame(vertical3, 100); verticalExp4North.AddFrame(vertical4, 100); verticalExp4North.AddFrame(vertical3, 100); verticalExp4North.AddFrame(vertical2, 100); verticalExp4North.AddFrame(vertical1, 100); verticalExp1South = new Animation(); verticalExp1South.AddFrame(vertical1, 100); verticalExp1South.AddFrame(vertical2, 100); verticalExp1South.AddFrame(vertical3, 100); verticalExp1South.AddFrame(vertical4, 100); verticalExp1South.AddFrame(vertical3, 100); verticalExp1South.AddFrame(vertical2, 100); verticalExp1South.AddFrame(vertical1, 100); verticalExp2South = new Animation(); verticalExp2South.AddFrame(vertical1, 100); verticalExp2South.AddFrame(vertical2, 100); verticalExp2South.AddFrame(vertical3, 100); verticalExp2South.AddFrame(vertical4, 100); verticalExp2South.AddFrame(vertical3, 100); verticalExp2South.AddFrame(vertical2, 100); verticalExp2South.AddFrame(vertical1, 100); verticalExp3South = new Animation(); verticalExp3South.AddFrame(vertical1, 100); verticalExp3South.AddFrame(vertical2, 100); verticalExp3South.AddFrame(vertical3, 100); verticalExp3South.AddFrame(vertical4, 100); verticalExp3South.AddFrame(vertical3, 100); verticalExp3South.AddFrame(vertical2, 100); verticalExp3South.AddFrame(vertical1, 100); verticalExp4South = new Animation(); verticalExp4South.AddFrame(vertical1, 100); verticalExp4South.AddFrame(vertical2, 100); verticalExp4South.AddFrame(vertical3, 100); verticalExp4South.AddFrame(vertical4, 100); verticalExp4South.AddFrame(vertical3, 100); verticalExp4South.AddFrame(vertical2, 100); verticalExp4South.AddFrame(vertical1, 100); eastCurrentFrame = northCurrentFrame = westCurrentFrame = southCurrentFrame = centerCurrentFrame = null; hCurrentFrame1East = hCurrentFrame2East = hCurrentFrame3East = hCurrentFrame4East = null; hCurrentFrame1West = hCurrentFrame2West = hCurrentFrame3West = hCurrentFrame4West = null; vCurrentFrame1North = vCurrentFrame2North = vCurrentFrame3North = vCurrentFrame4North = null; vCurrentFrame1South = vCurrentFrame2South = vCurrentFrame3South = vCurrentFrame4South = null; isEastD1 = isEastD2 = isEastD3 = isEastD4 = isEastD5 = false; isEastU1 = isEastU2 = isEastU3 = isEastU4 = isEastU5 = false; isNorthD1 = isNorthD2 = isNorthD3 = isNorthD4 = isNorthD5 = false; isNorthU1 = isNorthU2 = isNorthU3 = isNorthU4 = isNorthU5 = false; isWestD1 = isWestD2 = isWestD3 = isWestD4 = isWestD5 = false; isWestU1 = isWestU2 = isWestU3 = isWestU4 = isWestU5 = false; isSouthD1 = isSouthD2 = isSouthD3 = isSouthD4 = isSouthD5 = false; isSouthU1 = isSouthU2 = isSouthU3 = isSouthU4 = isSouthU5 = false; eastArea = new Rectangle(); northArea = new Rectangle(); westArea = new Rectangle(); southArea = new Rectangle(); centerArea = new Rectangle(); hori1EastArea = new Rectangle(); hori2EastArea = new Rectangle(); hori3EastArea = new Rectangle(); hori4EastArea = new Rectangle(); vert1NorthArea = new Rectangle(); vert2NorthArea = new Rectangle(); vert3NorthArea = new Rectangle(); vert4NorthArea = new Rectangle(); oldExpTime = newExpTime = elapsedExpTime = 0; } /******************************************************************************* ** ** ** A N I M A T I O N S ** ** ** *******************************************************************************/ protected void BombAnimation() { newTime = System.currentTimeMillis(); elapsedTime = newTime - oldTime; if(elapsedTime < 3000) animBomb.Update(5); if(elapsedTime > 3000 && elapsedTime < 5000) animBomb.Update(10); if(elapsedTime > 5000 && elapsedTime < 6000) animBomb.Update(15); if(elapsedTime > 6000) { dropped = false; exploded = true; oldExpTime = System.currentTimeMillis(); } } protected void ExplosionAnimation() { newExpTime = System.currentTimeMillis(); elapsedExpTime = newExpTime - oldExpTime; switch(power) { case 1: eastArea.setRect(x + 32, y, 32, 32); westArea.setRect(x - 32, y, 32, 32); southArea.setRect(x, y + 32, 32, 32); northArea.setRect(x, y - 32, 32, 32); break; case 2: eastArea.setRect(x + 64, y, 32, 32); westArea.setRect(x - 64, y, 32, 32); southArea.setRect(x, y + 64, 32, 32); northArea.setRect(x, y - 64, 32, 32); hori1EastArea.setRect(x + 32, y, 32, 32); hori1WestArea.setRect(x - 32, y, 32, 32); vert1NorthArea.setRect(x, y - 32, 32, 32); vert1SouthArea.setRect(x, y - 32, 32, 32); break; case 3: eastArea.setRect(x + 96, y, 32, 32); westArea.setRect(x - 96, y, 32, 32); southArea.setRect(x, y + 96, 32, 32); northArea.setRect(x, y - 96, 32, 32); hori1EastArea.setRect(x + 32, y, 32, 32); hori1WestArea.setRect(x - 32, y, 32, 32); hori2EastArea.setRect(x + 64, y, 32, 32); hori2WestArea.setRect(x - 64, y, 32, 32); vert1NorthArea.setRect(x, y - 32, 32, 32); vert1SouthArea.setRect(x, y + 32, 32, 32); vert2NorthArea.setRect(x, y - 64, 32, 32); vert2SouthArea.setRect(x, y + 64, 32, 32); break; case 4: eastArea.setRect(x + 128, y, 32, 32); westArea.setRect(x - 128, y, 32, 32); southArea.setRect(x, y + 128, 32, 32); northArea.setRect(x, y - 128, 32, 32); hori1EastArea.setRect(x + 32, y, 32, 32); hori1WestArea.setRect(x - 32, y, 32, 32); hori2EastArea.setRect(x + 64, y, 32, 32); hori2WestArea.setRect(x - 64, y, 32, 32); hori3EastArea.setRect(x + 96, y, 32, 32); hori3WestArea.setRect(x - 96, y, 32, 32); vert1NorthArea.setRect(x, y - 32, 32, 32); vert1SouthArea.setRect(x, y + 32, 32, 32); vert2NorthArea.setRect(x, y - 64, 32, 32); vert2SouthArea.setRect(x, y + 64, 32, 32); vert3NorthArea.setRect(x, y - 96, 32, 32); vert3SouthArea.setRect(x, y + 96, 32, 32); break; case 5: eastArea.setRect(x + 160, y, 32, 32); westArea.setRect(x - 160, y, 32, 32); southArea.setRect(x, y + 160, 32, 32); northArea.setRect(x, y - 160, 32, 32); hori1EastArea.setRect(x + 32, y, 32, 32); hori1WestArea.setRect(x - 32, y, 32, 32); hori2EastArea.setRect(x + 64, y, 32, 32); hori2WestArea.setRect(x - 64, y, 32, 32); hori3EastArea.setRect(x + 96, y, 32, 32); hori3WestArea.setRect(x - 96, y, 32, 32); hori4EastArea.setRect(x + 128, y, 32, 32); hori4WestArea.setRect(x - 128, y, 32, 32); vert1NorthArea.setRect(x, y - 32, 32, 32); vert1SouthArea.setRect(x, y + 32, 32, 32); vert2NorthArea.setRect(x, y - 64, 32, 32); vert2SouthArea.setRect(x, y + 64, 32, 32); vert3NorthArea.setRect(x, y - 96, 32, 32); vert3SouthArea.setRect(x, y + 96, 32, 32); vert4NorthArea.setRect(x, y - 128, 32, 32); vert4SouthArea.setRect(x, y + 128, 32, 32); break; } centerArea.setRect(x, y, 32, 32); for(int i = 0; i < bricksDestroyable.size(); i++) { switch(power) { case 1: if(eastArea.intersects(bricksDestroyable.get(i).GetArea())) { isEastD1 = true; bricksDestroyable.remove(i); } if(westArea.intersects(bricksDestroyable.get(i).GetArea())) { isWestD1 = true; bricksDestroyable.remove(i); } if(northArea.intersects(bricksDestroyable.get(i).GetArea())) { isNorthD1 = true; bricksDestroyable.remove(i); } if(southArea.intersects(bricksDestroyable.get(i).GetArea())) { isSouthD1 = true; bricksDestroyable.remove(i); } break; case 2: break; } } for(int i = 0; i < bricksUndestroyable.size(); i++) { } if(elapsedExpTime > 0 && elapsedExpTime < 1000) { centerExp.Update(10); if(isEastD1 || isEastD2 || isEastD3 || isEastD4 || isEastD5) eastExp.Update(10); if(isWestD1 || isWestD2 || isWestD3 || isWestD4 || isWestD5) westExp.Update(10); if(isNorthD1 || isNorthD2 || isNorthD3 || isNorthD4 || isNorthD5) northExp.Update(10); if(isSouthD1 || isSouthD2 || isSouthD3 || isSouthD4 || isSouthD5) southExp.Update(10); } if(elapsedExpTime > 1000) { isEastD1 = false; } } /******************************************************************************* ** ** ** S E T F U N C T I O N S ** ** ** *******************************************************************************/ protected void SetBricks(ArrayList bricksU, ArrayList bricksD) { bricksUndestroyable = bricksU; bricksDestroyable = bricksD; } protected void SetDestroyableBricks(ArrayList bricksD) { bricksDestroyable = bricksD; } protected void SetUserLocation(Rectangle userLocation) { this.userLocation = userLocation; } protected void SetArea(int x, int y) { area.setRect(x, y, 32, 32); } protected void SetXY(int x, int y) { this.x = x; this.y = y; } protected void SetPower(int power) { this.power = power; } protected void SetDropped(boolean dropped) { oldTime = System.currentTimeMillis(); this.dropped = dropped; } protected void SetExploded(boolean exploded) { this.exploded = exploded; } /******************************************************************************* ** ** ** G E T F U N C T I O N S ** ** ** *******************************************************************************/ protected ArrayList GetUndestroyableBricks() { return bricksUndestroyable; } protected ArrayList GetDestroyableBricks() { return bricksDestroyable; } protected BufferedImage GetCurrentFrame() { currentFrame = (BufferedImage) animBomb.GetImage(); return currentFrame; } protected BufferedImage GetEastCurrentFrame() { eastCurrentFrame = (BufferedImage) eastExp.GetImage(); return eastCurrentFrame; } protected Rectangle GetArea() { return area; } protected int GetX() { return x; } protected int GetY() { return y; } protected boolean Dropped() { return dropped; } protected boolean Exploded() { return exploded; } }