From 4d56984b3ea6f2467b19d7cfa07fdb28ef1d1cf5 Mon Sep 17 00:00:00 2001 From: Chara1236 Date: Mon, 20 Jun 2022 20:52:03 -0400 Subject: [PATCH] Commenting --- saves/Level1-signs.txt | 2 +- saves/Level2-signs.txt | 2 +- src/GamePanel.java | 2 +- src/Particle.java | 7 ++++++- src/Player.java | 25 ++++++++++++++++++++----- src/StickyBomb.java | 31 ++++++++++++++++++++++--------- 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/saves/Level1-signs.txt b/saves/Level1-signs.txt index 6fb060d..0440100 100644 --- a/saves/Level1-signs.txt +++ b/saves/Level1-signs.txt @@ -9,7 +9,7 @@ 5700 150 kill slimes 3320 200 They float! / -3430 500 Click left to throw a bomb You have a limited amount of bombs per level! +3500 500 Click left to throw a sticky bomb You have a limited amount of bombs per level! 2950 550 you can blow up boxes 500 200 Press WASD to move and R to restart 500 250 S makes you fall down faster diff --git a/saves/Level2-signs.txt b/saves/Level2-signs.txt index 666ce7b..b038a50 100644 --- a/saves/Level2-signs.txt +++ b/saves/Level2-signs.txt @@ -1,5 +1,5 @@ 1300 50 Jump to the top -1900 200 Trust me, the steel +1900 250 Trust me, the steel 1900 300 block is quite useful here 3450 500 Steel block? 4500 100 You need your bomb here... \ No newline at end of file diff --git a/src/GamePanel.java b/src/GamePanel.java index befdeb1..1f3ed1f 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -339,7 +339,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ } // draw bomb counter (bomb image and amount of bombs remaining) g.drawImage(bomb.image,20,20,35,35,null); - g.drawString("X"+LevelManager.bombs,60,40); + g.drawString("X"+LevelManager.bombs+" Sticky bombs",60,40); if (isPaused) { // cover background with translucent rectangle g.setColor(new Color(255, 255, 255, 100)); diff --git a/src/Particle.java b/src/Particle.java index b54f1e9..a58f6be 100644 --- a/src/Particle.java +++ b/src/Particle.java @@ -1,5 +1,5 @@ // Eric Li, Charlie Zhao, ICS4U, Finished 6/18/22 -// create particles when the character jumps or on top of lava +// create particles when the character walks or on top of lava import java.awt.*; import java.io.IOException; @@ -16,16 +16,21 @@ public class Particle extends GenericSprite implements Serializable { public BufferedImageWrapper sprite; public Particle(int x, int y, int xVelocity, int yVelocity, int length, String filePath) throws IOException { + //Creates generic sprite class super(x,y,length, length); this.xVelocity = xVelocity; this.yVelocity = yVelocity; sprite = new BufferedImageWrapper((filePath)); } + + //Moves the sprite, and gives it gravity public void move(){ x+=xVelocity; y+=yVelocity; yVelocity+=0.5; } + + //Draws the sprite on the screen public void draw(Graphics g){ g.drawImage(sprite.image,x-GameFrame.game.camera.x,y,WIDTH,HEIGHT, null); } diff --git a/src/Player.java b/src/Player.java index 78196db..f29a37b 100644 --- a/src/Player.java +++ b/src/Player.java @@ -347,9 +347,9 @@ public class Player extends GenericSprite { //Handles events when the player presses a mouse button. //Left for throwing bombs, and right for picking and placing steel public void mousePressed(MouseEvent e) throws SpriteException, IOException { - //canReach(1,1); mouseX = e.getX(); mouseY = e.getY(); + //If the player left clicks and is holding steel and can place it, place the steel if(e.getButton()==MouseEvent.BUTTON1) { int x = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length; int y = (mouseY / Tile.length); @@ -364,6 +364,8 @@ public class Player extends GenericSprite { leftMouseDown = true; } } + //If the player right clicks, either pick up steel, or places it down depending if the player + //is already holding steel if(e.getButton()==MouseEvent.BUTTON3) { int x = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length; int y = (mouseY / Tile.length); @@ -397,16 +399,15 @@ public class Player extends GenericSprite { } - //TODO + + //Using BFS check if there is a physical path where the player can reach a block public boolean canReach(int x, int y){ try { int pX = (int) (((double) GameFrame.game.camera.x + GamePanel.GAME_WIDTH) / Tile.length); int pY = (int) (((double) this.y + HEIGHT / 2) / Tile.length); - //System.out.println(pX+" "+pY); if (pY < 0) { return false; } - //BFS int[][] check = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; int[][] dis = new int[1000][18]; for (int[] a : dis) { @@ -447,20 +448,26 @@ public class Player extends GenericSprite { return false; } } + + //When the player drags the mouse, update the the x and y position of the mouse public void mouseDragged(MouseEvent e) throws IOException, SpriteException { mouseX = e.getX(); mouseY = e.getY(); } + //When the player moves the mouse, update the x and y position of the mouse public void mouseMoved(MouseEvent e) { mouseX = e.getX(); mouseY = e.getY(); } + + //Handles actions when the player releases a mouse button public void mouseReleased(MouseEvent e) throws IOException, SpriteException { mouseX = e.getX(); mouseY = e.getY(); + //If it's the left button, throw a bomb, given they have bombs if(e.getButton()==MouseEvent.BUTTON1) { leftMouseDown = false; if (leftClickPlacedSteel) { @@ -473,26 +480,35 @@ public class Player extends GenericSprite { } } } + //If its right click, simply set rightMouseDown to false if(e.getButton()==MouseEvent.BUTTON3){ rightMouseDown = false; } } + + //Adds walking particles to the player public void addParticle(int x) throws IOException { if(UtilityFunction.randInt(1,3)==3) { GameFrame.game.particles.add(new Particle(this.x + GameFrame.game.camera.x + WIDTH / 2 + UtilityFunction.randInt(-PLAYER_WIDTH / 2, PLAYER_WIDTH / 2) , (int) (y + HEIGHT * 0.95), UtilityFunction.randInt(-2, 2) + x, UtilityFunction.randInt(-4, 1), UtilityFunction.randInt(1, 7), "img/particles/GrassParticle.png")); } } + + //Draws the player on the screen public int draw(Graphics g, int frame) { frame %= spriteArray[0][0].length; + //Draws the pickup range for steel if the player is holding down right click if(rightMouseDown){ g.drawOval((int)(x+WIDTH/2-(reach*steelReachRange)),(int)(y+HEIGHT/2-(reach*steelReachRange)), (int)(reach*steelReachRange*2),(int)(reach*steelReachRange*2)); } + + //If the player isn't pressing up/left/right, dont animate sprite if (!upPressed && !leftPressed && !rightPressed) { g.drawImage(spriteArray[lastXDirection][lastYDirection][7].image, x-10, y, null); return 0; } else { + //If they do click one of those keys, animate the sprite lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2; lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2; lastFrame = frame; @@ -503,5 +519,4 @@ public class Player extends GenericSprite { } - //public int BfsDis() } diff --git a/src/StickyBomb.java b/src/StickyBomb.java index 93d2cff..3e36db7 100644 --- a/src/StickyBomb.java +++ b/src/StickyBomb.java @@ -1,3 +1,6 @@ +// Eric Li, Charlie Zhao, ICS4U, Completed 6/20/2022 +// StickyBomb class creates a bomb with physics and exploding abilities + import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; import java.awt.*; @@ -27,11 +30,13 @@ public class StickyBomb extends GenericSprite implements Serializable { public StickyBomb(int x, int y, int xVelocity, int yVelocity, BufferedImageWrapper sprite, BufferedImageWrapper[] explosionSpriteArray){ + //Creates generic sprite class super(x,y,length,length); this.xVelocity = xVelocity; this.yVelocity = yVelocity; this.sprite = sprite; this.explosionSpriteArray = explosionSpriteArray; + //Sets default state of bomb fuse = GlobalState.second*5; isMove = true; alive = true; @@ -40,24 +45,18 @@ public class StickyBomb extends GenericSprite implements Serializable { + //Updates the realX position of the bomb, rather than where it is drawn on the screen. public void update(){ realX = x - GameFrame.game.camera.x; } + //After the fuse runs out explode 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(); UtilityFunction.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); + //Propels player away from bomb if(hypo<300) { if (yDis != 0) { GameFrame.game.player.yVelocity += 10 * (yDis) / (hypo); @@ -66,8 +65,10 @@ public class StickyBomb extends GenericSprite implements Serializable { GameFrame.game.player.xVelocity += 10 * (xDis) / (hypo); } } + //Caps player speed GameFrame.game.player.capSpeed(); alive = false; + //Kills enemies for(int i=0; i50){yVelocity=50;} update(); + //Updates fuse if(fuse>0) { fuse-=1; if(fuse == 0) { explode(); } } + //If it can move, set movement to true if(canUpdate(1,1)&&canUpdate(-1,1)&&canUpdate(0,-1)&&isMove == false&&fuse>0){ isMove = true; xVelocity = 0; yVelocity = 0; } + //If isMove is true, and the bomb can update, update the position if(isMove) { if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){ checked = true; x += -Math.signum(xVelocity); isMove = false; int updateAmount = 0; + //Depending on the xVelocity and yVelocity, check which direction to update in + if(xVelocity>0&&yVelocity>0){ while(canUpdate(updateAmount, updateAmount)){ updateAmount++;