2022-05-31 18:19:23 +01:00
|
|
|
/* Eric Li, ICS4U, Completed 5/29/2022
|
|
|
|
|
|
|
|
Paddle class defines behaviours for the left and right player-controlled paddles */
|
|
|
|
|
2022-06-03 18:50:13 +01:00
|
|
|
import javax.sound.sampled.LineUnavailableException;
|
|
|
|
import javax.sound.sampled.UnsupportedAudioFileException;
|
2022-05-31 18:19:23 +01:00
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.awt.image.ImageObserver;
|
2022-06-03 18:50:13 +01:00
|
|
|
import java.io.IOException;
|
2022-05-31 18:19:23 +01:00
|
|
|
|
|
|
|
public class Player extends GenericSprite {
|
|
|
|
public final int SPEED = 5;
|
2022-06-05 22:33:24 +01:00
|
|
|
public static final int PLAYER_WIDTH = 64;
|
|
|
|
public static final int PLAYER_HEIGHT = 94;
|
2022-06-01 19:39:57 +01:00
|
|
|
public int lastXDirection, lastYDirection, lastFrame;
|
2022-05-31 18:19:23 +01:00
|
|
|
public int upKey, downKey, rightKey, leftKey;
|
2022-06-03 18:50:13 +01:00
|
|
|
|
2022-06-03 22:33:21 +01:00
|
|
|
private final Sound jump;
|
2022-05-31 18:19:23 +01:00
|
|
|
// sA[0] is -x, -y
|
|
|
|
// sA[1] is x, -y
|
|
|
|
// sA[2] is -x, y
|
|
|
|
// sA[3] is x, y
|
2022-06-01 19:39:57 +01:00
|
|
|
public BufferedImage[][][] spriteArray;
|
2022-06-03 18:50:13 +01:00
|
|
|
public Player(int x, int y, int upKey, int downKey, int leftKey, int rightKey, BufferedImage[][][] sprites) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
|
2022-05-31 18:19:23 +01:00
|
|
|
super(x, y, PLAYER_HEIGHT, PLAYER_WIDTH);
|
2022-06-03 18:50:13 +01:00
|
|
|
jump = new Sound("sound/jump.wav");
|
2022-05-31 18:19:23 +01:00
|
|
|
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) {
|
2022-06-02 18:21:29 +01:00
|
|
|
if(e.getKeyChar() == 'd'){
|
|
|
|
rightPressed = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
if(e.getKeyChar() == 'a'){
|
|
|
|
leftPressed = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
if(e.getKeyChar() == 'w'){
|
|
|
|
upPressed = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
if(e.getKeyChar() == 's'){
|
|
|
|
downPressed = true;
|
|
|
|
|
|
|
|
}
|
2022-05-31 18:19:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// stops moving paddle when key is released
|
|
|
|
public void keyReleased(KeyEvent e) {
|
2022-06-02 18:21:29 +01:00
|
|
|
if(e.getKeyChar() == 'd'){
|
|
|
|
rightPressed = false;
|
|
|
|
}
|
|
|
|
if(e.getKeyChar() == 'a'){
|
|
|
|
leftPressed = false;
|
|
|
|
}
|
|
|
|
if(e.getKeyChar() == 'w'){
|
|
|
|
upPressed = false;
|
|
|
|
}
|
|
|
|
if(e.getKeyChar() == 's'){
|
|
|
|
downPressed = false;
|
|
|
|
}
|
2022-05-31 18:19:23 +01:00
|
|
|
}
|
|
|
|
|
2022-06-05 16:53:03 +01:00
|
|
|
private boolean collide(Tile tile, double x, double y){
|
|
|
|
if(!tile.collision){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(x+PLAYER_WIDTH>tile.realX&&x<tile.realX+Tile.length&&y-tile.y<Tile.length&&tile.y-y<PLAYER_HEIGHT){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-31 18:19:23 +01:00
|
|
|
// calls parent
|
2022-06-05 16:53:03 +01:00
|
|
|
|
2022-06-05 22:33:24 +01:00
|
|
|
public boolean canUpdate(double x, double y){
|
2022-06-05 16:53:03 +01:00
|
|
|
boolean canUpdate = true;
|
|
|
|
for(Tile i: GamePanel.map){
|
|
|
|
if(collide(i,this.x+x,this.y+y)){
|
|
|
|
canUpdate = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return canUpdate;
|
|
|
|
}
|
2022-06-02 18:21:29 +01:00
|
|
|
public void move(){
|
2022-06-05 16:53:03 +01:00
|
|
|
|
2022-06-05 22:33:24 +01:00
|
|
|
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
|
|
|
|
GamePanel.camera.x += -Math.signum(xVelocity);
|
|
|
|
}
|
2022-06-05 16:53:03 +01:00
|
|
|
if(!canUpdate(xVelocity, 0)){
|
|
|
|
int updateAmount = 0;
|
|
|
|
if(xVelocity>0){
|
|
|
|
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;
|
2022-06-05 22:33:24 +01:00
|
|
|
}
|
|
|
|
if(!canUpdate(0, yVelocity)){
|
2022-06-05 16:53:03 +01:00
|
|
|
if(yVelocity>0){
|
|
|
|
while(canUpdate(0,1)){
|
|
|
|
y+=1;
|
|
|
|
}
|
|
|
|
isGrounded = true;
|
2022-06-05 22:33:24 +01:00
|
|
|
} else if(yVelocity<0){
|
2022-06-05 16:53:03 +01:00
|
|
|
while(canUpdate(0,-1)){
|
|
|
|
y-=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
yVelocity = 0;
|
|
|
|
}
|
|
|
|
if(canUpdate(xVelocity, yVelocity)) {
|
|
|
|
y = y + (int) yVelocity;
|
|
|
|
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity;
|
|
|
|
}
|
|
|
|
|
2022-06-02 18:21:29 +01:00
|
|
|
if(rightPressed){
|
|
|
|
xVelocity+=1;
|
|
|
|
}
|
|
|
|
if(leftPressed) {
|
|
|
|
xVelocity -= 1;
|
|
|
|
}
|
|
|
|
if(upPressed&isGrounded){
|
2022-06-05 16:53:03 +01:00
|
|
|
y-=1;
|
|
|
|
isGrounded = false;
|
|
|
|
if(canUpdate(0,-8)) {
|
|
|
|
jump.start();
|
|
|
|
}
|
2022-06-02 18:21:29 +01:00
|
|
|
yVelocity = -10;
|
|
|
|
}
|
|
|
|
xVelocity*=0.9;
|
2022-06-05 16:53:03 +01:00
|
|
|
if(!isGrounded) {
|
|
|
|
yVelocity += 0.3;
|
2022-06-05 22:33:24 +01:00
|
|
|
if(downPressed){
|
|
|
|
yVelocity+=1;
|
|
|
|
}
|
2022-06-05 16:53:03 +01:00
|
|
|
}
|
2022-06-02 18:21:29 +01:00
|
|
|
capSpeed();
|
|
|
|
}
|
|
|
|
|
2022-05-31 18:19:23 +01:00
|
|
|
public int draw(Graphics g, int frame) {
|
2022-06-03 22:33:21 +01:00
|
|
|
frame %= spriteArray[0][0].length;
|
2022-06-01 19:39:57 +01:00
|
|
|
if (!upPressed && !downPressed && !leftPressed && !rightPressed) {
|
2022-06-05 22:33:24 +01:00
|
|
|
g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x-6, y, null);
|
2022-05-31 18:19:23 +01:00
|
|
|
return 0;
|
|
|
|
} else {
|
2022-06-01 19:39:57 +01:00
|
|
|
lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2;
|
|
|
|
lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2;
|
|
|
|
lastFrame = frame;
|
2022-06-05 22:33:24 +01:00
|
|
|
g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x-6, y, null);
|
2022-05-31 18:19:23 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|