/* Eric Li, ICS4U, Completed 5/29/2022 Paddle class defines behaviours for the left and right player-controlled paddles */ import java.awt.*; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; public class Player extends GenericSprite { public final int SPEED = 5; public static final int PLAYER_WIDTH = 72; public static final int PLAYER_HEIGHT = 97; public int lastXDirection, lastYDirection, lastFrame; public int upKey, downKey, rightKey, leftKey; // 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) { super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH); this.upKey = upKey; this.downKey = downKey; this.leftKey = leftKey; this.rightKey = rightKey; spriteArray = sprites; } // moves paddle when key is pressed public void keyPressed(KeyEvent e) { if(e.getKeyChar() == 'd'){ rightPressed = true; } if(e.getKeyChar() == 'a'){ leftPressed = true; } if(e.getKeyChar() == 'w'){ upPressed = true; } if(e.getKeyChar() == 's'){ downPressed = true; } move(); } // stops moving paddle when key is released public void keyReleased(KeyEvent e) { if(e.getKeyChar() == 'd'){ rightPressed = false; move(); } if(e.getKeyChar() == 'a'){ leftPressed = false; move(); } if(e.getKeyChar() == 'w'){ upPressed = false; move(); } if(e.getKeyChar() == 's'){ downPressed = false; move(); } } // calls parent public void move(){ y = y + (int)yVelocity; GamePanel.camera.x = GamePanel.camera.x + (int)xVelocity; if(rightPressed){ xVelocity+=1; } if(leftPressed) { xVelocity -= 1; } if(upPressed&isGrounded){ yVelocity = -10; } xVelocity*=0.9; yVelocity+=0.3; capSpeed(); } public void capSpeed(){ if(xVelocity>speedCap){ xVelocity = speedCap; } else if(xVelocity<-1*speedCap) { xVelocity = -1*speedCap; } } public int draw(Graphics g, int frame) { // g.setColor(Color.WHITE); if (!upPressed && !downPressed && !leftPressed && !rightPressed) { g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x, 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, y, null); return 1; } } }