/* Eric Li, ICS4U, Completed 5/29/2022 Paddle class defines behaviours for the left and right player-controlled paddles */ 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.awt.image.ImageObserver; import java.io.IOException; public class Player extends GenericSprite { public final int SPEED = 5; public static final int PLAYER_WIDTH = 52; public static final int PLAYER_HEIGHT = 94; public int lastXDirection, lastYDirection, lastFrame; public int upKey, downKey, rightKey, leftKey; public static final int walkSpeedCap = 5; public boolean alive; private final Sound jump; public boolean leftMouseDown; // sA[0] is -x, -y // sA[1] is x, -y // sA[2] is -x, y // sA[3] is x, y public BufferedImage[][][] spriteArray; public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][][] sprites) throws UnsupportedAudioFileException, LineUnavailableException, IOException { super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH); jump = new Sound("sound/jump.wav"); this.upKey = upKey; this.downKey = downKey; this.leftKey = leftKey; this.rightKey = rightKey; spriteArray = sprites; alive = true; isPlayer = true; leftMouseDown = false; } // moves paddle when key is pressed public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_D){ rightPressed = true; } if(e.getKeyCode() == KeyEvent.VK_A){ leftPressed = true; } if(e.getKeyCode() == KeyEvent.VK_W){ upPressed = true; } if(e.getKeyCode() == KeyEvent.VK_S){ downPressed = true; } } // stops moving paddle when key is released public void keyReleased(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_D){ rightPressed = false; } if(e.getKeyCode() == KeyEvent.VK_A){ leftPressed = false; } if(e.getKeyCode() == KeyEvent.VK_W){ upPressed = false; } if(e.getKeyCode() == KeyEvent.VK_S){ downPressed = false; } } public boolean collide(Tile tile, double x, double y){ if(tile==null){return false;} if(!tile.collision){ return false; } if(x+WIDTH>tile.realX&&x0){ while(canUpdate(updateAmount, 0)){ updateAmount++; } GamePanel.camera.x+=updateAmount-1; } else if(xVelocity<0){ while(canUpdate(updateAmount, 0)){ updateAmount--; } GamePanel.camera.x+=updateAmount+1; } //xVelocity = 0; } if(!canUpdate(0, yVelocity)){ 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; GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity; } else if(canUpdate(0,yVelocity)){ y = y + (int) yVelocity; xVelocity*=0.75; } else if(canUpdate(xVelocity,0)){ GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity; } if(rightPressed){ if(isGrounded){ addParticle(-1); } if(xVelocity<5) { xVelocity += 1; } } if(leftPressed) { if(isGrounded){ addParticle(1); } if(xVelocity>-5) { xVelocity -= 1; } } if(upPressed&isGrounded){ y-=1; isGrounded = false; if(canUpdate(0,-8)) { jump.start(); } yVelocity = -10; } xVelocity *= 0.93; if(!isGrounded) { yVelocity += 0.3; if(downPressed){ yVelocity+=1; } } if(!alive){ alive = true; reset(); } capSpeed(); } public void reset(){ GamePanel.camera.x = LevelManager.xSpawn; y = LevelManager.ySpawn; } public void mousePressed(MouseEvent e) { if(e.getButton()==MouseEvent.BUTTON1) { leftMouseDown = true; } } public void mouseReleased(MouseEvent e) { int xx = e.getX(); int yy = e.getY(); if(e.getButton()==MouseEvent.BUTTON1) { leftMouseDown = false; if (GamePanel.bombs.size() < 3) { GamePanel.bombs.add(new StickyBomb(GamePanel.player.x + GamePanel.camera.x, GamePanel.player.y, 25, (xx - GamePanel.player.x) / 20, (yy - GamePanel.player.y) / 10, GamePanel.bomb, GamePanel.explosionArray)); } } } public void addParticle(int x) throws IOException { if(GlobalState.randInt(1,3)==3) { GamePanel.particles.add(new Particle(this.x + GamePanel.camera.x + WIDTH / 2 + GlobalState.randInt(-PLAYER_WIDTH / 2, PLAYER_WIDTH / 2) , (int) (y + HEIGHT * 0.95), GlobalState.randInt(-2, 2) + x, GlobalState.randInt(-4, 1), GlobalState.randInt(1, 7), "img/particles/GrassParticle.png")); } } public int draw(Graphics g, int frame) { frame %= spriteArray[0][0].length; if (!upPressed && !downPressed && !leftPressed && !rightPressed) { g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x-10, y, null); return 0; } else { lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2; lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2; lastFrame = frame; g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x-10, y, null); return 1; } } }